新聞中心
注意:無特殊說明,F(xiàn)lutter版本及Dart版本如下:Flutter版本: 1.12.13+hotfix.5Dart版本: 2.7.0
Stack
Stack組件可以將子組件疊加顯示,根據(jù)子組件的順利依次向上疊加,用法如下:
Stack(
children: [
Container(
height: 200,
width: 200,
color: Colors.red,
),
Container(
height: 170,
width: 170,
color: Colors.blue,
),
Container(
height: 140,
width: 140,
color: Colors.yellow,
)
],
)
效果如下:
Stack未定位的子組件大小由fit
參數(shù)決定,默認(rèn)值是StackFit.loose
,表示子組件自己決定,StackFit.expand
表示盡可能的大,用法如下:
Stack(
fit: StackFit.expand,
...
)
Stack未定位的子組件的默認(rèn)左上角對齊,通過alignment
參數(shù)控制,用法如下:
Stack(
alignment: Alignment.center,
...
)
效果如下:
有沒有注意到fit
和alignment
參數(shù)控制的都是未定位的子組件,那什么樣的組件叫做定位的子組件?使用Positioned包裹的子組件就是定位的子組件,用法如下:
Stack(
alignment: Alignment.center,
children: [
Container(
height: 200,
width: 200,
color: Colors.red,
),
Positioned(
left: 10,
right: 10,
bottom: 10,
top: 10,
child: Container(
color: Colors.green,
),
)
],
)
Positioned組件可以指定距Stack各邊的距離,效果如下:
如果子組件超過Stack邊界由overflow
控制,默認(rèn)是裁剪,下面設(shè)置總是顯示的用法:
Stack(
overflow: Overflow.visible,
children: [
Container(
height: 200,
width: 200,
color: Colors.red,
),
Positioned(
left: 100,
top: 100,
height: 150,
width: 150,
child: Container(
color: Colors.green,
),
)
],
)
效果如下:
IndexedStack
IndexedStack是Stack的子類,Stack是將所有的子組件疊加顯示,而IndexedStack只顯示指定的子組件,用法如下:
IndexedStack(
index: _index,
children: [
Center(
child: Container(
height: 300,
width: 300,
color: Colors.red,
alignment: Alignment.center,
child: Icon(
Icons.fastfood,
size: 60,
color: Colors.blue,
),
),
),
Center(
child: Container(
height: 300,
width: 300,
color: Colors.green,
alignment: Alignment.center,
child: Icon(
Icons.cake,
size: 60,
color: Colors.blue,
),
),
),
Center(
child: Container(
height: 300,
width: 300,
color: Colors.yellow,
alignment: Alignment.center,
child: Icon(
Icons.local_cafe,
size: 60,
color: Colors.blue,
),
),
),
],
)
通過點(diǎn)擊按鈕更新_index
值,代碼如下:
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.fastfood),
onPressed: () {
setState(() {
_index = 0;
});
},
),
IconButton(
icon: Icon(Icons.cake),
onPressed: () {
setState(() {
_index = 1;
});
},
),
IconButton(
icon: Icon(Icons.local_cafe),
onPressed: () {
setState(() {
_index = 2;
});
},
),
],
)
效果如下:
Positioned
Positioned用于定位Stack子組件,Positioned必須是Stack的子組件,基本用法如下:
Stack(
children: [
Positioned(
left: 10,
right: 10,
top: 10,
bottom: 10,
child: Container(color: Colors.red),
),
],
)
效果如下:
相關(guān)說明:
- 提供
top
、bottom
、left
、right
四種定位屬性,分別表示距離上下左右的距離。 - 只能用于Stack組件中。
left
、right
和width
3個(gè)參數(shù)只能設(shè)置其中2個(gè),因?yàn)樵O(shè)置了其中2個(gè),第三個(gè)已經(jīng)確定了,同理top
、bottom
和height
也只能設(shè)置其中2個(gè)。
Positioned提供便捷的構(gòu)建方式,比如Positioned.fromRect
、Positioned.fill
等,這些便捷的構(gòu)建方式萬變不離其宗,只不過換了一種方式設(shè)置top
、bottom
、left
、right
四種定位屬性。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站題目:Flutter疊加組件Stack的使用方法-創(chuàng)新互聯(lián)
文章路徑:http://www.dlmjj.cn/article/deccgj.html