1. 项目的目录结构

文件夹 描述
android android平台相关代码
ios ios平台相关代码
lib Flutter相关代码,主要编写的文件在这个目录下
test 存放测试代码
pubspec.yaml 配置文件,存放第三方库的依赖项

入口文件、入口方法

  • lib文件夹中的main.dart文件为入口文件
  • main方法是Dart的入口方法,runApp方法是Flutter的入口方法
  • 内置组件都是类,new关键字可以省略。

    2. 自定义组件

    materialApp

  • 封装了应用程序实现Material Design所需要的一些widget,一般作为顶层widget使用

    3. 基础组件

    状态管理组件

  • 在Flutter中自定义组件其实就是一个类,这个类继承StatelessWidget/StatefulWidget。

  • vscode 快捷键插件Awesome Flutter Snippets

    无状态组件

    StatelessWidget 无状态组件,状态不可变的Widget

    有状态组件

    StatefulWidget 有状态组件,持有的状态可能在widget生命周期改变

    Text 组件

    组件重要参数说明
名称 功能
textAlign 文本对齐方式(center、left、right、justfy)
textDirection 文本方向(ltr从左至右、rtl从右至左)
maxLines 文本显示最大行数
overflow 文本超出屏幕之后的处理方式(clip裁剪、fade渐隐、ellipsis省略号)
textScaleFactor 文本显示最大倍数
style 字体的样式设置TextStyle

TextStyle重要参数说明

名称 功能
decoration 文字装饰线
(none,lineThrough 删除线,overline 上划线,underline 下划线)
decorationColor 文字装饰线颜色
decorationStyle 文字装饰线风格
(dashed,dotted 虚线,double 两根线,solid 一根线,wavy 波浪线)
wordSpacing 单词间隙
letterSpacing 字母间隙
fontStyle 字体样式
fontSize 字体大小
color 字体颜色
fontWeight 字体粗细

Image 组件

Image.network 网络图片

名称 类型 说明
alignment Alignment 图片的对齐方式
color 设置图片背景颜色,通常和colorBlendMode一起使用
colorBlendMode BlendMode 颜色混合
fit BoxFit 控制图片的拉伸和挤压
repeat ImageRepeat 图片平铺

Image.assets 本地图片

如何实现圆角以及实现圆形图片

  • 通过Container 中的 decoration -> borderRadius
  • clipOver
  • CircleAvatar (头像裁剪用的多)

    1. // 通过Container 中的 decoration -> borderRadius
    2. decoration: BoxDecoration(
    3. borderRadius: BorderRadius.circular(150),
    4. image: DecorationImage(
    5. image: NetworkImage(
    6. 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3593964987,1999043304&fm=15&gp=0.jpg',
    7. ),
    8. fit: BoxFit.cover,
    9. ),
    10. // borderRadius: BorderRadius.all(Radius.circular(150))
    11. )
    12. // clipOver
    13. child: ClipOval(
    14. child: Image.asset(
    15. 'images/4.png',
    16. width: 150,
    17. height: 100,
    18. ),
    19. //
    20. CircleAvatar(
    21. backgroundImage: NetworkImage(
    22. "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1129084263,3532636474&fm=26&gp=0.jpg"),
    23. )

    AspectRatio 组件

  • AspectRatio根据设置调整子元素child的宽高比。

  • AspectRatio首先会在布局限制条件允许的范围内尽可能的扩展,widget的高度是由宽度和比率决定的,类似于BoxFit中的contain,按照固定比率去尽量占满区域。
  • 如果在满足所有限制条件过后无法找到一个可行的尺寸,AspectRatio最终将会去优先适应布局限制条件,而忽略所设置的比率

    Card 卡片组件

    | 参数 | 说明 | | —- | —- | | margin | 外边距 | | shadowColor | 阴影颜色 | | shape | 阴影效果,默认阴影效果为圆角的长方形边 | | color | 背景颜色 | | child | 子组件 |

按钮组件

  • ElevatedButton
  • TextButton
  • OutlinedButton 线框按钮
  • IconButton 图标按钮
  • ButtonBar 按钮组
  • FloatingActionButton 浮动按钮

    1. // 普通按钮
    2. ElevatedButton(
    3. onPressed: () {},
    4. style: ButtonStyle(
    5. backgroundColor: MaterialStateProperty.all(Colors.red),
    6. elevation: MaterialStateProperty.all(20.0),
    7. ),
    8. child: Text('阴影按钮'),
    9. ),
    10. // 文字按钮
    11. TextButton.styleFrom(
    12. shape: RoundedRectangleBorder(
    13. side: BorderSide(
    14. color: Colors.yellow,
    15. ),
    16. ),
    17. ),
    18. // 线框按钮
    19. OutlinedButton(
    20. onPressed: () {},
    21. child: Container(
    22. height: 50,
    23. child: Text('outline'),
    24. ),
    25. )
    26. // 图标按钮
    27. IconButton(
    28. icon: Icon(Icons.ac_unit_outlined),
    29. onPressed: () {},
    30. )
    31. // 按钮组
    32. ButtonBar(
    33. children: [
    34. ElevatedButton(onPressed: () {}, child: Text('按钮组1')),
    35. ElevatedButton(onPressed: () {}, child: Text('按钮组2')),
    36. ElevatedButton(onPressed: () {}, child: Text('按钮组3'))
    37. ],
    38. )
    39. // 浮动按钮
    40. floatingActionButton: FloatingActionButton(
    41. onPressed: () {
    42. this.setState(() {
    43. this.currentIndex = 1;
    44. });
    45. },
    46. backgroundColor: this.currentIndex == 2 ? Colors.blue : Colors.red,
    47. child: Container(
    48. width: 100.0,
    49. height: 100.0,
    50. decoration: BoxDecoration(
    51. borderRadius: BorderRadius.circular(70),
    52. // color: Colors.blue,
    53. ),
    54. child: Icon(
    55. Icons.add,
    56. ),
    57. padding: EdgeInsets.all(8),
    58. // margin: EdgeInsets.only(top: 10.0),
    59. ),
    60. ),
    61. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

    表单组件

  • TextField 文本框组件

  • Checkbox、CheckboxListTile 多选框组件
  • Radio、RadioListTile
  • 日期组件
  • 自带组件
  • 第三方组件
  • swiper组件

    弹框组件

  • showDialog

  • AlertDialog

    1. showDialog(
    2. context: context,
    3. builder: (context) {
    4. return AlertDialog(
    5. title: Text('删除'),
    6. content: Text('确认取消吗'),
    7. actions: [
    8. ElevatedButton(
    9. onPressed: () {
    10. Navigator.pop(context, 'cancel');
    11. },
    12. child: Text('取消'),
    13. ),
    14. ElevatedButton(
    15. onPressed: () {
    16. Navigator.pop(context, 'ok');
    17. },
    18. child: Text('确定'),
    19. )
    20. ],
    21. );
    22. },
    23. );
  • SimpleDialog

    1. showDialog(
    2. context: context,
    3. builder: (context) {
    4. return SimpleDialog(
    5. title: Text('选择内容'),
    6. children: [
    7. SimpleDialogOption(
    8. child: Text('1'),
    9. ),
    10. SimpleDialogOption(
    11. child: Text('2'),
    12. ),
    13. SimpleDialogOption(
    14. child: Text('3'),
    15. ),
    16. SimpleDialogOption(
    17. child: Text('4'),
    18. )
    19. ],
    20. );
    21. });
  • showModalBottomSheet

    1. showModalBottomSheet(
    2. context: context,
    3. builder: (context) {
    4. return Container(
    5. height: 200.0,
    6. child: Column(
    7. children: [
    8. ListTile(
    9. title: Text('分享一'),
    10. ),
    11. Divider(color: Colors.black),
    12. ListTile(
    13. title: Text('分享二'),
    14. ),
    15. Divider(color: Colors.black),
    16. ListTile(
    17. title: Text('分享三'),
    18. )
    19. ],
    20. ),
    21. );
    22. },
    23. );
  • Fluttertoast.showToast

    1. import 'package:fluttertoast/fluttertoast.dart';
    2. Fluttertoast.showToast(
    3. msg: '提示信息',
    4. toastLength: Toast.LENGTH_LONG,
    5. gravity: ToastGravity.BOTTOM,
    6. timeInSecForIos: 1,
    7. backgroundColor: Colors.red,
    8. );
  • 自定义Dialog

    • 自定义 Dialog 类

      4. 容器类组件

      Scaffold

  • sMaterial Design布局结构的基本实现,此类提供了用于显示drawer、snackbar和底部sheet的API

    1. import 'package:flutter/material.dart';

    BottomNavigationBar 底部导航组件

    | 参数 | 说明 | | —- | —- | | currentIndex | 默认选中第几个 | | onTap | 点击回调方法 | | iconSize | icon大小 | | fixedColor | 选中的颜色 | | type | BottomNavigationBarType.fixed 可以有多个按钮 BottomNavigationBarType.shifting | | items | LIst BottomNavigationBarItem
    底部导航条按钮集合 |

AppBar 顶部导航组件

参数 说明
centerTitle 无论安卓、ios都居中显示
leading 头部导航前面展示的组件
actions 头部导航后面展示的组件
bottom 自定义组件

TabBar 组件

参数 说明
tabs 显示的标签内容,便是Tab对象,也可以是其他的widget
controller TabController对象
(结合SingleTickerProviderStateMixin)
isScrollable 是否可以滚动
indicatorColor 指示器的颜色
indicatorWeight 指示器高度
indicatorPadding 底部指示器的高度
indicator 指示器decoration,例如边框等
indicatorSize 指示器大小计算方式
TabBarIndicatorSize.label 跟文字等宽
TabBarIndicatorSize.tab 跟tab等宽
labelColor 选中的label的颜色
labelStyle 选中的label的style
labelPadding 每个label的padding值
unselectedLabelColor 未选中的label的颜色
unselectedLabelStyle 未选中的label的style

Container 组件

组件重要参数说明

名称 功能
alignment topCenter 顶部居中对齐
topLeft 顶部左对齐
topRight 顶部右对齐
center 水平垂直居中对齐
centerLeft 垂直居中水平居左对齐
centerRight 垂直居中水平居右对齐
bottomLeft 底部左对齐
bottomRight 底部右对齐
transform 矩阵变换
decoration 设置背景颜色、边框颜色

Padding

名称 说明
padding padding EdgeInsets设置填充的值
child 自组件

5. 可滚动组件

ListView

水平列表、垂直列表

  • 列表参数 | 名称 | 类型 | 说明 | | —- | —- | —- | | scrollDirection | Axis | Axis.horizontal 水平列表
    Axis.vertical 垂直列表 | | padding | EdgeInsetsGeometry | 内边距 | | resolve | bool | 组件反向排序 | | children | List[Widget] | 列表元素 |
  1. ListView(
  2. scrollDirection: Axis.horizontal,
  3. padding: EdgeInsets.all(10),
  4. children: <Widget>[
  5. ListTile(
  6. title: Text("标题"),
  7. subtitle: Text("二级标题"),
  8. leading: Icon(Icons.home),
  9. // tileColor: Colors.red,
  10. selectedTileColor: Colors.yellow),
  11. ListTile(
  12. title: Text("标题"),
  13. subtitle: Text("二级标题"),
  14. leading: Image.network(
  15. 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3593964987,1999043304&fm=15&gp=0.jpg'),
  16. trailing: Icon(Icons.offline_bolt, color: Colors.red, size: 28.0),
  17. )
  18. ]
  19. )
  • 常使用组件 ListTile

    动态列表

  • map、for 等方法

  • ListView.builder

    GridView

    网格列表

  • GridView.count | 名称 | 类型 | 说明 | | —- | —- | —- | | children | List[Widget] | 列表元素 | | crossAxisCount | Int | 一行的Widget数量 | | mainAxisSpacing | Double | 垂直Widget之间的间距 | | crossAxisSpacing | Double | 水平Widget之间的间距 | | childAspectRatio | Double | Widget的宽高比 |

  • GridView.builder | 名称 | 类型 | 说明 | | —- | —- | —- | | itemCount | Int | 列表数量 | | gridDelegate | SliverGridDelegateWithFixedCrossAxisCount | 列表样式之类的写在其中 | | itemBuilder | Widget | Widget元素 |

6. 布局类组件

线性布局

Row 水平布局组件

名称 说明
mainAxisAlignment 主轴的排序方式
crossAxisAlignment 次轴的排序方式
stretch(次轴高度填满)、center、start、end
children 组件子元素

Column 垂直布局组件

名称 说明
mainAxisAlignment 主轴的排序方式
crossAxisAlignment 次轴的排序方式
stretch(次轴高度填满)、center、start、end
children 组件子元素

弹性布局

Expanded

web端的flex布局

名称 说明
flex
child 子组件

对齐与相对定位 Stack 、Align

  1. // Stack 和 Align
  2. Stack(
  3. children: [
  4. Align(
  5. alignment: Alignment.topCenter,
  6. child: Icon(
  7. Icons.ac_unit,
  8. color: Colors.white,
  9. ),
  10. ),
  11. Align(
  12. alignment: Alignment.center,
  13. child: Icon(
  14. Icons.sanitizer,
  15. color: Colors.white,
  16. ),
  17. ),
  18. Align(
  19. alignment: Alignment.bottomCenter,
  20. child: Icon(
  21. Icons.east,
  22. color: Colors.white,
  23. ),
  24. ),
  25. ],
  26. )

层叠布局 Stack 、Positioned

  • 与Web中的绝对定位概念类似

stack参数说明

名称 说明
alignment 配置所有子元素的显示位置
children 自组件
  1. // Stack 和 Positioned
  2. Stack(
  3. children: [
  4. Positioned(
  5. left: 0,
  6. top: 0,
  7. child: Icon(
  8. Icons.ac_unit,
  9. color: Colors.white,
  10. ),
  11. ),
  12. Positioned(
  13. top: 0,
  14. right: 0,
  15. child: Icon(
  16. Icons.sanitizer,
  17. color: Colors.white,
  18. ),
  19. ),
  20. ]
  21. )

流式布局 Wrap、Flow

Wrap

warp实现流式布局,单行的Wrap和Row表现基本一致,单列的Wrap则跟Column表现一致。但Row和Column都是单行单列的,Wrap则突破了这个限制,mainAxis上空间不足时,则向crossAxis上扩展显示。

属性 说明
direction 主轴方向,默认水平
alignment 主轴的对齐方式
runAlignment 纵轴的对齐方式
spacing 主轴方向上的间距
runSpacing 纵轴方向上的间距
children 子组件

7. 路由

基本路由以及参数传递

  1. // 基本路由
  2. Navigator.of(context).push(
  3. MaterialPageRoute(
  4. builder: (context) {
  5. return Search();
  6. },
  7. ),
  8. ),

命名路由以及参数传递

  1. // 命名路由
  2. Navigator.pushNamed(context, '/search')
  3. // 不传惨
  4. routes: {
  5. '/search': (context) => Search(),
  6. '/list': (context) => List(),
  7. },
  8. // 传参数
  9. onGenerateRoute: (RouteSettings settings) {
  10. final name = settings.name;
  11. final Function pageContentBuilder = this.routes[name];
  12. Route route;
  13. if (pageContentBuilder != null) {
  14. if (settings.arguments != null) {
  15. route = MaterialPageRoute(
  16. builder: (context) {
  17. return pageContentBuilder(context, arguments: settings.arguments);
  18. },
  19. );
  20. }
  21. } else {
  22. route = MaterialPageRoute(
  23. builder: (context) {
  24. return pageContentBuilder(context);
  25. },
  26. );
  27. }
  28. return route;
  29. },
  30. );

路由替换

  1. Navigator.pushReplacementNamed(
  2. context,
  3. '/search',
  4. arguments: {
  5. "title": "参数传递",
  6. },
  7. )

返回根路由

  1. Navigator.of(context).pushAndRemoveUntil(
  2. new MaterialPageRoute(
  3. builder: (context) => Tabs(currentIndex: 1),
  4. ),
  5. (route) => route == null)

8. 接口请求

  • json字符串和Map类型的转换
    • import ‘dart:convert’;
    • json.encode
    • json.decode
  • 请求数据 http

    1. import 'package:http/http.dart' as http;
    2. var httpUrl = '';
    3. var data = await http.get(httpUrl);
    4. var data = await http.post(
    5. httpUrl,
    6. // headers: {'Content-Type': 'application/json; charset=utf-8'},
    7. body: json.encode({'Phone': '18116462666', 'Password': '123456789'}),
    8. );
  • 请求数据 dio

    1. import 'package:dio/dio.dart';
    2. var httpUrl = '';
    3. var dio = Dio();
    4. Response result = await dio.get(httpUrl);
    5. Response result = await dio.post(httpUrl, data: data);