Flutter组件

Flutter组件

ScreenUtil

不同的设备, 尺寸不同, 最简单适配方式是: 等比缩放

flutter_screenutil 扩展包解决设备适配问题,使用简单方便

修改文件 pubspec.yaml, 添加扩展包

1
2
dependencies:
flutter_screenutil: ^5.9.3

修改 MyApp 类的 build 方法

修改前

1
2
3
4
5
6
7
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}

修改后

1
2
3
4
5
6
7
8
9
10
11
12
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(375, 812),
builder: (context, child) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
},
);
}

宽、高尺寸设置

1
2
3
4
5
6
7
Container(
width: ScreenUtil().setWidth(375),
height: ScreenUtil().setHeight(400),
decoration: BoxDecoration(
color: Colors.red,
),
),

Flutter组件
http://example.com/2024/11/09/Flutter/ScreenUtil/
作者
John Doe
发布于
2024年11月9日
许可协议