Flutter屏幕适配与响应式布局插件adaptive_responsive_util的使用
Flutter屏幕适配与响应式布局插件adaptive_responsive_util
的使用
该插件旨在简化屏幕适配和响应式布局的开发过程。它包含了一些方便的工具,可以帮助开发者根据不同的平台或屏幕方向构建更加适应的UI。
插件信息
该插件由以下视频启发:https://www.youtube.com/watch?v=MrPJBAOzKTQ&t=262s
Adaptive 或/和 Responsive 小部件
AdaptiveScreen
小部件
- 如果你需要根据不同的平台(如网站、Android、iOS、Mac、Windows)来构建页面或特定的小部件,请使用此小部件。
OrientationScreen
小部件
- 如果你需要根据页面的方向(横屏或竖屏)来构建页面或特定的小部件,请使用此小部件。
ResponsivePortraitScreen
小部件
- 如果你希望在竖屏模式下形成组/分支以适应屏幕尺寸条件,请使用此小部件。(确保也在横屏模式下设置相同的配置,使用
ResponsiveLandscapeScreen
小部件)
ResponsiveLandscapeScreen
小部件
- 如果你希望在横屏模式下形成组/分支以适应屏幕尺寸条件,请使用此小部件。(确保也在竖屏模式下设置相同的配置,使用
ResponsivePortraitScreen
小部件)
工具内容
工具内容是另一个来自adaptive / responsive
的包,包含额外的代码以帮助加快编码过程。
灵感来源于这篇文章:https://blog.stackademic.com/flutter-extensions-tricks-to-boost-your-productivity-88573b7efc0f
在BuildContext
上扩展
这个扩展用于多个部分,例如主题、媒体查询、显示静态UI(如SnackBar和AlertDialog),以及基于屏幕方向自定义小部件的简单版本。
简化主题访问
primaryColor => Theme.of(this).primaryColor;
canvasColor => Theme.of(this).canvasColor;
cardColor => Theme.of(this).cardColor;
focusColor => Theme.of(this).focusColor;
dialogBackgroundColor => Theme.of(this).dialogBackgroundColor;
disabledColor => Theme.of(this).disabledColor;
dividerColor => Theme.of(this).dividerColor;
highlightColor => Theme.of(this).highlightColor;
hintColor => Theme.of(this).hintColor;
hoverColor => Theme.of(this).hoverColor;
indicatorColor => Theme.of(this).indicatorColor;
primaryDark => Theme.of(this).primaryColorDark;
primaryLight => Theme.of(this).primaryColorLight;
shadowColor => Theme.of(this).shadowColor;
示例:context.primaryColor
简化媒体查询访问
width => MediaQuery.of(this).size.width;
height => MediaQuery.of(this).size.height;
aspectRatio => MediaQuery.of(this).size.aspectRatio;
longestSide => MediaQuery.of(this).size.longestSide;
shortestSide => MediaQuery.of(this).size.shortestSide;
orientation => MediaQuery.of(this).orientation;
padding => MediaQuery.of(this).padding;
示例:context.width
显示SnackBar
示例:context.snackBar('example');
显示AlertDialog
示例:
context.displayAlertDialog(
title: 'title',
content: 'content',
onPositivePressed: () {},
positiveButtonText : 'Ok',
onNegativePressed: () {},
negativeButtonText: 'Cancel',
);
基于方向构建小部件
示例:
import 'package:flutter/material.dart';
import 'extension_on_build_context.dart';
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return context.orientationAction(
onPortrait: () {
return const Column(
children: [],
);
},
onLandscape: () {
return const Row(
children: [],
);
},
);
}
}
更多关于Flutter屏幕适配与响应式布局插件adaptive_responsive_util的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter屏幕适配与响应式布局插件adaptive_responsive_util的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在 Flutter 中,屏幕适配和响应式布局是一个常见的需求,尤其是当应用需要在不同尺寸和分辨率的设备上运行时。adaptive_responsive_util
是一个可以帮助开发者实现屏幕适配和响应式布局的插件。以下是如何使用 adaptive_responsive_util
的基本指南。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 adaptive_responsive_util
插件的依赖:
dependencies:
flutter:
sdk: flutter
adaptive_responsive_util: ^1.0.0 # 请检查最新版本
然后运行 flutter pub get
来安装插件。
基本用法
adaptive_responsive_util
提供了一些工具函数和类来帮助你实现屏幕适配和响应式布局。以下是几个基本的使用场景:
1. 获取屏幕尺寸
你可以使用 ScreenUtil
类来获取屏幕的宽度、高度等信息。
import 'package:adaptive_responsive_util/adaptive_responsive_util.dart';
double screenWidth = ScreenUtil.screenWidth;
double screenHeight = ScreenUtil.screenHeight;
double statusBarHeight = ScreenUtil.statusBarHeight;
double bottomBarHeight = ScreenUtil.bottomBarHeight;
2. 设置设计稿尺寸
通常,设计师会提供一个固定尺寸的设计稿(例如 375x812),你可以使用 ScreenUtil
来设置设计稿的尺寸,以便在不同的设备上进行适配。
ScreenUtil.init(
designSize: Size(375, 812), // 设计稿的尺寸
allowFontScaling: false, // 是否允许字体缩放
);
3. 使用适配尺寸
在布局中,你可以使用 ScreenUtil
提供的 setWidth
和 setHeight
方法来设置控件的尺寸,以确保它们在不同设备上都能正确显示。
Container(
width: ScreenUtil().setWidth(100), // 根据设计稿宽度适配
height: ScreenUtil().setHeight(50), // 根据设计稿高度适配
color: Colors.blue,
);
4. 适配字体大小
你可以使用 setSp
方法来适配字体大小。
Text(
'Hello, Flutter!',
style: TextStyle(
fontSize: ScreenUtil().setSp(16), // 根据设计稿字体大小适配
),
);
5. 响应式布局
adaptive_responsive_util
还提供了一些工具来实现响应式布局。例如,你可以使用 ResponsiveWidget
来根据屏幕宽度调整布局。
ResponsiveWidget(
breakpoints: [
Breakpoint(start: 0, end: 600, name: 'mobile'),
Breakpoint(start: 601, end: 1200, name: 'tablet'),
Breakpoint(start: 1201, end: double.infinity, name: 'desktop'),
],
builder: (context, screenSize) {
if (screenSize == 'mobile') {
return MobileLayout();
} else if (screenSize == 'tablet') {
return TabletLayout();
} else {
return DesktopLayout();
}
},
);
示例代码
以下是一个完整的示例,展示了如何使用 adaptive_responsive_util
进行屏幕适配和响应式布局:
import 'package:flutter/material.dart';
import 'package:adaptive_responsive_util/adaptive_responsive_util.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
ScreenUtil.init(
designSize: Size(375, 812),
allowFontScaling: false,
);
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Adaptive Responsive Util'),
),
body: ResponsiveWidget(
breakpoints: [
Breakpoint(start: 0, end: 600, name: 'mobile'),
Breakpoint(start: 601, end: 1200, name: 'tablet'),
Breakpoint(start: 1201, end: double.infinity, name: 'desktop'),
],
builder: (context, screenSize) {
if (screenSize == 'mobile') {
return MobileLayout();
} else if (screenSize == 'tablet') {
return TabletLayout();
} else {
return DesktopLayout();
}
},
),
);
}
}
class MobileLayout extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Container(
width: ScreenUtil().setWidth(200),
height: ScreenUtil().setHeight(100),
color: Colors.blue,
child: Center(
child: Text(
'Mobile Layout',
style: TextStyle(
fontSize: ScreenUtil().setSp(16),
color: Colors.white,
),
),
),
),
);
}
}
class TabletLayout extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Container(
width: ScreenUtil().setWidth(300),
height: ScreenUtil().setHeight(150),
color: Colors.green,
child: Center(
child: Text(
'Tablet Layout',
style: TextStyle(
fontSize: ScreenUtil().setSp(20),
color: Colors.white,
),
),
),
),
);
}
}
class DesktopLayout extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: Container(
width: ScreenUtil().setWidth(400),
height: ScreenUtil().setHeight(200),
color: Colors.red,
child: Center(
child: Text(
'Desktop Layout',
style: TextStyle(
fontSize: ScreenUtil().setSp(24),
color: Colors.white,
),
),
),
),
);
}
}