|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +/// 盒子模型demo |
| 4 | +/// 这里主要是盒子模型的api用法 |
| 5 | +void main() { |
| 6 | + runApp( |
| 7 | + new MaterialApp( |
| 8 | + title: 'BoxDemo', |
| 9 | + theme: new ThemeData( |
| 10 | + primarySwatch: Colors.blue, //设置全局主题 |
| 11 | + ), |
| 12 | + home: new BoxDemo(), |
| 13 | + ), |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +class BoxDemo extends StatelessWidget { |
| 18 | + @override |
| 19 | + Widget build(BuildContext context) { |
| 20 | + return new Scaffold( |
| 21 | + appBar: new AppBar( |
| 22 | + title: new Text('BoxDemo'), |
| 23 | + ), |
| 24 | + body: _buildBox2(), |
| 25 | + ); |
| 26 | + } |
| 27 | + |
| 28 | + //创建单个盒子,里面放一个text |
| 29 | + Widget _buildBox() { |
| 30 | + return new Container( |
| 31 | + //宽 |
| 32 | + width: 320.0, |
| 33 | + //高 |
| 34 | + height: 240.0, |
| 35 | + //背景颜色 |
| 36 | +// color: Colors.grey[300], |
| 37 | + margin: new EdgeInsets.all(50.0), |
| 38 | + padding: new EdgeInsets.all(80.0), |
| 39 | + alignment: Alignment.center, |
| 40 | + constraints: new BoxConstraints.expand(width: 300.0, height: 200.0), |
| 41 | + decoration: new BoxDecoration( |
| 42 | + borderRadius: new BorderRadius.all( |
| 43 | + //让矩形四个角都变成圆角 |
| 44 | + const Radius.circular(8.0), |
| 45 | + ), |
| 46 | + //垂直线性渐变 |
| 47 | + gradient: new LinearGradient( |
| 48 | + begin: const Alignment(0.0, -1.0), |
| 49 | + end: const Alignment(0.0, 0.6), |
| 50 | + colors: <Color>[const Color(0xffef5350), const Color(0x00ef5350)], |
| 51 | + ), |
| 52 | + //阴影 |
| 53 | + boxShadow: <BoxShadow>[ |
| 54 | + new BoxShadow( |
| 55 | + color: const Color(0xcc000000), |
| 56 | + offset: new Offset(0.0, 2.0), |
| 57 | + blurRadius: 4.0, |
| 58 | + ), |
| 59 | + new BoxShadow( |
| 60 | + color: const Color(0x80000000), |
| 61 | + offset: new Offset(0.0, 6.0), |
| 62 | + blurRadius: 20.0, |
| 63 | + ), |
| 64 | + ], |
| 65 | + ), |
| 66 | + child: new Text( |
| 67 | + "单个盒子", |
| 68 | + style: _buildTextStyle(), |
| 69 | + ), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // 旋转组件 在控件外层加一个Transform控件 |
| 74 | + Widget _buildBox2() { |
| 75 | + return new Container( |
| 76 | + child: new Center( |
| 77 | + child: new Transform( |
| 78 | + child: new Container( |
| 79 | + child: new Text( |
| 80 | + "旋转控件", |
| 81 | + style: _buildTextStyle(), |
| 82 | + textAlign: TextAlign.center, |
| 83 | + ), |
| 84 | + decoration: new BoxDecoration( |
| 85 | + color: Colors.red[400], |
| 86 | + ), |
| 87 | + padding: new EdgeInsets.all(16.0), |
| 88 | + ), |
| 89 | + alignment: Alignment.center, |
| 90 | + transform: _buildLocation(), |
| 91 | + ), |
| 92 | + ), |
| 93 | + width: 320.0, |
| 94 | + height: 240.0, |
| 95 | + color: Colors.grey[300], |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + //盒子中间嵌套盒子 |
| 100 | + Widget _buildBoxInBox() { |
| 101 | + // 外层盒子 |
| 102 | + return new Container( |
| 103 | + child: new Center( |
| 104 | + //内层盒子 |
| 105 | + child: new Container( |
| 106 | + child: new Text( |
| 107 | + "盒子中嵌套盒子", |
| 108 | + style: _buildTextStyle(), |
| 109 | + ), |
| 110 | + decoration: new BoxDecoration( |
| 111 | + color: Colors.deepOrange, |
| 112 | + ), |
| 113 | + padding: new EdgeInsets.all(16.0), |
| 114 | + width: 240.0, //最大宽度是 240.0 |
| 115 | + ), |
| 116 | + ), |
| 117 | + width: 320.0, |
| 118 | + height: 240.0, |
| 119 | + color: Colors.blue[300], |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + // 设置一个圆形 |
| 124 | + Widget _buildRound() { |
| 125 | + return new Container( |
| 126 | + // red circle |
| 127 | + child: new Text( |
| 128 | + "Lorem ipsum", |
| 129 | + style: _buildTextStyle(), |
| 130 | + textAlign: TextAlign.center, |
| 131 | + ), |
| 132 | + decoration: new BoxDecoration( |
| 133 | + color: Colors.red[400], |
| 134 | + shape: BoxShape.circle, |
| 135 | + ), |
| 136 | + padding: new EdgeInsets.all(16.0), |
| 137 | + width: 160.0, |
| 138 | + height: 160.0, |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + //富文本的显示 |
| 143 | + Widget _buildRichText() { |
| 144 | + return new RichText( |
| 145 | + text: new TextSpan( |
| 146 | + style: _buildTextStyle(), |
| 147 | + children: <TextSpan>[ |
| 148 | + new TextSpan(text: "Lorem "), |
| 149 | + new TextSpan( |
| 150 | + text: "ipsum", |
| 151 | + style: new TextStyle( |
| 152 | + fontWeight: FontWeight.w300, |
| 153 | + fontStyle: FontStyle.italic, |
| 154 | + fontSize: 48.0, |
| 155 | + ), |
| 156 | + ), |
| 157 | + ], |
| 158 | + ), |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + //文本样式 |
| 163 | + TextStyle _buildTextStyle() { |
| 164 | + return new TextStyle( |
| 165 | + //文本大小 |
| 166 | + fontSize: 24.0, |
| 167 | + //字体粗细 |
| 168 | + fontWeight: FontWeight.w900, |
| 169 | + //字体 |
| 170 | + fontFamily: "Georgia", //...还有其他属性就不一一列举了 |
| 171 | + //间距 |
| 172 | + letterSpacing: 4.0, |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + // 设置操作位置 |
| 177 | + Matrix4 _buildLocation() { |
| 178 | + //旋转 |
| 179 | + // return new Matrix4.identity()..rotateZ(15 * 3.1415927 / 180); |
| 180 | + // 缩放 |
| 181 | +// return new Matrix4.identity()..scale(1.5); |
| 182 | + // 平移 |
| 183 | +// return new Matrix4.identity()..translate(10.0,10.0,0.0); |
| 184 | + } |
| 185 | +} |
0 commit comments