【Flutter 专题】26 易忽略的【小而巧】的技术点汇总 (四)

网友投稿 684 2022-05-28

小菜继续整理 flutter 中日常用到的小知识点。

对于加载网络图片时,添加一个加载动画或网络图片异常时添加一个错误图片会给用户一个良好的体验,此时 CachedNetworkImage 可以帮我们解决这个问题。CachedNetworkImage 是一个三方 pub 库,引入的基本方式省略;

CachedNetworkImage 中有两个属性很重要:

placeholder 用来在加载图片时的缓冲过程,可以是动态 loading 亦或者 Widget 等;

errorWidget 用来网络图片加载异常时展现,可自定义进行展示。

Tips: 在使用加载 loading 或默认图片时,建议限制 loading 和默认图片的大小,这样不会出现默认图片比加载网络图更大的效果。

Widget _cachedNetImgWid() { return Container( color: Colors.grey, child: Center( child: CachedNetworkImage( height: 300.0, placeholder: Container( width: 50.0, height: 50.0, child: CircularProgressIndicator()), errorWidget: Container( height: 300.0, child: Image.asset('images/icon_wrong.jpg')), imageUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544938569112&di=feeab11968f3870520482630563c4f54&imgtype=0&src=http%3A%2F%2Fi1.hdslb.com%2Fbfs%2Farchive%2Fac5c906111130579c6909aae47f6656e20d0906b.jpg', ))); }

小菜在使用 TextField 文本框时会对键盘进行操作,为了良好对用户体验。在键盘右下角会有不同的按钮样式。例如搜索页面在输入完成搜索信息后展示搜索的按钮更便捷。此时需要考虑 TextInputAction 属性,可自定义展示内容。Flutter 提供了13种状态,但需注意的是有些是区分 Android 和 iOS 的,使用时需加注意。

Widget _textFiledWid() { return Padding(padding: EdgeInsets.all(10.0),child:ListView(children: [ Padding(padding: EdgeInsets.all(8.0),child:Text('键盘右下角按钮-done')), TextField(textInputAction: TextInputAction.done), Padding(padding: EdgeInsets.all(8.0),child:Text('键盘右下角按钮-go')), TextField(textInputAction: TextInputAction.go), Padding(padding: EdgeInsets.all(8.0),child:Text('键盘右下角按钮-newline')), TextField(textInputAction: TextInputAction.newline), Padding(padding: EdgeInsets.all(8.0),child:Text('键盘右下角按钮-next')), TextField(textInputAction: TextInputAction.next), Padding(padding: EdgeInsets.all(8.0),child:Text('键盘右下角按钮-search')), TextField(textInputAction: TextInputAction.search), Padding(padding: EdgeInsets.all(8.0),child:Text('键盘右下角按钮-send')), TextField(textInputAction: TextInputAction.send), Padding(padding: EdgeInsets.all(8.0),child:Text('键盘右下角按钮-join')), TextField(textInputAction: TextInputAction.join), ])); }

小菜在学习过程中发现一个很方便的 DefaultTextStyle,用来处理当前页面统一的文本样式。与 Android 中对文本进行自定义 style 很相似。

在当前页面中设置统一的 DefaultTextStyle 默认文本样式,在当前页面中用到的 Text 默认应用的都是该样式,若需要调整部分样式,直接设置 TextStyle 即可;若不需要重用该样式,设置 inherit: false 属性即可,但 textAlign 并不在该效果内。

Widget _childDefaultTextWid() { return new SafeArea( top: false, child: Scaffold( appBar: new AppBar( title: Text('DefaultTextStyle Demo'), ), body: DefaultTextStyle( style: TextStyle( color: Colors.blueGrey, wordSpacing: 10.0, fontSize: 20.0), textAlign: TextAlign.center, child: Container( child: ListView(children: [ Text("Ha ha ha!默认文本样式(局中/20.0)"), Text("Ha ha ha!与默认文本样式部分不同", style: TextStyle(color: Colors.redAccent), textAlign: TextAlign.left), Text("Ha ha ha!", style: TextStyle(inherit: false)), Text("Ha ha ha!自己单独样式", style: TextStyle(inherit: false, color: Colors.grey)) ])), ))); }

小菜在学习过程中尝试了一下 ExpansionTile,是一个可向下扩展空间的 Widget,如效果图。

const ExpansionTile({ Key key, this.leading, // 前置图标 @required this.title, // 标题内容 this.backgroundColor, // 背景色包括扩展空间整体背景色 this.onExpansionChanged,// 扩展时监听状态 this.children = const [], // 扩展空间 this.trailing, // 动画效果 this.initiallyExpanded = false, // 初始化时是否展开 }) : assert(initiallyExpanded != null), super(key: key);

小菜尝试过程中发现 ExpansionTile 虽然很方便,效果也很好,但是也有一些局限性,如下:

默认右侧箭头图标是固定的,包括动画旋转角度等,不能直接调整,需要自定义;

点击扩展区域不会消失,需要自定义。

Widget _childExpanTileWid() { return new SafeArea( child: Scaffold( appBar: AppBar(title: Text('扩展 Tile')), body: Container( child: ExpansionTile( title: Text('大礼包'), leading: Icon(Icons.adjust), backgroundColor: Colors.grey, onExpansionChanged: (bool) { _exState = bool; }, children: [ Container( decoration: BoxDecoration(borderRadius: BorderRadius.circular(3.0), color: Colors.white), margin: EdgeInsets.all(5.0), child: Column(children: [ Row(children: [ Padding( padding: EdgeInsets.fromLTRB(10.0, 8.0, 0.0, 8.0), child: Text('1. 可获取双倍会员积分;', style: TextStyle(color: Colors.blue, fontSize: 16.0))) ]), Row(children: [ Padding( padding: EdgeInsets.fromLTRB(10.0, 8.0, 0.0, 8.0), child: Text('2. 积分兑换奖品8折优惠;', style: TextStyle(color: Colors.blue, fontSize: 16.0))) ]) ])) ])))); }

Spacer 是小菜偶然间了解到的一个很强大的 Widget,Spacer 小菜的理解是占位组件,直接看效果图更加直观。Spacer 创建一个可调节的空间隔,可用于调整 Flex 容器(如行或列)中窗口小部件之间的间距;默认 flex: 1。

Widget _childSpacerWid() { return new SafeArea( top: false, child: Scaffold( appBar: new AppBar( title: Text('Spacer Demo'), ), body: Column(children: [ Row(children: [ Text('Begin', style: TextStyle(color: Colors.redAccent)), Spacer(), Text('Middle', style: TextStyle(color: Colors.greenAccent)), Spacer(flex: 2), Text('End', style: TextStyle(color: Colors.blue)) ]), Row(children: [ Text('Begin', style: TextStyle(color: Colors.redAccent)), Spacer(), Text('Middle', style: TextStyle(color: Colors.greenAccent)), Text('End', style: TextStyle(color: Colors.blue)) ]), Row(children: [ Text('Begin', style: TextStyle(color: Colors.redAccent)), Text('Middle', style: TextStyle(color: Colors.greenAccent)), Spacer(flex: 2), Text('End', style: TextStyle(color: Colors.blue)) ]) ]))); }

如果有不对的地方还希望多多指出。

【Flutter 专题】26 易忽略的【小而巧】的技术点汇总 (四)

来源:阿策小和尚

Flutter

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一天就可以拥有自己的论坛,你心动了吗?
下一篇:windows机器挂盘重置机器密码(超级详细)
相关文章