*、assets
當引用圖片的時候,需要在pubspec.yaml的文件中的flutter下添加assets,類似于下面的樣子:這里需要注意的是文件里的assets只要一個縮進即和flutter里的內容保持對齊,否則,會出問題。我遇到的是,沒辦法選擇運行設備。
一、Layout原理
Flutter布局Layout的核心就是Widget。在Flutter里,基本上任何東西都是Widget,甚至布局的模型。比如images、icon、text等你能在界面上看到的。你看不到的,如Row、Column等也是Widget。
復雜的Widget都是有單個widget組成的。
下面是幾個常用Widget
- Container:
容器Widget,允許自己定制一些子widget。其初始化如下:
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背景色等等。
-
Row:
定義一行中的所有Widget和這些Widget的展示方式,包括主軸方向和副軸方向,具體的定義如下,其中MainAxisAlignment表示主軸方向(水平方向),CrossAxisAlignment表示副軸方向(和主軸垂直,即垂直方向):
row-diagram.png
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:
定義一列中的所有Widget和這些Widget的展示方式,也有主軸和副軸兩個方向,具體的和Row相同,其定義如下:
column-diagram.png
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>[],
})
ps:主軸和副軸對于熟悉RN的人應該比較好理解
二、Layout Widget(Container、MaterialApp、Scaffold)
這里通過一個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、背景色和標題等,所有的內容都需要自定義。
注意??:
1、Scaffold必須放在MaterialApp里面,否則會報錯,大致如下:
══╡ 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.
2、非Material下Text的textDirection屬性是必須的
??????到底什么是Material App?
三、在水平和垂直方向上Layout(Row、Column)
1、水平布局:
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,
);
}
}
2、垂直布局
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)
使用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
總體來說,所有的Widget可以分為兩類:Standard Widget和Material Component。Standard Widget可以在Material App和非Material App中使用,但是Material Component只能在Material App中使用。
1、Standard Widget
常用的是下面四種:
- Container
為Widget添加內邊距padding, 外邊距margins, 邊框borders, 背景色background color和其他的一些修飾信息。 - GridView
將其中的Widget按照Grid的形式展示,并且是可滑動的。(似乎和UICollectionView相似)。 - ListView
將其中的Widget按照List形式展示,并且是可滑動的。(似乎和UIScrollView、UITableView相似)。 - Stack
在Widget上覆蓋一個Widget
2、Material Component
- Card
- ListTitle