Flutter系列重學(xué)Container示例詳解
一、Container 簡(jiǎn)介
flutter 開(kāi)發(fā)中最核心的是用最少的組件(層次)完成功能開(kāi)發(fā);Container 前端的盒子模型實(shí)現(xiàn),類似 div 標(biāo)簽,掌握其他組件前,深入學(xué)習(xí)理解 Container 的使用是必要的。
Container
是一個(gè)組合類容器,由DecoratedBox
、ConstrainedBox、Transform
、Padding
、Align
等組件組合的一個(gè)多功能容器,所以我們只需通過(guò)一個(gè)Container
組件可以實(shí)現(xiàn)同時(shí)需要裝飾、變換、約束限制的場(chǎng)景。
二、示例
- 示例包含透明度,背景裝飾,前景裝飾,child 模糊度,紅色部分是 child;
buildSection() { return Opacity( opacity: 1, child: Container( margin: EdgeInsets.all(20), padding: EdgeInsets.all(20), decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(20)), gradient: LinearGradient( colors: [Colors.green, Colors.yellow], begin: Alignment.topCenter, end: Alignment.bottomCenter, ), boxShadow: [ BoxShadow( color: Colors.red.withOpacity(0.5), spreadRadius: 5, blurRadius: 7, offset: Offset(0, 3), // changes position of shadow ), ], image: DecorationImage( image: NetworkImage('https://tenfei02.cfp.cn/creative/vcg/800/new/VCG21409037867.jpg'), fit: BoxFit.cover, ), ), foregroundDecoration: BoxDecoration( color: Colors.yellow, border: Border.all(color: Colors.green, width: 5), borderRadius: BorderRadius.all(Radius.circular(400)), image: DecorationImage( image: NetworkImage('https://pic.616pic.com/bg_w1180/00/04/08/G5Bftx5ZDI.jpg'), fit: BoxFit.cover, ), ), child: BackdropFilter( filter: ui.ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0), child: Container( constraints: BoxConstraints.expand(), decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(20)), color: Colors.red, ), child: Text('VCG21409037867'), ), ), ), ); }
- 實(shí)現(xiàn)背景圖 background-repeat 顯示,flutter支持四種類型:
enum ImageRepeat { repeat, repeatX, repeatY, noRepeat, }
這里以第一種 ImageRepeat.repeat 為例:
Container( decoration: BoxDecoration( image: DecorationImage( image: AssetImage('images/img_update.png'), repeat: ImageRepeat.repeat, alignment: Alignment.topLeft, ) ), child: Container( constraints: BoxConstraints.expand(), child: OutlinedButton( onPressed: () { print("ImageRepeat.repeat"); }, child: Text('ImageRepeat.repeat', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red), ), ), ), ),
transform && alignment
Container( decoration: BoxDecoration( image: DecorationImage( image: AssetImage('images/img_update.png'), repeat: ImageRepeat.repeat, alignment: Alignment.topLeft, ) ), transform: Matrix4.rotationZ(.2),//變化 alignment: Alignment.centerRight, //卡片文字對(duì)齊 child: Text( "5.20", style: TextStyle(color: Colors.red, fontSize: 40.0), ), ),
三、源碼分析
class Container extends StatelessWidget { Container({ Key? key, this.alignment, this.padding, this.color, this.decoration, this.foregroundDecoration, double? width, double? height, BoxConstraints? constraints, this.margin, this.transform, this.transformAlignment, this.child, this.clipBehavior = Clip.none, }):super(key: key); // 子項(xiàng) final Widget? child; // 內(nèi)邊距 final EdgeInsetsGeometry? padding; // 背景色,和 decoration 只能二選一 final Color? color; // 背景裝飾 final Decoration? decoration; // 前景裝飾(在 child 之上) final Decoration? foregroundDecoration; // 約束條件 final BoxConstraints? constraints; // 外邊距 final EdgeInsetsGeometry? margin; // 變化 final Matrix4? transform; // 變化之后 child 對(duì)齊方式 final AlignmentGeometry? transformAlignment; // 裁剪方式 final Clip clipBehavior; EdgeInsetsGeometry? get _paddingIncludingDecoration { if (decoration == null || decoration!.padding == null) return padding; final EdgeInsetsGeometry? decorationPadding = decoration!.padding; if (padding == null) return decorationPadding; return padding!.add(decorationPadding!); } @override Widget build(BuildContext context) { Widget? current = child; if (child == null && (constraints == null || !constraints!.isTight)) { current = LimitedBox( maxWidth: 0.0, maxHeight: 0.0, child: ConstrainedBox(constraints: const BoxConstraints.expand()), ); } if (alignment != null) current = Align(alignment: alignment!, child: current); final EdgeInsetsGeometry? effectivePadding = _paddingIncludingDecoration; if (effectivePadding != null) current = Padding(padding: effectivePadding, child: current); if (color != null) current = ColoredBox(color: color!, child: current); if (clipBehavior != Clip.none) { assert(decoration != null); current = ClipPath( clipper: _DecorationClipper( textDirection: Directionality.maybeOf(context), decoration: decoration!, ), clipBehavior: clipBehavior, child: current, ); } if (decoration != null) current = DecoratedBox(decoration: decoration!, child: current); if (foregroundDecoration != null) { current = DecoratedBox( decoration: foregroundDecoration!, position: DecorationPosition.foreground, child: current, ); } if (constraints != null) current = ConstrainedBox(constraints: constraints!, child: current); if (margin != null) current = Padding(padding: margin!, child: current); if (transform != null) current = Transform(transform: transform!, alignment: transformAlignment, child: current); return current!; } ... }
源碼很簡(jiǎn)單,組件層層包裹,但是我們必須清楚每一個(gè)參數(shù)的作用和使用場(chǎng)景。它可以幫你用更少的層級(jí)完成功能,這很重要。
以上就是Flutter系列重學(xué)Container示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Flutter重學(xué)Container的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Android的英文詞典的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了基于Android的英文詞典的實(shí)現(xiàn)方法2016-05-05Android studio實(shí)現(xiàn)兩個(gè)界面間的切換
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)兩個(gè)界面間的切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android IPC進(jìn)程間通信詳解最新AndroidStudio的AIDL操作)
這篇文章主要介紹了Android IPC進(jìn)程間通信的相關(guān)資料,需要的朋友可以參考下2016-09-09Android仿騰訊QQ實(shí)現(xiàn)滑動(dòng)刪除 附源碼下載
仿騰訊QQ滑動(dòng)刪除操作,這篇文章主要為大家詳細(xì)介紹了ListView滑動(dòng)刪除的具體操作方法,感興趣的小伙伴們可以參考一下2016-07-07Android 3.0引入的異步加載機(jī)制Loader
Loader裝載器從android3.0開(kāi)始引進(jìn)。它使得在activity或fragment中異步加載數(shù)據(jù)變得簡(jiǎn)單。下面我們就來(lái)詳細(xì)講解下2017-12-12Flutter使用SingleTickerProviderStateMixin報(bào)錯(cuò)解決
這篇文章主要為大家介紹了Flutter使用SingleTickerProviderStateMixin報(bào)錯(cuò)解決示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android中AutoCompleteTextView與MultiAutoCompleteTextView的用法
這篇文章主要介紹了Android中AutoCompleteTextView與MultiAutoCompleteTextView的用法,需要的朋友可以參考下2014-07-07Android編程中PopupWindow的用法分析【位置、動(dòng)畫(huà)、焦點(diǎn)】
這篇文章主要介紹了Android編程中PopupWindow的用法,結(jié)合實(shí)例形式分析了PopupWindow控件位置、動(dòng)畫(huà)、焦點(diǎn)等操作相關(guān)技巧,需要的朋友可以參考下2017-02-02