這裏須要注意的是文件裏的assets只要一個縮進即和flutter裏的內容保持對齊,不然,會出問題。我遇到的是,沒辦法選擇運行設備。php
Flutter佈局Layout的核心就是Widget。在Flutter裏,基本上任何東西都是Widget,甚至佈局的模型。好比images、icon、text等你能在界面上看到的。你看不到的,如Row、Column等也是Widget。
複雜的Widget都是有單個widget組成的。java
Container({
Key key,
this.alignment, this.padding, Color color, Decoration decoration, this.foregroundDecoration, double width, double height, BoxConstraints constraints, this.margin, this.transform, })
能夠發現,該UI Widget能夠控制其中的Widget的padding、margin以及當前widget的寬高、border背景色等等。python
Row:
app
Row({
Key key,
MainAxisAlignment mainAxisAlignment: MainAxisAlignment.start,
MainAxisSize mainAxisSize: MainAxisSize.max,
CrossAxisAlignment crossAxisAlignment: CrossAxisAlignment.center,
TextDirection textDirection,
VerticalDirection verticalDirection: VerticalDirection.down,
TextBaseline textBaseline,
List<Widget> children: const <Widget>[], })
Column:
ide
Column({
Key key,
MainAxisAlignment mainAxisAlignment: MainAxisAlignment.start,
MainAxisSize mainAxisSize: MainAxisSize.max,
CrossAxisAlignment crossAxisAlignment: CrossAxisAlignment.center,
TextDirection textDirection,
VerticalDirection verticalDirection: VerticalDirection.down,
TextBaseline textBaseline,
List<Widget> children: const <Widget>[], })
這裏經過一個HelloWorld app來展現如何建立一個Widget並展現出來,並區分Material和非Material環境。
hello_word.dart裏的代碼以下:佈局
class HelloWorld { static Widget helloWorld() { return new MaterialApp( title: 'Hello World', theme: new ThemeData( primarySwatch: Colors.blue, ), home: new Scaffold( appBar: new AppBar(title: new Text('Hello World')), body: new Center( child: new Text( "Hello World", style: new TextStyle(fontSize: 32.0), )), ), ); } static Widget hellWorldWithoutMaterial() { return new Container( decoration: new BoxDecoration(color: Colors.white), child: new Center( child: new Text( 'Hello World', textDirection: TextDirection.ltr, // 這一句必須有 style: new TextStyle(color: Colors.black, fontSize: 40.0), ), ), ); } }
而在展現上,主要的區別在於非Material下,沒有Appbar、背景色和標題等,全部的內容都須要自定義。測試
一、Scaffold必須放在MaterialApp裏面,不然會報錯,大體以下:flex
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Scaffold(dirty, state: ScaffoldState#6f74d): No MediaQuery widget found. Scaffold widgets require a MediaQuery widget ancestor. The specific widget that could not find a MediaQuery ancestor was: Scaffold The ownership chain for the affected widget is: Scaffold ← MyApp ← [root] Typically, the MediaQuery widget is introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree.
二、非Material下Text的textDirection屬性是必須的ui
??????到底什麼是Material App?this
class LayoutImagesH { static layoutImagesH() { MaterialApp material = new MaterialApp( home: new Scaffold( appBar: new AppBar( title: new Text( "Images H Layout", style: new TextStyle(color: Colors.white, fontSize: 20.0), ), ), body: _getLayoutContainer(), ), ); return material; } static _getLayoutContainer() { Row row = new Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ _getRowsImage('images/pic1.jpg'), _getRowsImage('images/pic2.jpg'), _getRowsImage('images/pic3.jpg') ], ); Container container = new Container( padding: const EdgeInsets.all(15.0), color: Colors.grey, child: row, ); return container; } static _getRowsImage(imageStr) { return new Image.asset( imageStr, width: 100.0, ); } }
class LayoutImagesV { static layoutImagesVSpaceEvenly() { return new Container( color: Colors.green, child: new Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ new Image.asset('images/pic1.jpg'), new Image.asset('images/pic2.jpg'), new Image.asset('images/pic3.jpg'), ], ), ); } static layoutImagesVSpaceAround() { return new Container( color: Colors.green, child: new Column( mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ new Image.asset('images/pic1.jpg'), new Image.asset('images/pic2.jpg'), new Image.asset('images/pic3.jpg'), ], ), ); } static layoutImagesVSpaceBetween() { return new Container( color: Colors.green, child: new Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ new Image.asset('images/pic1.jpg'), new Image.asset('images/pic2.jpg'), new Image.asset('images/pic3.jpg'), ], ), ); } }
主軸的枚舉以下:
enum MainAxisAlignment { start, end, center, /// 第一個和最後一個貼邊,中間的平分 spaceBetween, /// 第一個和最後一個距離邊的距離是它與中間的距離的一半 spaceAround, /// 徹底平分 spaceEvenly, }
使用Expanded Widget,它繼承自Flexible,主要是經過其中flex屬性來控制,默認整個Expanded裏的flex都是1,即平分。咱們能夠經過控制flex來控制每一個Widget所佔的大小。
通過嘗試,你會發現,只要總尺寸超過了容器的限制,就會出現這種問題。要解決這種問題,目前來看,一是控制圖片的尺寸,一個就是使用Expanded 的Widget。
代碼以下:
class ExpandedImages { static expandedImages() { return new Container( color: Colors.green, // margin: const EdgeInsets.all(15.0), child: new Row( textDirection: TextDirection.ltr, // mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: _getRowChildren(), ), ); } static expandImagesWithMaterial() { return new MaterialApp( home: new Scaffold( appBar: new AppBar( title: new Text('Expanded') ), body: new Center( child: new Row( crossAxisAlignment: CrossAxisAlignment.center, children: _getRowChildren(), ), ) ), ); } static _getRowChildren() { return [ // new Expanded(child: new Image.asset("images/pic1.jpg")), // new Expanded(flex:2, child: new Image.asset("images/pic2.jpg")), // new Expanded(child: new Image.asset("images/pic3.jpg")) new Expanded(child: new Image.asset("images/expand1.jpg")), new Expanded(flex:2, child: new Image.asset("images/expand2.jpg")), new Expanded(child: new Image.asset("images/expand3.jpg")) ]; } }
最後的效果以下圖所示:
看到這裏其實也明白了,上面的水平方向和垂直方向的佈局是有問題的。
通常的Row和Column的佈局都是儘量大的利用Space,若是此時不想要這些space,那麼可使用相應的mainAxisSize的值來控制,它只有min和max兩個值,默認是max,即咱們看見的那種狀況。若是設置了該值爲min,那麼mainAxisAlignment的後面的幾個值對其再也不起做用。
下面是測試代碼:
class _MyHomePageState extends State<_MyHomePage> { @override Widget build(BuildContext context) { // TODO: implement build return new Scaffold( appBar: new AppBar(title: new Text("Packing Widget")), body: new Center( child: new Row( mainAxisAlignment: MainAxisAlignment.spaceAround, mainAxisSize: MainAxisSize.min, // crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ new Icon(Icons.star, color: Colors.green[500]), new Icon(Icons.star, color: Colors.green[500]), new Icon(Icons.star, color: Colors.green[500]), new Icon(Icons.star, color: Colors.grey), new Icon(Icons.star, color: Colors.grey), ], ), ) ); } }
整體來講,全部的Widget能夠分爲兩類:Standard Widget和Material Component。Standard Widget能夠在Material App和非Material App中使用,可是Material Component只能在Material App中使用。
經常使用的是下面四種: