本节目标
- 基础用法
- Padding 和 Margin
- BoxDecoration 装饰
- BoxShadow 阴影
- RadialGradient 渐变
基础用法
Container 是一个组合类容器,它本身不对应具体的 RenderObject,它是 DecoratedBox、ConstrainedBox、Transform、Padding、Align 等组件组合的一个多功能容器,所以我们只需通过一个 Container 组件可以实现同时需要装饰、变换、限制的场景。下面是 Container 的定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| Container({ Key key, this.alignment, this.padding, Color color, Decoration decoration, this.foregroundDecoration, double width, double height, BoxConstraints constraints, this.margin, this.transform, this.child, })
|
BoxDecoration 装饰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const BoxDecoration({ this.color, this.image, this.border, this.borderRadius, this.boxShadow, this.gradient, this.backgroundBlendMode, this.shape = BoxShape.rectangle, })
|
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| Container( constraints: BoxConstraints.expand( height: 200.0, ), margin: const EdgeInsets.all(20.0), padding: const EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: Text('Hello World', style: Theme.of(context) .textTheme .display1 .copyWith(color: Colors.white)),
decoration: BoxDecoration( color: Colors.blueAccent, gradient: RadialGradient( colors: [Colors.red, Colors.orange], center: Alignment.topLeft, radius: .98, ), boxShadow: [ BoxShadow( blurRadius: 2, offset: Offset(0, 2), color: Colors.blue, ), ], backgroundBlendMode: BlendMode.color, shape: BoxShape.circle, ),
),
|
代码
https://github.com/ducafecat/flutter-learn/blob/master/container_widget
参考
© 猫哥
https://ducafecat.tech
邮箱 ducafecat@gmail.com / 微信 ducafecat / 留言板 disqus