当前位置:   article > 正文

Flutter 常用 Widget 介绍

children:

前言

笔者最近在看 Flutter Widget of the Week:https://www.youtube.com/watch?v=b_sQ9bMltGU,并落地了代码 FlutterLearningRecord:https://github.com/fluttercndev/FlutterLearningRecord。在本文中,笔者将分享几个 Widget 的使用场景及使用方式。在本文中,笔者会介绍如下Widget:SafeArea、Expanded、Wrap、AnimatedContainer、Opacity、FutureBuilder、在BottomAppBar居中的 FloatingActionButton。

首先给大家展示下目前笔者做的常用 Widget 的效果。

一、常用 Widget Demo 效果

笔者上方的常用 Widget Demo 效果图,展示了SafeArea、Expanded、Wrap、AnimatedContainer、Opacity、FutureBuilder、在底部AppBar居中的 FloationgActionButton的使用场景及使用效果。

Widget使用场景
SafeArea用于带头帘、下巴的设备
Expanded用于Row、Column中的子Widget布局完后,撑开剩余空间
Wrap用于包裹可能子Widget可能越过屏幕边界的场景,使子Widget可以换行
AnimatedContainer用于给子Widget做动画效果
Opacity用于对子Widget做不透明度处理
AnimatedOpacity用于给子Widget的不透明度变化过程做动画
FutureBuilder用于耗时任务,耗时执行完后返回请求到的数据
FloationActionButton可以在BottomAppBar底部居中,一定程度适用发布视频文章,也可在屏幕中其他位置悬浮

下边给大家简单介绍下上边的Widget的使用方式。

二、常用 Widget 使用方式

1

SafeArea

1.1

不带SafeArea 示意图

1.2

带SafeArea 示意图

使用 SafeArea 作为 body 的子 Widget,原来的子 Widget 作为 SafeAreade 的子 Widget;

1.3

SafeArea 示例代码

  1. @override
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. body: SafeArea(
  5. child: ListView(
  6. children: <Widget>[
  7. _randomColorContainer(),
  8. ],
  9. ),
  10. ),
  11. );
  12. }

2

Expanded

2.1

左右 Expanded(黄色部分Widget) 分别占剩余空间的2:1

Expanded 会显然同级的其他 Widget 先布局,之后剩余的空间,Expanded 才会去占用。

当有多个Expanded时,Expanded的 flex 参数,用于指定要占空白的空间的比例。

2.2

Expaned 示例代码

  1. Row _expandedRow3() {
  2. return Row(
  3. children: <Widget>[
  4. Text(
  5. '3.LeftText',
  6. style: TextStyle(fontSize: 32.0, backgroundColor: Colors.greenAccent),
  7. ),
  8. Expanded(
  9. flex: 2,
  10. child: Container(
  11. height: 20.0,
  12. color: Colors.yellow,
  13. )),
  14. Container(
  15. color: Colors.blue,
  16. width: 100.0,
  17. // width: 10.0,
  18. height: 50.0,
  19. child: Text(
  20. 'C',
  21. style:
  22. TextStyle(fontSize: 32.0, backgroundColor: Colors.greenAccent),
  23. ),
  24. ),
  25. Expanded(
  26. flex: 1,
  27. child: Container(
  28. height: 20.0,
  29. color: Colors.yellow,
  30. )),
  31. Container(
  32. width: 100.0,
  33. height: 50.0,
  34. child: Text(
  35. 'Right',
  36. style:
  37. TextStyle(fontSize: 32.0, backgroundColor: Colors.greenAccent),
  38. ),
  39. ),
  40. ],
  41. );
  42. }

3

Wrap

3.1

Wrap Demo 示意图

1.2

Wrap 示例代码

  1. Wrap horizontalWrap(int index) {
  2. return Wrap(
  3. // 默认主轴为水平方向
  4. // direction: Axis.horizontal,
  5. children: <Widget>[
  6. horizontalRandomColorWrapContainer(index++),
  7. horizontalRandomColorWrapContainer(index++),
  8. horizontalRandomColorWrapContainer(index++),
  9. horizontalRandomColorWrapContainer(index++),
  10. ],
  11. );
  12. }

4

AnimatedContainer

4.1

AnimatedContainer 执行动画前示意图

4.2

AnimatedContainer 执行动画后示意图 

AnimatedContainer 执行前后,改变了 Widget 的背景色、宽度、高度、子 Widget 的对齐方式。

4.3

AnimatedContainer 示例代码

  1. AnimatedContainer(
  2. onEnd: () {
  3. print('动画结束');
  4. },
  5. child: DecoratedBox(
  6. child: FlutterLogo(size: halfContainerWH,),
  7. decoration: BoxDecoration(
  8. //TODO: borderRadius 效果
  9. borderRadius: selected ? BorderRadius.all(Radius.circular(25.0)) : BorderRadius.all(Radius.circular(0)),
  10. )),
  11. duration: Duration(seconds: 2),
  12. curve: Curves.linearToEaseOut,
  13. width: selected ? halfContainerWH : containerWH,
  14. height: selected ? containerWH : halfContainerWH,
  15. alignment: selected ? Alignment.topCenter : Alignment.center,
  16. color: selected ? Colors.purpleAccent : Colors.blueGrey),

5

Opacity

5.1

 Opacity 的不透明度

5.2

Opacity 示例代码

  1. Opacity(
  2. opacity: 1.0,
  3. child: Container(
  4. decoration: BoxDecoration(color: Colors.blue),
  5. child: Text(
  6. 'Opacity: 1.0',
  7. style: TextStyle(
  8. color: Colors.white,
  9. backgroundColor: Colors.blue,
  10. fontSize: 24.0),
  11. ),
  12. ),
  13. // child: Text('Opacity:1.0'),
  14. ),

6

AnimatedOpacity

6.1

AnimatedOpacity 改变透明度前

6.2

AnimatedOpacity 改变透明度后

6.3

AnimatedOpacity 示例代码

  1. AnimatedOpacity(
  2. onEnd: () {
  3. print('动画结束');
  4. },
  5. opacity: animatedOpacityValue,
  6. duration: Duration(seconds: 2),
  7. curve: Curves.fastOutSlowIn,
  8. child: FlutterLogo(size: 100.0),
  9. ),

7

FutureBuilder

7.1

FutureBuilder 效果图

7.2

FutureBuilder 示例代码

  1. FutureBuilder(
  2. future: Dio().get('https://api.github.com/orgs/flutterchina/repos'),
  3. builder: (BuildContext context, AsyncSnapshot snapshot) {
  4. if (snapshot.connectionState == ConnectionState.done) {
  5. Response response = snapshot.data;
  6. // 请求结果有误,显示错误信息
  7. if (snapshot.hasError) {
  8. return Text(snapshot.error.toString());
  9. }
  10. // 显示请求结果
  11. return ListView(
  12. children: response.data
  13. .map<Widget>((obj) => ListTile(
  14. title: Text(obj["name"]),
  15. subtitle: Text(obj["full_name"])))
  16. .toList(),
  17. );
  18. } else if (snapshot.connectionState == ConnectionState.waiting) {
  19. } else if (snapshot.connectionState == ConnectionState.none) {
  20. }
  21. // 请求过程中返回进度指示器
  22. return CircularProgressIndicator(
  23. strokeWidth: 10.0,
  24. semanticsLabel: '请稍候...',
  25. );
  26. }),

8

FloationgActionButton 

8.1

居中 FloatingActionButton 效果

8.2

FloationActionButton 居中示例代码

  1. floatingActionButton: FloatingActionButton(
  2. // child: Text('FAB'),
  3. child: Icon(Icons.add),
  4. onPressed: () {
  5. print('点击FAB');
  6. },
  7. ),
  8. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  9. bottomNavigationBar: BottomAppBar(
  10. color: Colors.blue,
  11. child: Container(
  12. height: 44.0,
  13. ),
  14. ),

三、Demo

1

Demo下载地址

1.1

FlutterLearingRecord

Demo 下载地址:FlutterLearningRecord:https://github.com/fluttercndev/FlutterLearningRecord

四、参考学习网址

- https://flutter.cn

- https://flutter.dev/docs/get-started/web

- https://flutter.dev/docs/

- https://book.flutterchina.club

- https://book.flutterchina.club/chapter14/

- http://gityuan.com/2019/06/29/flutter_run_app/

- https://juejin.im/post/5cce7042518825415234792d

- https://www.jianshu.com/p/094fab99cec6

- https://juejin.im/post/5d6ce03ef265da03ab426881

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/313290
推荐阅读
相关标签
  

闽ICP备14008679号