<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
flutter 開發中最核心的是用最少的元件(層次)完成功能開發;Container 前端的盒子模型實現,類似 div 標籤,掌握其他元件前,深入學習理解 Container 的使用是必要的。
Container
是一個組合類容器,由DecoratedBox
、ConstrainedBox、Transform
、Padding
、Align
等元件組合的一個多功能容器,所以我們只需通過一個Container
元件可以實現同時需要裝飾、變換、約束限制的場景。
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'), ), ), ), ); }
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, //卡片文字對齊 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); // 子項 final Widget? child; // 內邊距 final EdgeInsetsGeometry? padding; // 背景色,和 decoration 只能二選一 final Color? color; // 背景裝飾 final Decoration? decoration; // 前景裝飾(在 child 之上) final Decoration? foregroundDecoration; // 約束條件 final BoxConstraints? constraints; // 外邊距 final EdgeInsetsGeometry? margin; // 變化 final Matrix4? transform; // 變化之後 child 對齊方式 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!; } ... }
原始碼很簡單,元件層層包裹,但是我們必須清楚每一個引數的作用和使用場景。它可以幫你用更少的層級完成功能,這很重要。
以上就是Flutter系列重學Container範例詳解的詳細內容,更多關於Flutter重學Container的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45