赞
踩
我们在布局中使用ScreenUtil().screenWidth获取屏幕宽度是,在初始化未装载视图时,第一次获取时拿不到,会出现如上的崩溃。
import 'package:flutter_screenutil/screenutil_init.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @override Widget build(BuildContext context) { //使用屏幕初始化包裹整个body布局 return ScreenUtilInit( //设置初始化屏幕大小 designSize: Size(375, 812), builder: () => Scaffold( backgroundColor: WBColors.color_f4f5f7, body: Container( height: 263.5, width: ScreenUtil().screenWidth, ) ) ); }
使用ScreenUtilInit屏幕初始化组件包裹整个页面布局最外层
设置屏幕初始大小
body里面可任意使用获取屏幕宽高的API了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。