Flutter 是一个跨平台的 UI 工具集,它的设计初衷,就是允许在各种操作系统上复用同样的代码,例如 iOS 和 Android,同时让应用程序可以直接与底层平台服务进行交互。
其核心定位是让开发者在**不同平台上交付拥有原生体验的高性能应用**,在尽可能共享复用代码的同时包容不同平台的差异。
1. 概述
Flutter 是一个跨平台的 UI 工具集,旨在让开发者用同一套代码在 iOS、Android 等操作系统上复用,并直接与底层平台服务交互。其核心定位是在尽可能共享复用代码的同时包容平台差异,为不同平台交付拥有原生体验的高性能应用。本文围绕 Dart 语言基础、Flutter 开发环境与核心组件、布局导航、动画与状态管理,以及数据库与本地存储,系统梳理了 Flutter 的相关技术要点。
2. Dart 语言基础
Dart 是 Flutter 的编程语言,理解其语法与核心特性是开发 Flutter 应用的前提。本章按基础语法、函数、类、继承、泛型、新特性与性能优化进行梳理。
1.1 基础
1.1.1 入口方法
main.dart
1.1.2 注释
//
/**/
///
1.1.3 定义变量
var strs= “dart”;
print(strs);
1.1.4 声明变量
字符串
String str=”dart”;
数字类型
int num = 10;
1.1.5 修饰符
var
final 只能赋值一次不能修改
const 一开始就赋值 常量不可变
1.1.6 声明字符串
print(“$str1$str2”)
print(str1+str2) //字符串的连接
1.1.7 List 的声明
var list
var list = ["test",20,true];
//设置存放指定类型
var list =<String>["test","test1"];
list.length; //===>获取长度
list.add("add"); //===>添加元素
创建一个固定长度的数组
第一个参数为长度,第二个参数为填充的内容,无法增加数据,无法修改长度
void main() {
var list=List.filled(2,"1");
print(list);
}
1.1.8 Map
第一种方式
var persion={
"name":"Dart",
"age":"8"
};
print(persion); //输出 {name: Dart, age: 8}
print(persion['name']); //输出 Dart
print(persion['age']); //输出 8
第二种方式
var persion1=new Map();
persion1["name"]="张三";
persion1["age"]="9";
print(persion1); //输出 {name: 张三, age: 9}
print(persion1['name']); //输出 张三
print(persion1['age']); //输出 9
1.1.9 is 判断类型
var str = '111';
if (str is String) {
print("str is String type");
} else if (str is int) {
print("str is int type");
} else {
print("str is other type");
}
1.1.10 运算符
+-*/ %取余 ~/取整
> , < , <= , >= , == , !=
&&并且 || 或者 !取反
+=,-=,*=,/=,%=,~/=
判断为空之后赋值(??=)
int b;
b??=6; //当 b在这之前没有被赋值,则在这行代码中会被赋值
//解释:当a为null时,赋值??符号后的10给a,然后将a赋值给b.
// 当a不为null时,直接将a赋值给b
var a;
var b = a ?? 10;
print(b); // 输出10
1.1.11 类型转换
//将字符串转换为整型
String str='111';
var myNum=int.parse(str); //输出 111
其他类型转 boolean
var mynum="";
if (mynum.isEmpty){
print("空");
}else{
print("非空");
}
// ===>空
a++
a—
自增自减运算
1.1.12 循环
for (int i=0;i<50;i++) while() do()while() 与 java 类似
void main(){
List list = [{
"title":"新闻1",
},{
"title":"新闻2",
},{
"title":"新闻3",
},{
"title":"新闻4",
}];
//for in 循环
for (var ele in list) {
print(ele["title"]);
}
}
1.1.13 List
常用属性
length 长度
reversed 翻转
isEmpty 是否为空
isNotEmpty 是否不为空
//增加 add
list.add("test");
//List<T>.filled(number,ele) 填充元素
List newlist = List<String>.filled(2, "1");
//全部加入,拼接数组 addAll
list.addAll(list1);
//翻转列表
List newlist = list.reversed.toList();
//indexOf()查找元素,找不到返回-1
List list = ["1","2","3"];
print(list.indexOf("1"));
//remove()移除
list.remove("1");
//fillRange(1,2,'aaa'); 从1开始到索引2,填充为'aaa'
list.fillRange(1,2,'aaa');
//insert() 在索引值插入插入
list.insert(1,"4");
//insertAll 插入全部数组
list.insertAll(1,["5","6"]);
//join拼接数组
var desc = list.join('-');
//split
var list = desc.split('-');
1.1.14 Set
去除数组重复的内容
Set 是没有顺序且不能重复的集合,所以不能通过所有去获取值
keys 获取所有的 key 值
values 获取所有的 value 值
isEmpty 判断是否为空
isNotEmpty 判断是否不为空
1.1.15 遍历
1.1.15.1 forEach
list.forEach((value){
print(value);
});
Map person = {
"name":"tom",
"age":19
};
person.forEach((key,value){
print(key.toString()+"-"+value.toString());
});
1.1.15.2 map
var newlist = list.map((value){
return value + "6";
});
print(newlist.toList());
1.1.15.3 any
var res = list.any((value){
return value>5;
});
print(res);
//有一个满足返回true
1.1.15.4 every
var res = list.every((value){
return value>5;
});
print(res);
//全部满足返回true
1.2 函数
1.2.1 定义
可以在 main 函数内或者 main 函数外
void printinfo(){
print("test");
}
printinfo();
1.2.2 可选参数
此时的 gender 和 age 为可选参数
String test(String name,[String gender,int age]){
if (age != null){
return "name:${name},gender:${gender}";
}
return "name:${name},gender:${gender},age:${age}";
}
1.2.3 默认参数
String test(String name,[String gender="man",int age]){
if (age != null){
return "name:${name},gender:${gender}";
}
return "name:${name},gender:${gender},age:${age}";
}
1.2.4 命名参数
String test(String name,{String gender="man",int age}){
if (age != null){
return "name:${name},gender:${gender}";
}
return "name:${name},gender:${gender},age:${age}";
}
//调用的参数和形参名相同
1.2.5 将函数当做参数传递
fn1(fn2){
fn2();
}
fn2(){
print("object");
}
fn1(fn2);
1.2.6 使用匿名函数传递参数
var fn = ()=>{
print("mytest");
};
fn3(fn){
fn();
}
fn3(fn);
1.2.7 箭头函数
list.forEach((item)=>print(item.toString()));
1.2.8 自执行方法
((value){
print(value);
})(2);
1.2.9 闭包
void test1(){
var a = 0;
print(a);
a++;
}
test1();
1.3 类
1.3.1 定义
class Person{
String name = "zs";
String email = "123@git.com";
String address = "America";
void getinfo(){
print("${name}${email}${address}");
}
}
var p1 = new Person();
print(p1.name);
p1.getinfo();
1.3.2 构造函数
Person(String name,String email,String address){
this.name = name;
this.email = email;
this.address = address;
}
1.3.3 简写的构造函数
Person(this.name,this.email,this.address);
1.3.4 命名构造函数
Person.now(){
print("命名构造函数");
}
var p = new Person.now();
1.3.5 私有成员属性,加下划线
class Person{
//私有属性
String _name = "zs";
String _email = "123@git.com";
String _address = "America";
Person();
//私有方法
String _getname(){
return this._name;
}
void getinfo(){
print("${this._name}${this._email}${this._address}");
}
}
1.3.6 get set
class Rect{
num height;
num width;
Rect(this.height,this.width);
get area{
return height*width;
}
set areaheight(value){
this.height = value;
}
}
1.3.7 在实例化之前进行赋值操作
Rect():height=2,width=10{
}
1.3.8 静态属性和静态方法 static
在属性和方法前加上 static
非静态方法可以调用静态成员和非静态成员
静态方法无法访问非静态的属性和方法
1.3.9 对象操作符
?条件运算符
as 类型转换
is 类型判断 Person is Object ====>true
..联级操作
var p;
//不为空才调用
p?.getinfo();
var s;
s="";
s=new Person();
(s as Person).getinfo();
//..联级操作 赋值之后进行调用方法操作
Person p = new Person();
p..name = "city"
..email = "123@m.com"
..getinfo();
1.4 继承
1.4.1 定义继承
class Animal{
}
class Cat extends Animal{
}
1.4.2 实例化子类给父类传参
class Animal{
String name;
String kind;
Animal(this.name,this.kind);
getinfo(){
print("${name}-${kind}");
}
}
class Cat extends Animal{
Cat(String name,String kind):super(name,kind);
}
在覆写父类的方法时可以加上 @override 也可以不写
super.父类方法名 调用父类的方法
1.4.3 抽象类
抽象并且定义标准
父类定义一个方法不去实现,子类有不同的实现 —》 多态
abstract class Wolf{
void run();
}
class Dog extends Wolf{
void run(){
print("run");
}
}
1.4.4 一个类实现多个接口
abstract class A{
void run();
}
abstract class B{
void walk();
}
class C implements A,B{
void run() {
// TODO: implement run
}
void walk() {
// TODO: implement walk
}
}
1.4.5 mixins
mixin A{
void run(){
print("A");
}
}
mixin B{
void walk(){
print("B");
}
}
class C with A,B{
}
1.作为 mixins 的类只能继承自 Object,不能继承其他类
2.作为 mixins 的类不能有构造函数
3.一个类可以 mixins 多个 mixins 的类
4.minxins 不是继承,也不是接口,而是一种特性
C c = new C()
print(C is A)
print(c is B)
print(c is C)
- true true true
1.5 泛型
1.5.1 泛型方法
print(getData<num>(12));
T getData<T>(T value){
return value;
}
1.5.2 泛型类
class MyList<T>{
List list = <T>[];
void add(value){
list.add(value);
}
}
1.5.3 泛型接口
文件缓存,内存缓存
1.getBykey 和 setBykey
abstract class Cache<T>{
getByKey(String key);
setByKey(String key,T value);
}
class FileCache<T> implements Cache{
getByKey(String key) {
return null;
}
setByKey(String key, value) {
print(key +"="+ value);
}
}
class MemoeryCache<T> implements Cache{
getByKey(String key) {
return null;
}
setByKey(String key, value) {
print(key +"="+ value);
}
}
1.5.4 内置库 dart:io
可以使用 async 和 await
async 让方法变成异步
await 等待异步方法的完成
1.5.5 第三方库
pub get
The official repository for Dart and Flutter packages. (pub.dev)
import '' show get; //从一个模块仅导入get方法
import '' hiden get; //不需要get方法
import '' deferred; //懒加载,用到才导入
1.6 2.13 新特性
1.6.1 可空类型
int a=12; //声明a为不可空类型
a = null;
//此时会报错,因为dart有Null safety
int? a=12; //声明a为可空类型
a=null;
String test(url){
if (url !=null){
return 'test';
}
return null;
}
//报错,返回值可能为null
String? test(url){
if (url !=null){
return 'test';
}
return null;
}
//声明返回值为可空
1.6.2 类型断言
void test(String? url){
//如果不为空会打印出长度,为空会抛出异常
print(url!.length);
}
void test(String? url){
//如果不为空会打印出长度,为空会抛出异常
try{
print(url!.length);
}catch(error){
print("url为空");
}
}
1.6.3 late 关键字
late 关键字用来延迟初始化
class person{
late String name;
late String age;
void setname(String name,String age){
this.name = name;
this.age = age;
}
}
1.6.4 require 用来标记为必填参数
void PrintTest({required String name,required String age}){
}
1.7 性能优化
1.7.1 常量
const PI = 3.14 –>定义常量
- const final
- const 声明的常量是在编译时确定的
- final 声明的常量允许声明后再赋值,赋值后不可改变
1.7.2 identical
var a = new Object();
var b = new Object();
print(identical(a, b)); //查看两个对象是否共用同一个内存空间
// ---->false
const 在多个地方定义相同的对象时,内存只保留了一个对象
const a = Object();
const b = Object();
print(identical(a, b)); //查看两个对象是否共用同一个内存空间
// ---->true
1.7.3 常量构造函数
常量构造函数以 const 开头
const 构造函数必须用于成员变量都是 final 的类
如果实例化不加 const,即使调用的是常量构造函数 ,实例化的成员也不是常量实例对象
class person{
final String name;
final String age;
const person({required this.name,required this.age});
}
const a = const person(name:"test",age:"12");
const b = const person(name:"test",age:"12");
print(identical(a, b)); //查看两个对象是否共用同一个内存空间
//---->true
3. Flutter 基础环境
配置 JDK,下载 Android studio
2.1 配置 Flutter
2.1.1 下载 SDK
https://docs.flutter.dev/get-started/install/windows/mobile
2.1.2 配置镜像
PUB_HOSTED_URL:https://pub.flutter-io.cn
FLUTTER_STORAGE_BASE_URL:https://storage.flutter-io.cn
2.1.3 配置环境变量
sdk 的 bin 目录
2.1.4 报错说需要同意证书
安装安卓命令行工具 cmdline-tools
flutter doctor –android-licenses
2.2 flutter 项目文件
创建 flutter 项目
flutter create rctool
lib –> 所有的代码将会写在 lib 目录下
pubspec.yaml —>配置文件
2.3 入口 main.dart
void main(){
runApp(const Center(
child: Text("哈哈哈",textDirection: TextDirection.ltr,style: TextStyle(
color: Color.fromRGBO(35, 67, 188, 1), //字体颜色
fontSize: 40 //字体大小
),),
));
}
4. Flutter 核心组件
MaterialApp 和 Scaffold 两个组件装饰 APP
3.1 MaterialApp
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("hello flutter")),
body: const Center(
child: Text("哈哈哈",textDirection: TextDirection.ltr,style: TextStyle(
color: Color.fromRGBO(35, 67, 188, 1), //字体颜色
fontSize: 40 //字体大小
))
))
));
statelessWidget 无状态组件,状态不可变的 Widget
statefulWidget 状态组件,状态可能在生命周期发生改变
class myApp extends StatelessWidget{
const myApp({Key? key}) : super(key: key);
Widget build(BuildContext context) {
return const Center(
child: Text("哈哈哈",textDirection: TextDirection.ltr,style: TextStyle(
color: Color.fromRGBO(35, 67, 188, 1), //字体颜色
fontSize: 40 //字体大小
))
);
}
}
3.2 Container 容器组件
Container 的常用属性可以通过下表快速查阅:
| 属性 | 说明 |
|---|---|
| alignment | 容器中子组件 child 的对齐方式 |
| margin | Container 与外部其他组件的距离 |
| padding | Container 边缘与 Child 之间的距离 |
| decoration | 容器的修饰器,用于背景或者 border |
| transform | 根据需求调整旋转的角度 |
| width | 宽度 |
| height | 高度 |
常用 alignment 取值如下:
| 对齐方式 | 说明 |
|---|---|
| topCenter | 顶部居中对齐 |
| topLeft | 顶部左对齐 |
| topRight | 顶部右对齐 |
| center | 水平垂直居中对齐 |
| centerLeft | 垂直居中水平居左对齐 |
| centerRight | 垂直居中水平居右对齐 |
| bottomCenter | 底部居中对齐 |
| bottomLeft | 底部居左对齐 |
| bottomRight | 底部居右对齐 |
如果在 decoration 中用了 color,就把容器中设置的 color 去掉,不要重复设置 color;设置的边框样式是让四条边框的宽度为 8px,颜色为黑色。
class myApp extends StatelessWidget{
const myApp({super.key});
Widget build(BuildContext context) {
return Center(
child: Container(
alignment: Alignment.center, //对齐位置
width: 100,
height: 100,
//margin: EdgeInsetsGeometry.lerp(),
decoration: BoxDecoration(
color: Colors.red, //背景颜色
border: Border.all( //边框
color: Colors.blue,
width: 2
),
borderRadius: BorderRadius.circular(50), //圆角
boxShadow: const [ //配置阴影效果
BoxShadow(color:Colors.black,
blurRadius: 10.0
)
],
gradient: const LinearGradient(colors: [Colors.amberAccent,Colors.red])
),
child: const Text("data",style: TextStyle(color: Colors.amberAccent),),
)
);
}
}
margin
margin: const EdgeInsets.fromLTRB(left, top, right, bottom)
transform 移动
transform: Matrix4.translationValues(x, y, z), //位移
transform: Matrix4.rotationX(radians), //旋转 radians为角度
transform: Matrix4.skewX(alpha), //缩放
3.3 Text 组件
| style | 文字样式(颜色、粗细) |
|---|---|
| textAlign | 文字对齐样式 |
| overflow | 文字字数超出后样式 |
| textScaleFactor | 文字大小 |
| maxLines | 最大行数 |
3.4 Image 组件
3.4.1 加载远程图片
Image.network("")
child: Image.asset("assert/a8f6066ff11c400a10419beba3dd2a61.jpg",
fit: BoxFit.fill,
),
fit 可以控制图片的填充形式,宽度填满等
实现圆角图片
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(150),
image: const DecorationImage(
image: NetworkImage("https://pss.bdstatic.com/static/superman/img/logo/bd_logo1-66368c33f8.png"),
fit:BoxFit.cover
),
),
ClipOval 实现圆角图片
return ClipOval(
child: Image.network("https://pss.bdstatic.com/static/superman/img/logo/bd_logo1-66368c33f8.png",
width: 150,
height: 150,
fit: BoxFit.cover,
),
);
3.4.2 加载本地图片
pubspec.yaml
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- images/a.jpg
- images/2.0x/a.jpg
- images/3.0x/a.jpg
3.5 Icon 图标
Icon(Icons.home,size: 40,color: Colors.blue,)
3.5.1 pubspec.yaml
fonts:
- family: richuicon //自定义字体名
fonts:
- asset: fonts/iconfont.ttf
3.5.2 自定义图标
class richuicon{
static const IconData check = IconData(
0xe645, //unicode编码
fontFamily: "richuicon", //自定义的fontFamily
matchTextDirection: true
);
static const IconData close = IconData(
0xe646, //unicode编码
fontFamily: "richuicon", //自定义的fontFamily
matchTextDirection: true
);
}
3.5.3 使用
Icon(richuicon.check,size: 40,color: Colors.amberAccent,)
3.6 ListView
3.6.1 静态
可滑动
ListView(
children: const <Widget>[myApp(),myButton(),myImage(),Icon(Icons.home,size: 40,color: Colors.blue,),
Icon(richuicon.check,size: 40,color: Colors.amberAccent,)
],
)
scrollDirection Axis.horizationtal 水平列表 Axis.vertical 垂直列表
3.6.2 动态展示数据
class mylist extends StatelessWidget {
List<Widget> initdata(){
List<Widget> list = [];
for (var i=0;i<10;i++){
list.add( ListTile(
title: Text("data$i"),
));
}
return list;
}
const mylist({super.key});
Widget build(BuildContext context) {
return ListView(
children: initdata(),
);
}
}
使用 ListViewBuilder
class myview extends StatelessWidget {
List<String> list = [];
myview({super.key}){
for (var i=0;i<10;i++){
list.add("data$i");
}
}
Widget build(BuildContext context) {
return ListView.builder(
itemCount: list.length,
itemBuilder:(context,index){
return ListTile(
title: Text("data:${list[index]}"),
);
},
);
}
}
3.7 网格组件 GridView
基本用法
class mygrid extends StatelessWidget{
const mygrid({super.key});
Widget build(BuildContext context) {
return GridView.count(crossAxisCount: 3, //一行有多少Widget
children: const [
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter),
Icon(Icons.bike_scooter)
],
);
}
}
GridView 常用的四种构建方式对比如下:
| 构建方式 | 说明 |
|---|---|
| GridView.count | 传 int 类型参数(crossAxisCount),创建固定数量的 item,表示横轴 item 个数 |
| GridView.extent | 传 double 类型参数(maxCrossAxisExtent),表示横轴 item 的最大宽度 |
| GridView.builder | 适用于 widget 数量多时,动态创建 widget,需传 itemBuilder |
| GridView.custom | 通过 gridDelegate 与 childrenDelegate 两个代理方法创建 |
| 属性 | 说明 |
|---|---|
| scrollDirection | 滚动方向 |
| reverse | 组件反向排序 |
| controller | 滚动控制(滚动监听) |
| primary | 如果内容不足,则用户无法滚动;而如果 primary 为 true,它们总是可以尝试滚动 |
| physics | 滑动类型设置 AlwaysScrollableScrollPhysics 总是可以滑动 NeverScrollableScrollPhysics 禁止滚动 BouncingScrollPhysics 内容超过一屏 上拉有回弹效果 ClampingScrollPhysics 包裹内容 不会有回弹 |
| shrinkWrap | 默认 false 内容适配 |
| padding | 内边距 |
| crossAxisCount | 列 数量 |
| mainAxisSpacing | 垂直子 Widget 之间间距 |
| crossAxisSpacing | 水平子 Widget 之间间距 |
| childAspectRatio | 子 Widget 宽高比例 |
| addAutomaticKeepAlives | 默认 true |
| addRepaintBoundaries | 默认 true |
| addSemanticIndexes | 默认 true |
| cacheExtent | 设置预加载的区域 |
| children | 子元素 |
| semanticChildCount | 将提供语义信息的子代数量 |
| dragStartBehavior | |
| gridDelegate | GridView.builder 独有,控制子项布局的委托 |
| itemBuilder | 遍历数返回 Widget |
| itemCount | 子控件数量 |
GridView.builder
lass mybuild extends StatelessWidget{
Widget initGridData(context,index){
return Container(
decoration: const BoxDecoration(
color: Colors.blueAccent
),
child: Column(
children: [
Image.asset("images/a.jpg"),
const Text("hahaha")
],
),
);
}
Widget build(BuildContext context) {
return GridView.builder(
padding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
itemCount: 10,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
childAspectRatio: 1.2
), itemBuilder: initGridData);
}
}
3.8 Padding 组件
padding padding 值,EdgeInsets 设置填充的值
child 子组件
3.9 Switch 组件
Switch 是一个用于表示布尔值(开/关)的开关组件,通常用于切换某种状态。它是一个 Material Design 风格的组件,提供了丰富的自定义选项。
3.9.1 基本用法
Switch 组件的最基本用法是通过 value 属性设置当前状态,并通过 onChanged 回调处理状态变化。
Switch(
value: _isSwitched, // 当前开关状态
onChanged: (bool newValue) {
setState(() {
_isSwitched = newValue; // 更新状态
});
},
)
value:布尔值,表示开关的当前状态(true 表示开启,false 表示关闭)。onChanged:当用户切换开关时触发的回调函数,参数是新的布尔值。
3.9.2 自定义样式
Switch 提供了多个属性,可以自定义其外观:
| 属性 | 说明 |
|---|---|
| activeColor | 开关处于开启状态时的颜色 |
| inactiveThumbColor | 开关处于关闭状态时滑块的颜色 |
| inactiveTrackColor | 开关处于关闭状态时轨道的颜色 |
| activeThumbImage / inactiveThumbImage | 使用自定义图像作为滑块 |
| materialTapTargetSize | 设置开关的触摸目标大小 |
2.1 颜色
activeColor:开关处于开启状态时的颜色。inactiveThumbColor:开关处于关闭状态时滑块的颜色。inactiveTrackColor:开关处于关闭状态时轨道的颜色。
Switch(
value: _isSwitched,
onChanged: (bool value) {
setState(() => _isSwitched = value);
},
activeColor: Colors.blue, // 开启时的颜色
inactiveThumbColor: Colors.white, // 关闭时滑块的颜色
inactiveTrackColor: Colors.grey, // 关闭时轨道的颜色
)
2.2 大小
activeThumbImage 和 inactiveThumbImage:可以使用自定义图像作为滑块。materialTapTargetSize:设置开关的触摸目标大小。
Switch(
value: _isSwitched,
onChanged: (bool value) {
setState(() => _isSwitched = value);
},
activeColor: Colors.blue,
inactiveThumbColor: Colors.white,
inactiveTrackColor: Colors.grey,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // 调整触摸目标大小
)
5. 线性布局
4.1 Row 组件
宽度自适应
void main(){
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("hello flutter")),
body: const SizedBox(
height: 400,
width: 400,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, //控制主轴的方向
crossAxisAlignment: CrossAxisAlignment.center, //控制侧轴对其
children:[ IconContainer(),IconContainer(),IconContainer()],
),
)
)
)
);
}
class IconContainer extends StatelessWidget{
const IconContainer({super.key});
Widget build(BuildContext context) {
return Container(
height: 100,
width: 100,
alignment: Alignment.center,
decoration:const BoxDecoration(
color:Colors.red
),
child:const Icon(Icons.add,color: Colors.blueAccent,size: 32,),
);
}
}
4.2 Column 布局
高度自适应
void main(){
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("hello flutter")),
body: const SizedBox(
height: 400,
width: 400,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween, //控制主轴的方向
crossAxisAlignment: CrossAxisAlignment.center, //控制侧轴对其
children:[ IconContainer(),IconContainer(),IconContainer()],
),
)
)
)
);
}
class IconContainer extends StatelessWidget{
const IconContainer({super.key});
Widget build(BuildContext context) {
return Container(
height: 100,
width: 100,
alignment: Alignment.center,
decoration:const BoxDecoration(
color:Colors.red
),
child:const Icon(Icons.add,color: Colors.blueAccent,size: 32,),
);
}
}
4.3 弹性布局
Expanded
flex 设置占的份数
void main(){
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("hello flutter")),
body: const Flex(
direction: Axis.horizontal,
children: [
Expanded(
flex:1,
child: IconContainer()
),
Expanded(
flex:2,
child: IconContainer()
),
],
)
)
)
);
}
4.4 Stack 层叠组件
以堆的方式放入 children 子组件,所有的组件都堆在一起
void main(){
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("hello flutter")),
body: Stack(
children: [
Container(
height: 400,
width: 500,
color: Colors.red,
),
const Text("data",textAlign: TextAlign.center,)
],
)
)
)
);
}
Positioned
- top
- bottom
- left
- right
- height
- width (宽度和高度必须为固定值,不能为 double.infinity)
定位类型 css 的 position
4.5 Align
可以控制子组件的位置
void main(){
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("hello flutter")),
body: const Align(
alignment: Alignment.center,
child: Text("data"),
)
)
);
}
4.6 AspectRatio
aspectRatio 宽高比
class myaspect extends StatelessWidget{
const myaspect({super.key});
Widget build(BuildContext context) {
return AspectRatio(
aspectRatio: 3/1,
child: Container(
color: Colors.red,
),
);
}
}
4.7 Card
class mycard extends StatelessWidget{
const mycard({super.key});
Widget build(BuildContext context) {
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
elevation: 10, //设置阴影深度
margin:const EdgeInsets.fromLTRB(0, 10, 0, 0) ,
child: const Column(
children: [Text("data")],
)
);
}
}
4.8 CircleAvatar
通过 CircleAvatar 实现圆形图片
class mycard extends StatelessWidget{
const mycard({super.key});
Widget build(BuildContext context) {
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
elevation: 10, //设置阴影深度
margin:const EdgeInsets.fromLTRB(0, 10, 0, 0) ,
child: const Column(
children: [Text("data"),
CircleAvatar(
radius: 10,
backgroundImage: AssetImage("images/a.jpg"),
)
],
)
);
}
}
4.9 ElevatedButton、TextButton、IconButton
class myButton extends StatelessWidget{
const myButton({super.key});
Widget build(BuildContext context) {
/* return SizedBox(
child: ElevatedButton(onPressed: (){}, child: const Text("click")),//普通按钮
);*/
/* return SizedBox(
child: TextButton(onPressed: (){}, child: const Text("click")),//文本按钮
); */
/* return SizedBox(
child: OutlinedButton(onPressed: (){}, child: const Text("click")),//文本按钮
);*/
/* return SizedBox(
child: IconButton(onPressed: (){}, icon:const Icon(Icons.access_alarm)),//边框按钮
);*/
return SizedBox(
child: ElevatedButton.icon(onPressed: (){}, icon:const Icon(Icons.access_alarm),label: const Text("提醒",style: TextStyle(color: Colors.red,))),//图标加文字
);
}
}
ButtonStyle
class myButton1 extends StatelessWidget{
const myButton1({super.key});
Widget build(BuildContext context) {
return SizedBox(
child: SizedBox(
child: ElevatedButton.icon(
style: ButtonStyle( //设置按钮样式
backgroundColor: WidgetStateProperty.all(Colors.amberAccent),
foregroundColor: const WidgetStatePropertyAll(Colors.blueAccent)
),
onPressed: (){},
icon:const Icon(Icons.access_alarm),
label: const Text("提醒",style: TextStyle(color: Colors.red,fontSize: 10))),//图标加文字,
),
);
}
}
4.10 Wrap 组件
当 x 轴无法显示时可以显示到 y 轴
void main(){
runApp(MaterialApp(
home: Scaffold(
body: ListView(
padding: const EdgeInsets.all(10),
children:[ Layout("热搜"),Wrap(
spacing: 10, //间距
alignment: WrapAlignment.spaceAround,
runSpacing: 10, //间距
children: [ElevatedButton(onPressed:(){}, child: const Text("1") ),
ElevatedButton(onPressed:(){}, child: const Text("2") ),
ElevatedButton(onPressed:(){}, child: const Text("3") ),
ElevatedButton(onPressed:(){}, child: const Text("4") ),
ElevatedButton(onPressed:(){}, child: const Text("5") ),
ElevatedButton(onPressed:(){}, child: const Text("6") ),
ElevatedButton(onPressed:(){}, child: const Text("7") ),
ElevatedButton(onPressed:(){}, child: const Text("8") ),
ElevatedButton(onPressed:(){}, child: const Text("9") ),
],
),const SizedBox(height: 10,),
Layout("历史记录"),
const Divider(),
const Column(
children: [ListTile(title: Text("5"),
),Divider(),
ListTile(title: Text("6")),
Divider()
],
)
],
)
)
)
);
}
class Layout extends StatelessWidget{
var text;
Layout(this.text,{super.key});
Widget build(BuildContext context) {
return Row(
children: [
Text(text,style: Theme.of(context).textTheme.titleLarge,)
],
);
}
}
6. 有状态组件
实现点击按钮 ++
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState>{
int _num = 0;
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("$_num"),
const SizedBox(height: 10,),
ElevatedButton(onPressed: (){
setState(() {
_num++;
});
}, child:const Text("++"))
],
),
);
}
}
添加一个浮动按钮
void main(){
runApp(
MaterialApp(
title: "richu",
home: Scaffold(
body:HomeState(),
floatingActionButton: FloatingActionButton(onPressed: (){
},child: Text("click me"),),
),
)
);
}
实现动态列表
void main(){
runApp(
const MaterialApp(
title: "richu",
home: HomeState()
));
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState>{
final List<String> _list = [];
int num = 1;
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(onPressed: (){
setState(() {
_list.add("data$num");
num++;
});
},child: const Text("+"),),
appBar: AppBar(title:const Text("richu")),
body: ListView(
children:_list.map((v){
return ListTile(
title: Text(v),
);
}).toList()
),
);
}
}
5.1 BottomNavigationBar
自定义底部导航栏
items 配置底部菜单项
Ontab 点击事件
curretIndex 索引
Iconsize 底部图标的大小
fixedColor 选中的颜色
type:BottomNavigationBarType.fixed 如果菜单超过三个则需要加上这个
void main(){
runApp(
const MaterialApp(
title: "richu",
home: HomeState()
));
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState>{
int currentId = 0;
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(onTap: (index){
setState(() {
currentId = index;
});
},currentIndex: currentId,items: const[
BottomNavigationBarItem(icon: Icon(Icons.home),label: "首页"),
BottomNavigationBarItem(icon: Icon(Icons.settings),label: "设置"),
BottomNavigationBarItem(icon: Icon(Icons.supervised_user_circle),label: "用户")]),
appBar: AppBar(title:const Text("richu")),
body: const Text("data")
);
}
}
5.2 实现页面切换
import 'package:flutter/material.dart';
import './tabs/user.dart';
import './tabs/home.dart';
import './tabs/setting.dart';
void main(){
runApp(
const MaterialApp(
title: "richu",
home: HomeState()
));
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState>{
int currentId = 0;
final List<Widget> _list = const [
MyHomePage(),
SettingPage(),
UserPage(),
];
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(onTap: (index){
setState(() {
currentId = index;
});
},currentIndex: currentId,items: const[
BottomNavigationBarItem(icon: Icon(Icons.home),label: "首页"),
BottomNavigationBarItem(icon: Icon(Icons.settings),label: "设置"),
BottomNavigationBarItem(icon: Icon(Icons.supervised_user_circle),label: "用户")]),
appBar: AppBar(title:const Icon(Icons.arrow_back)),
body: _list[currentId]
);
}
}
5.3 FloatingActionButton 实现底部突起按钮
shape:const CircleBorder()
void main(){
runApp(
const MaterialApp(
title: "richu",
home: HomeState()
));
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState>{
int currentId = 0;
final List<Widget> _list = const [
MyHomePage(),
SettingPage(),
UserPage(),
];
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(onTap: (index){
setState(() {
currentId = index;
});
},currentIndex: currentId,fixedColor: Colors.blue,type: BottomNavigationBarType.fixed,items: const[
BottomNavigationBarItem(icon: Icon(Icons.home),label: "首页"),
BottomNavigationBarItem(icon: Icon(Icons.settings),label: "设置"),
BottomNavigationBarItem(icon: Icon(Icons.supervised_user_circle),label: "用户"),
//BottomNavigationBarItem(icon: Icon(Icons.supervised_user_circle),label: "用户")
]),
appBar: AppBar(title:const Icon(Icons.arrow_back)),
floatingActionButton: Container(
height: 60,
width: 60,
decoration:BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.circular(30)
),
child: FloatingActionButton(shape:const CircleBorder(),onPressed: (){
setState(() {
currentId = 1;
});
},child: const Icon(Icons.add),),),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: _list[currentId]
);
}
}
5.4 Drawer 侧边栏
void main(){
runApp(
const MaterialApp(
title: "richu",
home: HomeState()
));
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState>{
int currentId = 0;
final List<Widget> _list = const [
MyHomePage(),
SettingPage(),
UserPage(),
];
Widget build(BuildContext context) {
return Scaffold(
drawer: const Drawer(
child: Column(
children: [
DrawerHeader(child: ListTile(leading: Icon(Icons.home),title: Text("信息"),
)),
Divider(),
ListTile(
leading: CircleAvatar(child:Icon(Icons.supervised_user_circle)),
title: Text("用户中心"),
),Divider(),
ListTile(
leading: CircleAvatar(child:Icon(Icons.delete)),
title: Text("删除"),
),Divider()
],
),
),
bottomNavigationBar: BottomNavigationBar(onTap: (index){
setState(() {
currentId = index;
});
},currentIndex: currentId,fixedColor: Colors.blue,type: BottomNavigationBarType.fixed,items: const[
BottomNavigationBarItem(icon: Icon(Icons.home),label: "首页"),
BottomNavigationBarItem(icon: Icon(Icons.settings),label: "设置"),
BottomNavigationBarItem(icon: Icon(Icons.supervised_user_circle),label: "用户"),
]),
appBar: AppBar(title:const Icon(Icons.arrow_back)),
body: _list[currentId]
);
}
}
5.5 UserAccountDrawerHeader
class _HomeState extends State<HomeState>{
int currentId = 0;
final List<Widget> _list = const [
MyHomePage(),
SettingPage(),
UserPage(),
];
Widget build(BuildContext context) {
return Scaffold(
drawer: const Drawer(
child: Column(
children: [
/*DrawerHeader(child: ListTile(leading: Icon(Icons.home),title: Text("信息"),
)),*/
UserAccountsDrawerHeader(accountName: Text("日初"), accountEmail: Text("xxx@163.com"),currentAccountPicture:CircleAvatar(backgroundImage: AssetImage("images/a.jpg"))),
Divider(),
ListTile(
leading: CircleAvatar(child:Icon(Icons.supervised_user_circle)),
title: Text("用户中心"),
),Divider(),
ListTile(
leading: CircleAvatar(child:Icon(Icons.delete)),
title: Text("删除"),
),Divider()
],
),
),
bottomNavigationBar: BottomNavigationBar(onTap: (index){
setState(() {
currentId = index;
});
},currentIndex: currentId,fixedColor: Colors.blue,type: BottomNavigationBarType.fixed,items: const[
BottomNavigationBarItem(icon: Icon(Icons.home),label: "首页"),
BottomNavigationBarItem(icon: Icon(Icons.settings),label: "设置"),
BottomNavigationBarItem(icon: Icon(Icons.supervised_user_circle),label: "用户"),
]),
appBar: AppBar(title:const Icon(Icons.arrow_back)),
body: _list[currentId]
);
}
}
7. 导航栏与标签栏
6.1 AppBar
debugShowCheckedModeBanner: false 关闭 debug 图标
void main(){
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
title: "richu",
home: HomeState()
));
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState>{
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(onPressed: (){}, icon: const Icon(Icons.menu)), //左侧的按钮图标,
actions: [
IconButton(onPressed: (){}, icon: const Icon(Icons.add)) //右侧图标
],
),
body:const Text("data")
);
}
}
6.2 TabBar
tabbar 和 tabbarView 都要配置 Controller
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin{
late TabController _tabController;
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(onPressed: (){}, icon: const Icon(Icons.menu)), //左侧的按钮图标,
actions: [
IconButton(onPressed: (){}, icon: const Icon(Icons.add)) //右侧图标
],
bottom: TabBar(controller:_tabController ,tabs:const [
Tab(child: Text("推荐")),Tab(child: Text("热门")),Tab(child: Text("视频"),)
]),
),
body:TabBarView(controller: _tabController ,children:const [ListTile(
title: Text("我的推荐"),
),
ListTile(
title: Text("我的热门"),
),
ListTile(
title: Text("我的视频"),
)
])
);
}
}
6.3 AppBar 属性
| 字段 | 属性 | 描述 |
|---|---|---|
| key | Key | 当组件在组件树中移动时使用 Key 可以保持组件之前状态 |
| leading | Widget | 通常情况下返回一个返回键(IconButton) |
| leadingWidth | double | 左侧 leading 的宽度,默认 56 |
| automaticallyImplyLeading | bool | 和 leading 配合使用,如果为 true 并且 leading 为空的情况下,会自动配置返回键 |
| title | Widget | 导航栏的标题 |
| centerTitle | bool | 标题是否居中,不同操作系统默认显示位置不一样 |
| actions | List | 一个 Widget 列表 |
| bottom | PreferredSizeWidget | 出现在导航栏底部的控件 |
| elevation | double | 控制导航栏下方阴影的大小 |
| shadowColor | Color | 控制导航栏下方阴影的颜色 |
| shape | ShapeBorder | 导航栏的形状以及阴影 |
| backgroundColor | Color | 导航栏的背景颜色 |
| foregroundColor | Color | 导航栏中文本和图标的颜色 |
| backwardsCompatibility | bool | 与 foregroundColor 配合使用 |
| iconTheme | IconThemeData | 导航栏图标的颜色、透明度、大小的配置 |
| actionsIconTheme | IconThemeData | 导航栏右侧图标的颜色、透明度、大小的配置 |
| textTheme | TextTheme | 导航栏文本的排版样式 |
| primary | bool | 导航栏是否显示在屏幕顶部 |
| excludeHeaderSemantics | bool | 标题是否应该用 [Semantics] 包裹,默认 false |
| titleSpacing | double | title 内容的间距 |
| toolbarOpacity | double | 导航栏的透明度 |
| toolbarHeight | double | 导航栏的高度,默认 kToolbarHeight |
| toolbarTextStyle | TextStyle | 导航栏图标的颜色 |
| titleTextStyle | TextStyle | 导航栏标题的默认颜色 |
| flexibleSpace | Widget | 堆叠在工具栏和选项卡栏的后面 |
| systemOverlayStyle | SystemUiOverlayStyle | 叠加层的样式 |
| brightness | Brightness | 导航栏的亮度,改属性已废弃,用 systemOverlayStyle 代替 |
| bottomOpacity | double | 导航栏底部的透明度 |
6.4 PreferredSize 配置 appBar 的高度
appBar: PreferredSize(preferredSize: const Size.fromHeight(50), child: TabBar(
controller:_tabController ,
indicatorColor: Colors.red,
labelColor: Colors.black87,
tabs:const [
Tab(child: Text("推荐")),Tab(child: Text("热门")),Tab(child: Text("视频"),)
]),),
6.5 自定义缓存组件
class KeepAliveWrapper extends StatefulWidget{
final Widget? child;
final bool keepAlive;
const KeepAliveWrapper({Key? key,required this.child,this.keepAlive=true}):super(key: key);
State<KeepAliveWrapper> createState() => _KeepAliveWrapper();
}
class _KeepAliveWrapper extends State<KeepAliveWrapper> with AutomaticKeepAliveClientMixin{
Widget build(BuildContext context) {
return widget.child!;
}
bool get wantKeepAlive => widget.keepAlive;
}
实现页面的缓存
import 'package:flutter/material.dart';
class KeepAliveWrapper extends StatefulWidget{
final Widget? child;
final bool keepAlive;
const KeepAliveWrapper({Key? key,required this.child,this.keepAlive=true}):super(key: key);
State<KeepAliveWrapper> createState() => _KeepAliveWrapper();
}
class _KeepAliveWrapper extends State<KeepAliveWrapper> with AutomaticKeepAliveClientMixin{
Widget build(BuildContext context) {
return widget.child!;
}
bool get wantKeepAlive => widget.keepAlive;
}
void main(){
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
title: "richu",
home: HomeState()
));
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=> _HomeState();
}
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin{
late TabController _tabController;
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(preferredSize: const Size.fromHeight(50), child: TabBar(
controller:_tabController ,
indicatorColor: Colors.red,
labelColor: Colors.black87,
tabs:const [
Tab(child: Text("推荐")),Tab(child: Text("热门")),Tab(child: Text("视频"),)
]),),
backgroundColor: Colors.white,
body:TabBarView(controller: _tabController ,children:[KeepAliveWrapper(child: ListView(children: const [ListTile(
title: Text("我的推荐"),
)],),),
const ListTile(
title: Text("我的热门"),
),
const ListTile(
title: Text("我的视频"),
)
])
);
}
}
6.6 tabController 实现事件监听
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin{
late TabController _tabController;
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
_tabController.addListener((){
print(_tabController.index); //触发两次
});
}
解决触发两次的问题
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin{
late TabController _tabController;
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
_tabController.addListener((){
if (_tabController.index == _tabController.animation!.value){
print(_tabController.index); //触发一次
}
});
}
8. 路由导航
7.1 Navigator.of(context).push()
可以通过 Navigator.push() 跳转到另一个页面
Navigator.pop() 返回
class UserPage extends StatefulWidget{
const UserPage({super.key});
State<UserPage> createState() => _UserPage();
}
class _UserPage extends State<UserPage>{
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
ElevatedButton(onPressed: (){
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext builde){
return const MyHomePage();
})
);
}, child: const Text("搜索"))
],
)
);
}
}
7.2 基本路由
可以使用 widget.属性获取父类的属性
class SettingPage extends StatefulWidget{
final String title;
const SettingPage({super.key,this.title = ""});
State<SettingPage> createState() => _SettingPage();
}
class _SettingPage extends State<SettingPage>{
Widget build(BuildContext context) {
return Center(
child: Text(widget.title),
);
}
}
此时传递参数,在构造函数传递 title 即可
ElevatedButton(onPressed: (){
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext builde){
return const SettingPage(title: "我是设置");
})
);
}, child: const Text("设置"))],
7.3 命名路由
ElevatedButton(onPressed: (){
Navigator.pushNamed(context, "/setting");
}, child: const Text("设置"))
],
7.4 命名路由传值
7.4.1 配置 Map routes
Map routes = {
"/" : (context) => const MyHomePage(),
"/news" : (context) => const NewsPage(),
"/setting" : (context,arguments) => SettingPage(arguments: arguments),
};
7.4.2 配置 onGenerateRoute
//固定写法
onGenerateRoute: (RouteSettings settings){
final String? name = settings.name;
final Function? builder = routes[name];
if (builder != null){
if (settings.arguments != null){
final Route route = MaterialPageRoute(
builder: (context) => builder(context,arguments:settings.arguments)
);
return route;
}else{
final Route route = MaterialPageRoute(
builder: (context) => builder(context)
);
return route;
}
}
7.4.3 传递参数 arguments
ElevatedButton(onPressed: (){
Navigator.pushNamed(context, "/news",arguments: {
"title":"我是新闻",
"aid":20
});
}, child: const Text("新闻"))
7.4.4 flutter 返回上一级路由
Navigator.of(context).pop();
7.4.5 替换路由
return ListView(
children: [Text(widget.arguments["title"]),ElevatedButton(onPressed: (){
Navigator.of(context).pushReplacementNamed("/");
}, child: const Text("注册"))],
);
7.4.6 返回根目录
Navigator.of(context).pushAndRemoveUntil(newRoute, predicate);
7.4.7 使用 IOS 风格的路由跳转
import 'package:flutter/cupertino.dart';
onGenerateRoute: (RouteSettings settings){
final String? name = settings.name;
final Function? builder = routes[name];
if (builder != null){
if (settings.arguments != null){
final Route route = CupertinoPageRoute(
builder: (context) => builder(context,arguments:settings.arguments)
);
return route;
}else{
final Route route = CupertinoPageRoute(
builder: (context) => builder(context)
);
return route;
}
}
return null;
},
9. 对话框
8.1 AlertDialog
class NewsPage extends StatefulWidget{
final Map arguments;
const NewsPage({super.key,required this.arguments});
State<NewsPage> createState() => _NewsPage();
}
class _NewsPage extends State<NewsPage>{
Widget build(BuildContext context) {
void _alertDialog(){
showDialog(context: context, builder: (context){
return AlertDialog(
title: const Text("提示信息"),
content: const Text("确定?"),
actions: [
TextButton(onPressed: (){
Navigator.of(context).pop("ok");
}, child: const Text("确定")),
TextButton(onPressed: (){
Navigator.of(context).pop("none");
}, child: const Text("取消"))
],
);
});
}
return ListView(
children: [Text(widget.arguments["title"]),ElevatedButton(onPressed: (){
Navigator.of(context).pushReplacementNamed("/");
//Navigator.of(context).pushAndRemoveUntil(newRoute, predicate);
}, child: const Text("注册")),ElevatedButton(onPressed: (){
_alertDialog();
}, child: const Text("取消"))],
);
}
}
8.2 异步获取消息
Widget build(BuildContext context) {
void _alertDialog() async {
var res = await showDialog(context: context, builder: (context){
return AlertDialog(
title: const Text("提示信息"),
content: const Text("确定?"),
actions: [
TextButton(onPressed: (){
Navigator.of(context).pop("ok");
}, child: const Text("确定")),
TextButton(onPressed: (){
Navigator.of(context).pop("none");
}, child: const Text("取消"))
],
);
});
print(res);
}
8.3 SimpleDialog
void _simpleDialog()async {
var res = await showDialog(context: context, builder: (context){
return SimpleDialog(
title: const Text("提示信息"),
children: [
SimpleDialogOption(
onPressed: (){
Navigator.pop(context,"1");
},
child: const Text("1"),
),
SimpleDialogOption(
onPressed: (){
Navigator.pop(context,"2");
},
child: const Text("2"),
),
SimpleDialogOption(
onPressed: (){
Navigator.pop(context,"3");
},
child: const Text("3"),
),
],
);
});
print(res);
}
8.4 showModalBottomSheet
void _modelDialog()async {
var res = await showModalBottomSheet(context: context, builder: (context){
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ListTile(
title: const Text("test1"),
onTap: (){
Navigator.pop(context,"test1");
},
),
ListTile(
title: const Text("test2"),
onTap: (){
Navigator.pop(context,"test2");
},
),
ListTile(
title: const Text("test3"),
onTap: (){
Navigator.pop(context,"test3");
},
)
],
);
});
print(res);
}
8.5 showToast
添加配置
dependencies:
flutter:
sdk: flutter
fluttertoast: ^8.0.9
Fluttertoast.showToast(
msg: "This is Center Short Toast", //信息
toastLength: Toast.LENGTH_SHORT, //安卓平台生效
gravity: ToastGravity.CENTER, //提示的位置
timeInSecForIosWeb: 1, //提示时间web ios
backgroundColor: Colors.red, //背景颜色
textColor: Colors.white, //文本颜色
fontSize: 16.0 //字体大小
);
8.6 自定义 dialog
自定义 dialog,并且点击按钮关闭
class Mydialog extends Dialog{
const Mydialog({super.key});
Widget build(BuildContext context){
return Material(
child: Center(
child: Container(
height: 300,
width: 300,
color: Colors.white,
child: Column(
children: [
const SizedBox(height: 20,),
Stack(
children: [
const Align(alignment: Alignment.centerLeft,child: Text("提示"),),
Align(alignment: Alignment.centerRight,child: InkWell(child:const Icon(Icons.star_border_purple500),onTap: (){
//点击按钮关闭
Navigator.pop(context);
},))
],
),
const Divider(),
const SizedBox(height: 10,),
Container(
padding: const EdgeInsets.all(10),
width: double.infinity, child: const Text("提示信息",style: TextStyle(
fontSize: 40
),),
)
],
),
),
)
);
}
}
传递方法和参数
使用 dialog
void _mydialog() {
showDialog(context: context, builder:(context){
return Mydialog(title:"提示",content:"我是提示",onTab: (){
//点击按钮关闭
Navigator.pop(context);
print("关闭");
},);
});
}
class Mydialog extends Dialog{
String title;
String content;
final Function()? onTab;
Mydialog({required this.onTab, required this.title,required this.content,super.key});
Widget build(BuildContext context){
return Material(
type: MaterialType.transparency,
child: Center(
child: Container(
height: 300,
width: 300,
color: Colors.white,
child: Column(
children: [
const SizedBox(height: 20,),
Stack(
children: [
Align(alignment: Alignment.centerLeft,child: Text(title),),
Align(alignment: Alignment.centerRight,child: InkWell(onTap: onTab!(), child:const Icon(Icons.star_border_purple500)))
],
),
const Divider(),
const SizedBox(height: 10,),
Container(
padding: const EdgeInsets.all(10),
width: double.infinity, child: Text(content,style: const TextStyle(
fontSize: 40
),),
)
],
),
),
)
);
}
}
10. 页面视图
9.1 PageView
import 'package:flutter/material.dart';
class MyPageView extends StatefulWidget{
const MyPageView({super.key});
State<MyPageView> createState() => _MyPageView();
}
class _MyPageView extends State<MyPageView>{
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
children: [
Center(
child: Text("1",style: Theme.of(context).textTheme.headlineLarge),
),
Center(
child: Text("2",style: Theme.of(context).textTheme.headlineLarge),
),
Center(
child: Text("3",style: Theme.of(context).textTheme.headlineLarge),
),
Center(
child: Text("4",style: Theme.of(context).textTheme.headlineLarge),
),
],
),
);
}
}
9.2 PageView.Builder
return PageView.builder(
itemCount: 10,
itemBuilder: (context,index){
return Center(
child: Text("data${index+1}",style: Theme.of(context).textTheme.headlineLarge)
);
}
);
allowImplicitScrolling 缓存当前页面的前后两页
onpageChanged(index){ //页面改变时触发
}
9.3 轮播图
创建定时器
//创建定时器
Timer timer = Timer.periodic(const Duration(seconds: 3),(timer){
print("执行");
});
class MyPageView extends StatefulWidget{
const MyPageView({super.key});
State<MyPageView> createState() => _MyPageView();
}
class _MyPageView extends State<MyPageView>{
var _pageController;
late Timer timer;
var _currentIndex;
void initState() {
super.initState();
//pageController
_pageController =PageController(initialPage: 0);
//创建定时器
timer = Timer.periodic(const Duration(seconds: 3),(timer){
_pageController.animateToPage((_currentIndex+1)%10, uration: const Duration(milliseconds: 400), curve: Curves.linear,);
});
}
Widget build(BuildContext context) {
return PageView.builder(
controller: _pageController,
itemCount: 10,
itemBuilder: (context,index){
return Center(
child: Text("data${index+1}",style: Theme.of(context).textTheme.headlineLarge)
);
}
);
}
void dispose() {
super.dispose();
timer.cancel();
_pageController.dispose();
}
}
9.4 AutomaticKeepAliveClientMixin
class _MyPageView extends State<MyPageView> with AutomaticKeepAliveClientMixin{
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
11. Key 的作用
10.1 key 的使用
void main(){
runApp(const HomeState());
}
class HomeState extends StatefulWidget{
const HomeState({super.key});
State<HomeState> createState()=>_HomeState();
}
class _HomeState extends State<HomeState>{
List<Widget> list = [
const Boxed(color: Colors.red),
const Boxed(color: Colors.yellow),
const Boxed(color: Colors.blue,)
];
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(child: const Icon(Icons.refresh),onPressed: (){
setState(() {
list.shuffle();
});
}),
appBar: AppBar(
title: const Text("标题"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: list
),
),
)
);
}
}
class Boxed extends StatefulWidget{
final Color color;
const Boxed({super.key,required this.color});
State<Boxed> createState() => _Boxed();
}
class _Boxed extends State<Boxed>{
int _cout = 0;
Widget build(BuildContext context) {
return SizedBox(
height: 100,
width: 100,
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all(widget.color)
),
onPressed: (){
setState(() {
_cout++;
});
},
child: Text("$_cout",style: Theme.of(context).textTheme.headlineLarge,)),
);
}
}
当点击按钮改变顺序时数字没有跟着一起变
LocalKey 局部键
Globalkey 全局键
List<Widget> list = [
const Boxed(key: ValueKey("1"),color: Colors.red),
const Boxed(key: ValueKey("2"),color: Colors.yellow),
const Boxed(key: ValueKey("3"),color: Colors.blue,)
];
获取屏幕方向
print(MediaQuery.of(context).orientation);
10.2 父 widget 获取子 widget 的状态和方法
给子 widget 传递 globalkey 使用 globalkey 获取子组件
class HomeState extends StatefulWidget{
const HomeState({Key? key}) : super(key:key);
State<HomeState> createState()=>_HomeState();
}
class _HomeState extends State<HomeState>{
final GlobalKey _globalKey = new GlobalKey();
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
var BoxState = _globalKey.currentState as _Boxed;
print(BoxState._cout);
}),
appBar: AppBar(
title: const Text("标题"),
),
body: Boxed(key: _globalKey,color: Colors.red)
)
);
}
}
12. AnimatedList 动态列表
class _HomeState extends State<HomeState>{
List<String> list = ["第一","第二"];
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
setState(() {
list.add("value");
});
},child: const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: AnimatedList(initialItemCount: list.length,itemBuilder: ((context,index,animation){
return FadeTransition(opacity: animation,
child: ListTile(title: Text(list[index]),),
);
}))
)
);
}
}
增加数据要使用 insertItem
class _HomeState extends State<HomeState>{
final _globalKey = GlobalKey<AnimatedListState>();
List<String> list = ["第一","第二"];
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
setState(() {
list.add("value");
_globalKey.currentState!.insertItem(list.length-1);
});
},child: const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: AnimatedList(
key: _globalKey,
initialItemCount: list.length,itemBuilder: ((context,index,animation){
return FadeTransition(opacity: animation,
child: ListTile(title: Text(list[index]),),
);
}))
)
);
}
}
删除数据使用 removeItem
_globalKey.currentState!.removeItem(list.length-1,(context,animation){
return FadeTransition(opacity: animation,child: widget,);
});
13. 动画组件
12.1 隐式动画
在动画组件内,直接配置 curve 和 duration 属性
12.1.1 AnimatedContainer
使用 AnimatedContainer 组件,配置 curve 曲线过渡和 duration 过渡时间
class HomeState extends StatefulWidget{
const HomeState({Key? key}) : super(key:key);
State<HomeState> createState()=>_HomeState();
}
class _HomeState extends State<HomeState>{
bool press = false; //设置动画触发的条件
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
setState(() {
press = true; //点击FloatingActionButton进行动画效果
});
}
,child: const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: AnimatedContainer(
curve: Curves.ease, //曲线
duration: const Duration(seconds: 1), //延时
width: press ? 200 : 300,
height: 200,
color:Colors.yellow,
transform: press ? Matrix4.translationValues(0, 0, 0) :
Matrix4.translationValues(100, 100, 0)
),
)
)
);
}
}
12.1.2 AnimatedPadding
通过配置 padding 值的改变,引起组件的移动动画效果,同样支持 curve 和 duration 的配置
class _HomeState extends State<HomeState>{
bool press = false;
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
setState(() {
press = true;
});
}
,child: const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: AnimatedPadding(
padding: EdgeInsets.fromLTRB(10, press ? 10 : 400, 0, 0), //配置边距值
curve: Curves.ease, //曲线
duration: const Duration(seconds: 1), //延时
child: Container(
width: 200,
height: 200,
color:Colors.yellow,
),
),
)
)
);
}
}
12.1.3 AnimatedAlign
通过配置 alignment 值的改变,引起组件的对齐动画效果,同样支持 curve 和 duration 的配置
class _HomeState extends State<HomeState>{
bool press = false;
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
setState(() {
press = true;
});
}
,child: const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: AnimatedAlign(
alignment: press ? Alignment.center : Alignment.topCenter,
curve: Curves.ease, //曲线
duration: const Duration(seconds: 1), //延时
child: Container(
width: 200,
height: 200,
color:Colors.yellow,
),
),
)
)
);
}
}
12.1.4 AnimatedOpacity
通过配置 opacity 值的改变,引起组件的透明度变化动画效果,同样支持 curve 和 duration 的配置
class _HomeState extends State<HomeState>{
bool press = false;
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
setState(() {
press = true;
});
}
,child: const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: AnimatedOpacity(
opacity: press ? 1 : 0.1,
curve: Curves.ease, //曲线
duration: const Duration(seconds: 1), //延时
child: Container(
width: 200,
height: 200,
color:Colors.yellow,
),
),
)
)
);
}
}
12.1.5 AnimatedPositioned
通过配置 top,left,right,bottom 值的改变,引起组件的距离变化动画效果,同样支持 curve 和 duration 的配置
class _HomeState extends State<HomeState>{
bool press = false;
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
setState(() {
press = true;
});
}
,child: const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body:Stack(
children: [
AnimatedPositioned(
top: press ? 0 : 100,
left:press ? 0 : 100,
curve: Curves.ease, //曲线
duration: const Duration(seconds: 1), //延时
child: Container(
width: 200,
height: 200,
color:Colors.yellow,
),
),
],
)
)
);
}
}
12.2 显式动画
使用显示动画,定义 AnimationController,在组件上 with SingleTickerProviderStateMixin
12.2.1 RotationTransition
RotationTransition 实现旋转动画,turns 为 AnimationController,可以在 initState 初始化的时候设置 vsync,duration 来让程序和手机的刷新频率统一,使用 .. 联级操作调用 repeat 函数让动画重复运动
class _Boxed extends State<Boxed> with SingleTickerProviderStateMixin{
late AnimationController _controller;
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1)
)..repeat(); //让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return SizedBox(
height: 100,
width: 100,
child: RotationTransition(turns: _controller,
child: const FlutterLogo(size: 60),
)
);
}
}
12.2.2 AnimationController
controller 的播放方式
repeat 重复
forward 播放一次
reverse 倒序播放
stop 停止
reset 重置
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin {
late AnimationController _controller;
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: const Duration(seconds: 1));//让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
_controller.repeat(); //重复播放
},child:const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: Column(
children: [RotationTransition(
turns: _controller,
child: const FlutterLogo(size: 60),
),
ElevatedButton(onPressed: (){
_controller.forward(); //播放一次
}, child:const Icon(Icons.refresh)),
ElevatedButton(onPressed: (){
_controller.reverse(); //倒序播放
}, child:const Icon(Icons.refresh)),
ElevatedButton(onPressed: (){
_controller.stop(); //停止
}, child:const Icon(Icons.refresh)),
ElevatedButton(onPressed: (){
_controller.reset(); //重置
}, child:const Icon(Icons.refresh)),
]
)
)
)
);
}
}
12.2.3 FadeTransition
RotationTransition 实现透明度变化动画,opacity 为 AnimationController,可以在 initState 初始化的时候设置 vsync,duration 来让程序和手机的刷新频率统一,同时 controller 为 0-1 的值,因此 opacity 会发生从透明到实体的过程,如果要实现实体逐渐到透明可以使用 reverse() 倒序播放
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin {
late AnimationController _controller;
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: const Duration(seconds: 1));//让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
_controller.repeat(); //重复播放
},child:const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: FadeTransition(opacity: _controller,
child: const FlutterLogo(size: 60,),
)
)
)
);
}
}
也可以通过 lowerBound 和 upperBound 来配置 controller 的最低和最高值
12.2.4 ScaleTransition
ScaleTransition 实现放大缩小动画,scale 为 AnimationController,可以在 initState 初始化的时候设置 vsync,duration 来让程序和手机的刷新频率统一,同时 controller 为 0-1 的值,因此 scale 会发生从小到大的过程,如果要实现大逐渐到小可以使用 reverse() 倒序播放
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin {
late AnimationController _controller;
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: const Duration(seconds: 1));//让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
_controller.repeat(); //重复播放
},child:const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: ScaleTransition(scale: _controller,
child: const FlutterLogo(size: 60,),
)
)
)
);
}
}
_controller.drive(Tween(begin:0.5,end: 1.5)), 即为开始时30 结束后为90 (初始为60)
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin {
late AnimationController _controller;
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: const Duration(seconds: 1),
lowerBound: 0.5,
upperBound: 1
);//让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
_controller.repeat(); //重复播放
},child:const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: ScaleTransition(scale: _controller.drive(Tween(begin:0.5,end: 1.5)),
child: const FlutterLogo(size: 60,),
)
)
)
);
}
}
12.2.5 SlideTransition
SlideTransition 实现位移变化动画,position 为 AnimationController,可以在 initState 初始化的时候设置 vsync,duration 来让程序和手机的刷新频率统一,使用 drive 函数内部配置 Tween() 子组件 Offset() 设置初始位置和结束位置
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin {
late AnimationController _controller;
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: const Duration(seconds: 1),
lowerBound: 0.5,
upperBound: 1
);//让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton:FloatingActionButton(onPressed: (){
_controller.repeat(); //重复播放
},child:const Icon(Icons.add),) ,
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: SlideTransition(position: _controller.drive(Tween(begin: const Offset(0, 0),end: const Offset(1, 1))),
child: const FlutterLogo(size: 60,),
)
)
)
);
}
}
配置曲线
chain 函数内部使用 CurveTween 组件,同时可以继续使用链式操作继续配置动画
child: SlideTransition(position: Tween(begin: const Offset(0, 0),end: const Offset(0,2)).chain(CurveTween(curve: Curves.linear)).animate(_controller),
12.3 交错式动画
通过配置 controller.drive() 方法来配置交错动画
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin {
late AnimationController _controller;
bool flag = false;
void initState() {
super.initState();
_controller = AnimationController(
vsync: this, duration: const Duration(seconds: 1, milliseconds: 1),
// lowerBound: 0.5,
// upperBound: 1
); //让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
flag? _controller.forward() : _controller.reverse();
flag = !flag;
},
child: const Icon(Icons.add),
),
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: Stack(
children: [
//实现交错式动画
ScaleTransition(
scale: _controller.drive(Tween(begin: 0.0, end: 1.0)
.chain(CurveTween(curve: const Interval(0.5, 1)))),
child: const Icon(Icons.search)),
ScaleTransition(
scale: _controller.drive(Tween(begin: 1.0, end: 0.0)
.chain(CurveTween(curve: const Interval(0, 0.5)))),
child: const Icon(Icons.close),
)
],
))));
}
}
交错动画实现方块位移
class _HomeState extends State<HomeState> with SingleTickerProviderStateMixin {
late AnimationController _controller;
bool flag = false;
void initState() {
super.initState();
_controller = AnimationController(
vsync: this, duration: const Duration(seconds: 1, milliseconds: 1),
// lowerBound: 0.5,
// upperBound: 1
); //让程序和手机的刷新频率统一
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
flag? _controller.forward() : _controller.reverse();
flag = !flag;
},
child: const Icon(Icons.add),
),
appBar: AppBar(
title: const Text("测试"),
),
body: Center(
child: Column(
children: [
//实现交错式动画
SlideTransition(position: _controller.drive(Tween(begin: const Offset(0.0, 0.0),end: const Offset(1.5, 0)).chain(CurveTween(curve: Curves.ease)).chain(CurveTween(curve: const Interval(0, 0.2)))),
child: Container(
width: 100,
height: 100,
color: Colors.blue[100],
),
),
SlideTransition(position: _controller.drive(Tween(begin: const Offset(0.0, 0.0),end: const Offset(1.5, 0)).chain(CurveTween(curve: Curves.ease)).chain(CurveTween(curve: const Interval(0.2, 0.4)))),
child: Container(
width: 100,
height: 100,
color: Colors.blue[300],
),
),
SlideTransition(position: _controller.drive(Tween(begin: const Offset(0.0, 0.0),end: const Offset(1.5, 0)).chain(CurveTween(curve: Curves.ease)).chain(CurveTween(curve: const Interval(0.4, 0.6)))),
child: Container(
width: 100,
height: 100,
color: Colors.blue[600],
),
),
SlideTransition(position: _controller.drive(Tween(begin: const Offset(0.0, 0.0),end: const Offset(1.5, 0)).chain(CurveTween(curve: Curves.ease)).chain(CurveTween(curve: const Interval(0.6, 0.8)))),
child: Container(
width: 100,
height: 100,
color: Colors.blue[900],
),
),
],
),
)
));
}
}
12.4 Hero 动画
在路由组件内配置
class _HomeState extends State<HomeState>{
Map routes = {
"/" : (context) => const MyHomePage(),
"/news" : (context, {arguments}) => NewsPage(arguments: arguments),
"/setting" : (context, {arguments}) => SettingPage(arguments: arguments),
"/page" :(context) => const MyPageView()
};
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
onGenerateRoute: (RouteSettings settings){
final String? name = settings.name;
final Function? builder = routes[name];
if (builder != null){
if (settings.arguments != null){
final Route route = CupertinoPageRoute(
builder: (context) => builder(context,arguments:settings.arguments)
);
return route;
}else{
final Route route = CupertinoPageRoute(
builder: (context) => builder(context)
);
return route;
}
}
return null;
},
title: "richu",
initialRoute: "/",
home: RouteState(),
);
}
}
class RouteState extends StatelessWidget{
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView(
children: [
ElevatedButton(onPressed: (){
Navigator.pushNamed(context, "/news",arguments: {
"title":"组件news",
"aid":20
});
}, child: const Hero(tag: "组件news", child:Text("text"))), //通过tag传递参数,类似android的子组件共享参数
],
)
),
);
}
}
在子路由组件中使用 hero 组件接收参数
class _NewsPage extends State<NewsPage>{
return ListView(
children: [Hero(tag:widget.arguments["title"], child:Text(widget.arguments["title"])),ElevatedButton(onPressed: (){
Navigator.of(context).pushReplacementNamed("/");
//Navigator.of(context).pushAndRemoveUntil(newRoute, predicate);
}, child: const Text("注册")),const SizedBox(height: 20,),ElevatedButton(onPressed: (){
_alertDialog();
}, child: const Text("取消")),const SizedBox(height: 20,),ElevatedButton(onPressed: (){
_simpleDialog();
}, child: const Text("提示信息")),const SizedBox(height: 20,),ElevatedButton(onPressed: (){
_modelDialog();
}, child: const Text("显示")),const SizedBox(height: 20,),ElevatedButton(onPressed: (){
_mydialog();
}, child: const Text("提示"))],
);
}
}
14. GetX 状态管理与路由
13.1 基本使用
13.1.1 状态管理
GetX 高性能的状态管理,智能的依赖注入和便捷的路由管理
get: ^4.6.1
flutter pub get
Get.defaultDialog(
title: "提示信息",
middleText: "确定删除?",
confirm: ElevatedButton(onPressed: (){
print("ok");
}, child: const Text("确定")),
cancel: ElevatedButton(onPressed: (){
print("test");
}, child: const Text("确定"))
);
返回上一级
Get.back();
13.1.2 组件的使用
初始化
class HomeState extends StatelessWidget{
const HomeState({super.key});
Widget build(BuildContext context) {
return GetMaterialApp(
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.light,
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("data"),
),
body: const PageGet(),
),
);
}
}
defaultDialog
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () {
Get.defaultDialog(
title: "提示信息",
middleText: "确定删除?",
confirm: ElevatedButton(onPressed: (){
Get.back();
}, child: const Text("确定")),
cancel: ElevatedButton(onPressed: (){
Get.back();
}, child: const Text("取消"))
);
},
child: const Text("Show Dialog"),
),
);
}
snackbar 默认在顶部
Get.snackbar("警告", "未知错误");
//配置位置
Get.snackbar("警告", "未知错误",snackPosition: SnackPosition.BOTTOM);
bottomSheet
Get.bottomSheet(Container(
color: Colors.white,
height: 200,
child: Column(
children: [
ElevatedButton(onPressed: (){}, child: const ListTile(
leading: Icon(Icons.sunny),
title: Text("白天模式",style: TextStyle(color: Colors.black),),
),),
ElevatedButton(onPressed: (){}, child:const ListTile(
leading: Icon(Icons.nightlight),
title: Text("黑夜模式" ,style: TextStyle(color: Colors.black),),
))
],
),
));
13.1.3 通过 getx 切换主题
Get.bottomSheet(Container(
color: Colors.white,
height: 200,
child: Column(
children: [
ListTile(
leading: Icon(Icons.sunny),
onTap: (){
Get.changeTheme(ThemeData.light());
Get.back();
},
title: const Text("白天模式",style: TextStyle(color: Colors.black),),
),
ListTile(
leading: Icon(Icons.nightlight),
onTap: (){
Get.changeTheme(ThemeData.dark());
Get.back();
},
title:const Text("黑夜模式" ,style: TextStyle(color: Colors.black),),
)
],
),
));
app
class HomeState extends StatelessWidget{
const HomeState({super.key});
Widget build(BuildContext context) {
return GetMaterialApp(
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.light,
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("data"),
),
body: const PageGet(),
),
);
}
}
13.1.4 GetX 进行路由切换
配置默认的路由切换动画
initialRoute: "/",
defaultTransition: Transition.rightToLeft,
Get.toNamed(“/login”) 去到指定路由
Get.offAll(const Tabs(index:4)) 去到根路径
Get.off(NextScreen()); 进入下一个页面但是没有返回按钮
Get.back() 返回
13.1.5 GetX 配置路由
配置路由,参数可在组件内获取
rightToLeft 来配置动画效果
class HomeState extends StatelessWidget {
const HomeState({super.key});
Widget build(BuildContext context) {
return GetMaterialApp(
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.light,
debugShowCheckedModeBanner: false,
initialRoute: "/",
getPages: [
GetPage(name: "/home", page: () => const MyHomePage()),
GetPage(
name: "/user",
page: () => const UserPage(),
transition: Transition.rightToLeft)
],
home: Scaffold(
appBar: AppBar(
title: const Text("data"),
),
body: const PageGet(),
),
);
}
}
传递参数
Get.toNamed("/news",arguments: {"title":"richu"});
接收参数
Get.arguments["title"], child: Text(Get.arguments["title"]
13.1.6 中间件
定义中间件
class ShopMiddleware extends GetMiddleware{
RouteSettings? redirect(String? route){
//return null 默认不做操作
return null;
//路由跳转,并且传递参数,如果没有参数可以传递空map
return const RouteSettings(name: "/",arguments: {});
}
}
配置中间件
GetPage(
name: "/user",
page: () => const UserPage(),
transition: Transition.rightToLeft,
middlewares: [
ShopMiddleware()
]
),
配置多个中间件,并且配置优先级
ShopMiddleware(priority: 2)
class ShopMiddleware extends GetMiddleware{
ShopMiddleware({super.priority});
RouteSettings? redirect(String? route){
//return null;
return const RouteSettings(name: "/",arguments: {});
}
}
13.2 状态管理
定义一个响应式变量
RxInt _counter = 0.obs;
RxString _name = "".obs;
监听
children: [Obx(()=>Text("$_counter.value"))],
Obx(()=>Text(_name.value))
定义响应式变量的方法
Rx<String> _test1 = Rx<String>("test");
RxString _test2 = RxString("test");
13.3 响应式状态管理
多个页面共享数据
//定义controller
import 'package:get/get.dart';
class CounterController extends GetxController{
RxInt counter = 0.obs;
inc(){
counter++;
update();
}
dec(){
counter--;
update();
}
}
class _PageGet extends State<PageGet> {
CounterController counterController = Get.put(CounterController());
Widget build(BuildContext context) {
return Column(
children: [
Obx(() => Text("${counterController.counter}")),
ElevatedButton(
onPressed: () {
counterController.inc();
},
child: const Text("++")),
ElevatedButton(
onPressed: () {
Get.toNamed("/news", arguments: {"title": "richu"});
},
child: const Text("to news"))
],
);
}
}
获取
CounterController counterController = Get.find();
ElevatedButton(onPressed: (){
counterController.inc();
}, child:const Text("++")),
Obx(()=>Text("${counterController.counter}"))
13.3.1 Binding
import 'package:get/get.dart';
import 'package:richu/controllers/counter.dart';
class AllControllerBinding extends Bindings{
void dependencies() {
Get.lazyPut<CounterController>(()=>CounterController());
}
}
return GetMaterialApp(
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.light,
debugShowCheckedModeBanner: false,
initialRoute: "/",
defaultTransition: Transition.rightToLeft,
initialBinding: AllControllerBinding(),
getPages: [
GetPage(
name: "/home",
page: () => const MyHomePage(),
transition: Transition.rightToLeft),
GetPage(
name: "/user",
page: () => const UserPage(),
transition: Transition.rightToLeft,
middlewares: [ShopMiddleware(priority: 2)]),
GetPage(
name: "/news",
page: () => const NewsPage(),
transition: Transition.rightToLeft)
],
home: Scaffold(
appBar: AppBar(
title: const Text("data"),
),
body: const PageGet(),
),
);
配置之后无序 put
13.4 使用 GetView
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:richu/controllers/counter.dart';
class CounterPage extends GetView<CounterController>{
const CounterPage({super.key});
Widget build(BuildContext context) {
Get.put(CounterController());
return Scaffold(
appBar: AppBar(
title: const Text("title"),
),
body: Center(
child: Column(
children: [
Obx(()=>Text("${controller.counter}")),
const SizedBox(height: 20,),
ElevatedButton(onPressed: (){
controller.inc();
}, child: const Text("++"))
],
),
),
);
}
}
在路由注入 controller
GetPage(name: "/counter", page: () => const CounterPage(),binding:Counterbinding())
初始化后可以直接使用,无需 put
13.5 controller 的生命周期
void onInit() {
print("init");
}
void onReady() {
print("ready");
}
void onClose() {
print("close");
}
13.6 Flutter 实现多语言配置
ElevatedButton(onPressed: () {}, child: const Text("切换到中文")),
const SizedBox(
height: 20,
),
ElevatedButton(onPressed: () {}, child: const Text("切换到英文"))
定义语言
import 'package:get/get.dart';
class Message extends Translations {
Map<String, Map<String, String>> get keys =>{
'zh_CN':{
'hello':"你好"
},
'en-US':{
'hello':"hello richu"
}
};
}
return GetMaterialApp(
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.light,
debugShowCheckedModeBanner: false,
initialRoute: "/",
translations: Message(),
locale: const Locale('zh','CN'), //指定默认的语言
fallbackLocale: const Locale('en','US'), //添加一个回调语言选项
defaultTransition: Transition.rightToLeft,)
切换语言
ElevatedButton(onPressed: () {
var locale = const Locale("zh",'CN');
Get.updateLocale(locale);
}, child: const Text("切换到中文")),
const SizedBox(
height: 20,
),
ElevatedButton(onPressed: () {
var locale = const Locale("en",'US');
Get.updateLocale(locale);
}, child: const Text("切换到英文")),
使用多语言
Text("hello".tr);
13.7 GetUtils 工具库
GetUtils是getx为我们提供一些常用的工具类库,包括值是否为空、是否是数字、是否是视频、图片、音频、PPT、Word、APK、邮箱、手机号码、日期、MD5、SHA1等等。
验证邮箱
class _PageGet extends State<PageGet> {
TextEditingController textEditingController = TextEditingController();
Widget build(BuildContext context) {
return Column(
children: [
TextField(
controller: textEditingController,
),
ElevatedButton(onPressed: (){
if (GetUtils.isEmail(textEditingController.text)){
Get.snackbar("正确", "验证成功");
}else{
Get.snackbar("错误", "错误的邮箱",colorText: Colors.red[100]);
}
}, child: const Text("验证邮箱"))
],
);
}
}
验证手机号
TextField(
controller: textEditingController,
),
ElevatedButton(onPressed: (){
if (GetUtils.isPhoneNumber(textEditingController.text) && textEditingController.text.length == 11){
Get.snackbar("正确", "验证成功");
}else{
Get.snackbar("错误", "错误的手机号",colorText: Colors.red[100]);
}
}, child: const Text("验证手机号")),
15. SQLite 数据库
14.1 添加依赖
为了使用 SQLite 数据库,首先需要导入 sqflite 和 path package。
sqflite 提供了丰富的类和方法,以便你能便捷实用 SQLite 数据库。
path 提供了大量方法,以便你能正确的定义数据库在磁盘上的存储位置。
运行 flutter pub add 将其添加为依赖:
flutter pub add sqflite path
14.2 打开数据库
在你准备读写数据库的数据之前,你要先打开这个数据库。打开一个数据库有以下两个步骤:
使用 sqflite package 里的 getDatabasesPath 方法并配合 path package 里的 join 方法定义数据库的路径。
使用 sqflite package 里的 openDatabase 方法打开数据库。
提示
//导入 'package:flutter/widgets.dart'
WidgetsFlutterBinding.ensureInitialized();
//打开数据库并保存其引用
final database = openDatabase(
join(await getDatabasesPath(), 'dog.db'),
);
14.3 创建 dogs 表
接下来,你需要创建一个表用以存储各种狗狗的信息。在这个示例中,创建一个名为 dogs 数据库表,它定义了可以被存储的数据。这样,每条 Dog 数据就包含了一个 id, name 和 age。因此,在 dogs 数据库表中将有三列,分别是 id, name 和 age。
id 是 Dart 的 int 类型,在数据表中是 SQLite 的 INTEGER 数据类型。最佳实践是将 id 作为数据库表的主键,用以改善查询和修改的时间。
name 是 Dart 的 String 类型,在数据表中是 SQLite 的 TEXT 数据类型。
age 也是 Dart 的 int 类型,在数据表中是 SQLite 的 INTEGER 数据类型。
关于 SQLite 数据库能够存储的更多的数据类型信息请查阅官方的 SQLite Datatypes 文档。
final database = openDatabase(
join(await getDatabasesPath(), 'dog.db'),
// 当数据库被第一次创建, 会创建这个表用来存储狗狗的数据
onCreate: (db, version) {
return db.execute(
'CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)',
);
},
//版本
version: 1,
);
14.4 插入一条狗狗的数据
现在你已经准备好了一个数据库用于存储各种狗狗的信息数据,现在开始读写数据咯。
首先,在 dogs 数据表中插入一条 Dog 数据。分以下两步:
把 Dog 转换成一个 Map 数据类型;
使用 insert() 方法把 Map 保存到 dogs 数据表中。
class Dog {
final int id;
final String name;
final int age;
Dog({required this.id, required this.name, required this.age});
// 将狗狗数据转为map ,key的值要与数据库的列名相同
Map<String, Object?> toMap() {
return {'id': id, 'name': name, 'age': age};
}
String toString() {
return 'Dog{id: $id, name: $name, age: $age}';
}
}
Future<void> insertDog(Dog dog) async {
// 获取数据库的引用
final db = await database;
await db.insert(
'dogs',
dog.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
插入一条数据
var fido = Dog(id: 0, name: 'Fido', age: 35);
await insertDog(fido);
14.5 查询狗狗列表
现在已经有了一条 Dog 存储在数据库里。你可以通过查询数据库,检索到一只狗狗的数据或者所有狗狗的数据。分为以下两步:
调用 dogs 表对像的 query 方法。这将返回一个 List
将 List
query 函数的参数
void query(String table, //指定要查询的表名。
{bool? distinct, //是否去重
List<String>? //指定要查询的列名。如果为 null,则查询所有列。
String? where, //where条件指定 WHERE 条件,用于过滤查询结果。
List<Object?>? whereArgs, //WHERE 条件的参数,用于防止 SQL 注入。
String? groupBy, //指定 GROUP BY 子句,用于对结果进行分组。
String? having,//指定 HAVING 条件,用于过滤分组后的结果。
String? orderBy,//指定 ORDER BY 子句,用于对结果进行排序。
int? limit, //指定查询结果的最大行数。
int? offset});//指定查询结果的起始位置。
import 'package:sqflite/sqflite.dart';
// 假设已经有一个打开的数据库连接
Database database;
// 示例 1:查询所有列
List<Map<String, dynamic>> results = await database.query('users');
// 示例 2:查询特定列
results = await database.query('users', columns: ['id', 'name']);
// 示例 3:带条件查询
results = await database.query(
'users',
where: 'age > ?',
whereArgs: [18],
);
// 示例 4:带分组和排序
results = await database.query(
'users',
columns: ['age'],
groupBy: 'age',
having: 'COUNT(*) > 1',
orderBy: 'age DESC',
);
// 示例 5:带限制和偏移
results = await database.query(
'users',
limit: 10,
offset: 20,
);
// 示例 6:去重查询
results = await database.query(
'users',
distinct: true,
columns: ['email'],
);
综合示例
results = await database.query(
'users',
distinct: true,
columns: ['id', 'name', 'email'],
where: 'age >= ? AND active = ?',
whereArgs: [20, 1],
groupBy: 'department',
having: 'COUNT(*) > 5',
orderBy: 'name ASC',
limit: 50,
offset: 0,
);
Future<List<Dog>> dogs() async {
final db = await database;
final List<Map<String, Object?>> dogMaps = await db.query('dogs');
return [
for (final {'id': id as int, 'name': name as String, 'age': age as int}
in dogMaps)
Dog(id: id, name: name, age: age),
];
}
print(await dogs());
14.6 修改一条 Dog 数据
使用 sqflite package 中的 update() 方法,可以对已经插入到数据库中的数据进行修改(更新)。
修改数据操作包含以下两步:
将一条狗狗的数据转换成 Map 数据类型;
使用 where 语句定位到具体将要被修改的数据。
Future<void> updateDog(Dog dog) async {
final db = await database;
await db.update(
'dogs',
dog.toMap(),
where: 'id = ?',
whereArgs: [dog.id],
);
}
fido = Dog(id: fido.id, name: fido.name, age: fido.age + 7);
await updateDog(fido);
print(await dogs());
请注意
使用 whereArgs 将参数传递给 where 语句。有助于防止 SQL 注入攻击。
这里不要使用字符串模板,比如: where: “id = ${dog.id}”!
14.7 删除一条 Dog 的数据
除了插入和修改狗狗们的数据,你还可以从数据库中删除狗狗的数据。删除数据用到了 sqflite package 中的 delete() 方法。
在这一小节,新建一个方法用来接收一个 id 并且删除数据库中与这个 id 匹配的那一条数据。为了达到这个目的,你必须使用 where 语句限定哪一条才是被删除的数据。
Future<void> deleteDog(int id)
final db = await database;
await db.delete(
'dogs',
where: 'id = ?',
whereArgs: [id],
);
}
14.8 GetX Controller 生命周期补充
在 Flutter 中使用 GetX 进行状态管理时,可以通过以下方法确保 Controller 全局不销毁:
使用 permanent 参数:在创建 Controller 时,将 permanent 参数设置为 true。这样,Controller 会被标记为不可销毁,页面销毁时不会自动销毁 Controller。
Get.put<YourController>(YourController(), permanent: true);
手动销毁 Controller:如果需要手动销毁 Controller,可以调用 Get.delete() 方法。在需要销毁 Controller 时,可以这样操作:
Get.delete<YourController>();
生命周期管理:了解 Controller 的生命周期方法,如 onInit、onReady、onClose 等,确保在 onClose 方法中正确释放资源,以避免内存泄漏。
使用 Get.lazyPut:与 Get.put 类似,但 Get.lazyPut 会在第一次访问时才创建 Controller 实例,这在某些情况下也可以帮助管理 Controller 的生命周期。
16. SharedPreferences 本地存储
步骤 1:添加依赖
在 pubspec.yaml 文件中添加 shared_preferences 依赖:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.2.2 # 确保使用最新版本
然后运行 flutter pub get 来安装依赖。
步骤 2:使用 SharedPreferences
以下是一个完整的代码示例,展示如何存储、读取和删除数据:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
// 存储的数据
final String _keyName = "username";
final String _keyAge = "age";
final String _keyTheme = "isDarkMode";
// 保存数据
Future<void> _saveData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString(_keyName, "John Doe");
await prefs.setInt(_keyAge, 30);
await prefs.setBool(_keyTheme, true);
print("Data saved successfully!");
}
// 读取数据
Future<void> _readData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? name = prefs.getString(_keyName);
int? age = prefs.getInt(_keyAge);
bool? isDarkMode = prefs.getBool(_keyTheme);
print("Name: $name, Age: $age, Dark Mode: $isDarkMode");
}
// 删除数据
Future<void> _deleteData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove(_keyName);
await prefs.remove(_keyAge);
await prefs.remove(_keyTheme);
print("Data deleted successfully!");
}
// 清空所有数据
Future<void> _clearAllData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.clear();
print("All data cleared!");
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("SharedPreferences Example"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _saveData,
child: const Text("Save Data"),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _readData,
child: const Text("Read Data"),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _deleteData,
child: const Text("Delete Data"),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _clearAllData,
child: const Text("Clear All Data"),
),
],
),
),
);
}
}
一句话总结:Flutter 以统一的 Dart 语言与自绘渲染引擎,用一套代码库实现多端原生级体验,并配套组件、状态管理、路由、动画与本地存储等完整开发生态。