俗話說知己知彼百戰百勝,如果對Flutter 里面的各種Widgets不了解,那你就別想將它們組合成你想要的效果。從今天開始。會把一個一個的widget 擼一遍。。知道它大概的用法。效果。當你想做某個效果的時候。腦袋里面能第一時間想到它。
Sample可以在這里找到代碼 Sampe code Github
Container 作為最常用的內容widget
margin,padding, color(background),width,height,children 這些屬性很好理解。
new Container( margin: const EdgeInsets.all(10.0), color: const Color(0xFF00FF00), width: 48.0, height: 48.0, child: new Text("Hello Flutter"), padding: const EdgeInsets.only(left: 6.0), );
constraints 對Container大小的約束。他會結合width,height進行處理,后面在Flutter wideget 是怎么布局構造中詳細講解
foregroundDecoration 一個前置的裝飾器。哈哈把我的Text 擋住了。
transform 變形
new Container( constraints: new BoxConstraints.expand( height: Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0, ), padding: const EdgeInsets.all(8.0), color: Colors.teal.shade700, alignment: Alignment.center, child: new Text('Hello World', style: Theme.of(context).textTheme.display1.copyWith(color: Colors.white)), foregroundDecoration: new BoxDecoration( image: new DecorationImage( image: new NetworkImage('http://p0.so.qhimgs1.com/bdr/200_200_/t011fa0ccaa6d22240c.jpg'), centerSlice: new Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0), ), ), transform: new Matrix4.rotationZ(0.1), );
Row/Column 常用的多內容Widget,可以將Child 按照水平/垂直的方式(跟我了解的完全相反。。哈哈哈好尷尬)布局,它們都繼承於Flex.
Row和Column差別是設置了不同的flex-direction.
Flex通過direction設置了flex的主軸方向即main axis。和主軸垂直的方向叫做cross axis。flex布局中對子布局的控制是從main axis 和cross axis兩個方向上進行的.
Container( color: Colors.red, width: double.infinity, child:new Column( verticalDirection: VerticalDirection.up, mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ new Text('Deliver features faster', textAlign: TextAlign.center), new Text('Craft beautiful UIs', textAlign: TextAlign.center), new FittedBox( fit: BoxFit.contain, // otherwise the logo will be tiny child: const FlutterLogo(), ), ], ) , );
一圖流
Image /FadeInImage 帶占位符
默認是這幾種方式。支持GIF格式,如果你想寫一個緩存本地硬盤的Image。你可以重寫ImageProvider.這里推薦一下好用的插件flutter_advanced_networkimage
對於默認的Image是有做內存緩存,默認是1000.
const int _kDefaultSize = 1000; const int _kDefaultSizeBytes = 100 << 20; // 100 MiB//清除
PaintingBinding.instance.imageCache.clear();
//緩存張數
PaintingBinding.instance.imageCache.maximumSize=500;
//緩存大小
PaintingBinding.instance.imageCache.maximumSizeBytes=1000;
fit | Description | Result |
---|---|---|
BoxFit.fill | 全圖顯示,顯示可能拉伸,充滿 | ![]() |
BoxFit.contain | 全圖顯示,顯示原比例,不需充滿 | ![]() |
BoxFit.cover | 顯示可能拉伸,可能裁剪,充滿 | ![]() |
BoxFit.fitWidth | 顯示可能拉伸,可能裁剪,寬度充滿 | ![]() |
BoxFit.fitHeight | 顯示可能拉伸,可能裁剪,高度充滿 | ![]() |
BoxFit.none | ||
BoxFit.scaleDown | 效果和contain差不多,但是此屬性不允許顯示超過源圖片大小,可小不可大 | ![]() |