Flutter屏幕适配插件adaptable_screen的使用
Flutter屏幕适配插件adaptable_screen的使用
介绍
本README描述了该插件。如果你将此包发布到pub.dev,那么此README的内容将会出现在你的包的首页。
为了了解更多如何编写好的包README的信息,请查看 撰写包页面指南。
对于一般开发包的信息,请参阅Dart的 创建库包指南 和 Flutter的 开发包和插件指南。
该插件根据UI设计原型的大小和组件放置的窗口大小来缩放尺寸。
开始使用
添加依赖
在你的包的pubspec.yaml
文件中添加以下依赖:
dependencies:
adaptable_screen: ^0.0.4
然后安装依赖:
$ flutter pub get
使用方法
要根据设计构建时的大小设置宽度和高度,可以使用AdaptableScreenUtils.init
函数初始化大小。此函数应在应用程序启动时调用。
void main() {
AdaptableScreenUtils.init(
heightUIDisign: 1000,
widthUIDisign: 956,
);
runApp(const MyApp());
}
高度适配
使用.h
属性基于窗口大小缩放高度:
Container(
height: 10.h
)
宽度适配
使用.w
属性基于窗口大小缩放宽度:
Container(
width: 10.w
)
文本适配
使用.ssp
属性基于窗口大小缩放文本大小。此函数可以根据窗口大小改变字体大小:
Text(
"Text Exemplo",
style: TextStyle(fontSize: 15.ssp),
)
文本适配(另一种方式)
使用.sp
属性基于窗口大小缩放文本大小:
Text(
"Text Exemplo",
style: TextStyle(fontSize: 15.ssp),
)
圆角适配
使用.r
属性基于最小窗口大小缩放圆角半径:
Container(
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(10.r),
),
height: 50.h,
width: 100.w,
child: Center(
child: Text(
"Text Exemplo",
style: TextStyle(fontSize: 15.sp),
),
),
)
完整示例
如果你想看到一个简单的使用示例,可以查看/example
文件夹。
完整的示例代码如下:
import 'package:adaptable_screen/adaptable_screen.dart';
import 'package:flutter/material.dart';
void main() {
AdaptableScreenUtils.init(
heightUIDisign: 1000,
widthUIDisign: 956,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: [
Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(33.r),
),
height: 160.h,
width: 200.w,
child: Center(
child: Text(
"Text Exemplo",
style: TextStyle(fontSize: 20.ssp),
),
),
),
Container(
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(10.r),
),
height: 50.h,
width: 100.w,
child: Center(
child: Text(
"Text Exemplo",
style: TextStyle(fontSize: 15.sp),
),
),
),
],
),
),
);
}
}
更多关于Flutter屏幕适配插件adaptable_screen的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter屏幕适配插件adaptable_screen的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,关于如何在Flutter项目中使用adaptable_screen
插件来进行屏幕适配,以下是一个详细的代码示例。这个插件允许你通过定义设计基准尺寸(通常是设计图稿的尺寸),自动将UI元素缩放到不同屏幕尺寸上。
首先,确保你已经在pubspec.yaml
文件中添加了adaptable_screen
依赖:
dependencies:
flutter:
sdk: flutter
adaptable_screen: ^x.y.z # 替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,按照以下步骤进行配置和使用:
-
初始化
Adaptable
:在你的应用入口文件(通常是
main.dart
)中,初始化Adaptable
。这里假设你的设计基准尺寸是375x667(比如iPhone 8的屏幕尺寸)。
import 'package:flutter/material.dart';
import 'package:adaptable_screen/adaptable_screen.dart';
void main() {
// 初始化Adaptable
Adaptable.init(
context,
designSize: Size(375, 667), // 设计基准尺寸
orientation: Orientation.portrait, // 默认方向
).then((_) {
runApp(MyApp());
});
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
-
使用
Adaptable
进行UI布局:现在你可以使用
Adaptable
提供的方法(如Adaptable.width
,Adaptable.height
,Adaptable.sp
等)来进行UI布局。这些方法会根据当前设备的屏幕尺寸自动缩放。
import 'package:flutter/material.dart';
import 'package:adaptable_screen/adaptable_screen.dart';
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Adaptable Screen Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// 使用Adaptable.width和Adaptable.height进行布局
Container(
width: Adaptable.width(200), // 相当于设计基准中的200px
height: Adaptable.height(100), // 相当于设计基准中的100px
color: Colors.blue,
child: Center(
child: Text(
'Width: 200, Height: 100',
style: TextStyle(color: Colors.white),
),
),
),
SizedBox(height: Adaptable.height(20)),
// 使用Adaptable.sp进行文字大小适配
Text(
'Hello, Adaptable Screen!',
style: TextStyle(fontSize: Adaptable.sp(18)), // 相当于设计基准中的18sp
),
],
),
),
);
}
}
在这个示例中,Adaptable.width(200)
和Adaptable.height(100)
分别表示在设计基准尺寸(375x667)下的200像素宽度和100像素高度。当应用运行在不同尺寸的设备上时,这些值会自动按比例缩放。同样,Adaptable.sp(18)
用于文本大小适配,确保文本在不同屏幕上保持一致的视觉比例。
通过这种方式,你可以轻松地在Flutter应用中实现屏幕适配,确保UI在不同设备上都能保持良好的用户体验。