一、簡介css
二、構造函數bash
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,
})
複製代碼
三、例子ide
decoration能夠實現陰影和邊框的效果函數
Widget _buildColumn() {
return Container(
constraints: BoxConstraints.expand(
height: 200,
),
padding: EdgeInsets.all(8.0),
child: Text("Hello World"),
alignment: Alignment.center,
transform: Matrix4.identity(),
margin: EdgeInsets.all(16.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 2.0),
color: Colors.blue[200],
borderRadius: BorderRadius.circular(20.0)
),
foregroundDecoration: BoxDecoration(),
height: 200,
width: 100,
//color: Colors.blue[200],
);
}
複製代碼
一、簡介佈局
二、構造函數動畫
const Padding({
Key key,
@required this.padding,
Widget child,
})
複製代碼
三、例子ui
Widget _buildColumn() {
return Padding(
padding: EdgeInsets.all(8.0),
child: Container(
height: 200,
width: 200,
color: Colors.blue[200],
),
);
}
複製代碼
一、簡介this
二、構造函數spa
const Align({
Key key,
this.alignment: Alignment.center,
this.widthFactor,
this.heightFactor,
Widget child
})
複製代碼
三、例子.net
因爲設置了heightFactor和widthFactor,表示父控件的寬高最大應該是子控件的2.0倍。若是不設置heightFactor和widthFactor,則父控件則無限大,則子控件會在屏幕底部中點
Widget _buildColumn() {
return Container(
color: Colors.blue[200],
child: Align(
heightFactor: 2.0,
widthFactor: 2.0,
alignment: Alignment(0.0, 1.0), //底部中點對齊
child: Text("Hello!"),
),
);
}
複製代碼
一、簡介
alignment
屬性設置爲居中二、構造函數
class Center extends Align {
const Center({ Key key, double widthFactor, double heightFactor, Widget child })
: super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
}
複製代碼
三、例子
因爲設置了heightFactor和widthFactor,表示父控件的寬高最大應該是子控件的1.5倍,因此父控件大小是300x300,則子控件就會在父控件的基礎上居中顯示。若是不設置heightFactor和widthFactor,則父控件則無限大,填充屏幕,則Center組件就在父控件的居中顯示,即屏幕中間
Widget _buildColumn() {
return Container(
color: Colors.blueGrey,
child: Center(
heightFactor: 1.5,
widthFactor: 1.5,
child: Container(
height: 200,
width: 200,
color: Colors.blue[200],
),
),
);
}
複製代碼
一、簡介
二、構造函數
const FittedBox({
Key key,
this.fit: BoxFit.contain,
this.alignment: Alignment.center,
Widget child,
})
複製代碼
填充模式
三、例子
經常使用的就是對圖片的裁剪,也能夠是對widget的裁剪,只要widget有固定的大小
Widget _buildColumn() {
return Container(
width: 300,
height: 300,
color: Colors.blue,
child: FittedBox(
fit: BoxFit.cover,
alignment: Alignment.centerLeft,
child: Image.asset("images/day27/girl.jpg"),
),
);
}
複製代碼
一、簡介
二、構造函數
const AspectRatio({
Key key,
@required this.aspectRatio,
Widget child
})
複製代碼
三、例子
因爲高度200是肯定的,寬度是不肯定的,而子控件的縮放比是1.5,最後子控件的大小寬爲300,高爲200
Widget _buildColumn() {
return Container(
height: 200.0,
color: Colors.blue,
child: AspectRatio(
aspectRatio: 1.5,
child: Container(
color: Colors.red,
),
),
);
}
複製代碼
一、簡介
二、構造函數
ConstrainedBox({
Key key,
@required this.constraints,
Widget child
})
複製代碼
三、例子
約束子控件最小必須是100x100,最大必須是150x150
Widget _buildColumn() {
return ConstrainedBox(
constraints: BoxConstraints(
minWidth: 100.0,
minHeight: 100.0,
maxWidth: 150.0,
maxHeight: 150.0,
),
child: Container(
width: 200.0,
height: 200.0,
color: Colors.red,
),
);
}
複製代碼
一、簡介
二、構造函數
const Baseline({
Key key,
@required this.baseline,
@required this.baselineType,
Widget child
})
複製代碼
三、例子
因爲文字是具備baseline屬性的,經過例子看出沒有baseline屬性的話,只能根據child的bottom對齊,文字則會在baseline上
Widget _buildColumn() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Baseline(
baselineType: TextBaseline.alphabetic,
child: Text("Java",style: TextStyle(fontSize: 20.0),),
baseline: 50.0,
),
Baseline(
//alphabetic:對齊字符底部的水平線
//ideographic:對齊表意字符的水平線
baselineType: TextBaseline.alphabetic,
child: Container(
width: 30,
height: 30,
color: Colors.blue[200],
),
baseline: 50.0,
),
Baseline(
baselineType: TextBaseline.alphabetic,
child: Text("iOS",style: TextStyle(fontSize: 30.0),),
baseline: 50.0,
),
],
);
}
複製代碼
一、簡介
二、構造函數
const FractionallySizedBox({
Key key,
this.alignment = Alignment.center,
this.widthFactor,
this.heightFactor,
Widget child,
})
複製代碼
三、例子
不須要指定子控件的大小,經過FractionallySizedBox
約束子控件的大小爲父控件的一半
Widget _buildColumn() {
return Container(
color: Colors.blue[600],
width: 200,
height: 200,
child: FractionallySizedBox(
alignment: Alignment.center,
widthFactor: 0.5,
heightFactor: 0.5,
child: Container(
color: Colors.blue[200],
),
),
);
}
複製代碼
一、簡介
二、構造函數
const IntrinsicHeight({ Key key, Widget child })
複製代碼
三、例子
在IntrinsicHeight
子控件中放置四個相同寬度的容器,設置其中兩個容器的高度爲150和60,另外兩個不設置,IntrinsicHeight
會找到最大的高度去限制其他容器
Widget _buildColumn() {
return IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
color: Colors.blue,
width: 40.0,
height: 150.0,
),
Container(
color: Colors.red,
width: 40.0,
height: 60.0,
),
Container(color: Colors.yellow, width: 40.0),
Container(color: Colors.yellow, width: 40.0),
],
),
);
}
複製代碼
一、簡介
二、構造函數
const IntrinsicWidth({ Key key, this.stepWidth, this.stepHeight, Widget child })
複製代碼
三、例子
設置stepHeight爲200,可是父控件的高度不止200,該值不生效。設置stepWidth爲150,可是其餘child的控件最大寬度不超過150,則最小的寬度爲150,即圖中的灰色背景
Widget _buildColumn() {
return Container(
color: Colors.grey,
child: IntrinsicWidth(
//父容器的大小
stepHeight: 200,
stepWidth: 150,
child: Column(
children: <Widget>[
Container(
color: Colors.blue,
width: 100.0,
height: 150.0,
),
Container(
color: Colors.red,
width: 60.0,
height: 150.0,
),
Container(color: Colors.yellow, height: 150.0),
Container(color: Colors.green, height: 150.0),
],
),
),
);
}
複製代碼
一、簡介
二、構造函數
const LimitedBox({
Key key,
this.maxWidth = double.infinity,
this.maxHeight = double.infinity,
Widget child,
})
複製代碼
三、例子
因爲是Row組件,Row組件的高度是被肯定的,寬度是不限制的,因此高度限制不起做用,寬度限制起做用,最後寬度是150,高度是250
Widget _buildColumn() {
return Row(
children: <Widget>[
LimitedBox(
maxHeight: 150, //因爲是Row,高度被肯定,因此高度限制不起做用
maxWidth: 150, //因爲是Row,寬度不限制,因此寬度限制起做用
child: Container(
width: 250,
height: 250,
color: Colors.redAccent,
),
)
],
);
}
複製代碼
一、簡介
二、構造函數
const Offstage({ Key key, this.offstage = true, Widget child })
複製代碼
三、例子
Widget _buildColumn() {
return Offstage(
offstage: false,
child: Container(
width: 250,
height: 250,
color: Colors.redAccent,
),
);
}
複製代碼
一、簡介
二、構造函數
const OverflowBox({
Key key,
this.alignment = Alignment.center,
this.minWidth,
this.maxWidth,
this.minHeight,
this.maxHeight,
Widget child,
})
複製代碼
三、例子
子控件必須符合最小高度,因此最後出來的大小是300x600
Widget _buildColumn() {
return OverflowBox(
minHeight: 600,
child: Container(
width: 300,
height: 300,
color: Colors.blueAccent,
),
);
}
複製代碼
一、簡介
二、構造函數
const SizedBox({ Key key, this.width, this.height, Widget child })
複製代碼
三、例子
Widget _buildColumn() {
return SizedBox(
width: 200,
height: 200,
child: Container(
color: Colors.blueAccent,
),
);
}
複製代碼
一、簡介
二、構造函數
const SizedOverflowBox({
Key key,
@required this.size,
this.alignment = Alignment.center,
Widget child,
})
複製代碼
三、例子
控制child的尺寸大小爲100x100
Widget _buildColumn() {
return SizedOverflowBox(
size: Size(100, 100),
alignment: Alignment.center,
child: Container(
width: 300,
height: 300,
color: Colors.blueAccent,
),
);
}
複製代碼
一、簡介
二、構造函數
const Transform({
Key key,
@required this.transform,
this.origin,
this.alignment,
this.transformHitTests = true,
Widget child,
})
複製代碼
三、例子
讓容器輕微的偏移上方,而後作居於自身的旋轉效果
Widget _buildColumn() {
return Transform(
origin: Offset(0.0, 10.0),
transformHitTests: true,
alignment: Alignment.center,
transform: Matrix4.rotationZ(3.0),
child: Container(
width: 100,
height: 100,
color: Colors.blueAccent,
),
);
}
複製代碼
一、簡介
二、構造函數
const CustomSingleChildLayout({
Key key,
@required this.delegate,
Widget child
})
複製代碼
三、例子
須要先建立一個代理類,將傳遞進來的參數Size做爲預期尺寸,若是預期尺寸和當前子控件的尺寸不符合,則會從新讓子控件設置新的約束來適應預期Size的尺寸
class FixedSizeLayoutDelegate extends SingleChildLayoutDelegate {
FixedSizeLayoutDelegate(this.size);
// 預期尺寸
final Size size;
@override
Size getSize(BoxConstraints constraints) => size;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
// 從新設置子控件的約束,讓他適應預期Size的尺寸
return BoxConstraints.tight(size);
}
@override
bool shouldRelayout(FixedSizeLayoutDelegate oldDelegate) {
// 若是預期尺寸和當前子控件的尺寸不符合,則從新layout
return size != oldDelegate.size;
}
}
複製代碼
因爲預期的尺寸是300x300,當前尺寸和預期不匹配,因此會將子控件調整到預期的尺寸
Widget _buildColumn() {
return CustomSingleChildLayout(
delegate: FixedSizeLayoutDelegate(Size(300, 300)),
child: Container(
width: 100,
height: 100,
color: Colors.blueAccent,
),
);
}
複製代碼
![]() Hensen_ |