Flutter 頁面層疊組件與定位組件

Flutter Stack組件

Stack表示堆的意思,我們可以用Stack或者Stack結(jié)合Align或者Stack結(jié)合Postitiond來實(shí)現(xiàn)頁面的定位布局

屬性 說明
alignment 配置所有子元素的顯示位置
children 子組件

Stack組件,可以讓組件下children內(nèi)的所有組件進(jìn)行堆疊.再通過alignment屬性來設(shè)置堆疊布局的位置
簡單看一個(gè)Stack堆疊

import 'package:flutter/material.dart';
class StackPage extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'stack page',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Stack Page')
        ),
        body: PageWidget()
      ),
    );
  }
}

class PageWidget extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,  //- Stack 下的所有元素都以中心對(duì)齊堆疊
      children: <Widget>[
        Container(
          width:300.0,
          height:400.0,
          color: Colors.orangeAccent,
        ),
        Container(
          width:100.0,
          height:150.0,
          color: Colors.pinkAccent
        ),
        Text('我是文本'),  //- 文本在最下面加載,才能夠顯示在最上面,如果順序不同,可能文字被其他區(qū)塊遮擋
      ],
    );
  }
}

頁面效果:

image.png

上面的demo中,我們使用了一個(gè)關(guān)鍵屬性alignment,具體排列可以參考Alignment下的方位屬性.當(dāng)然我們也可以不使用Alignment的方位屬性,可以使用Alignment指定方位.需要直接實(shí)例化Alignment()這個(gè)類,傳入X,Y的值.區(qū)間為-1 到 1.對(duì)應(yīng)X軸和Y軸的坐標(biāo)位置.

class PageWidget extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment(1,1),  //- Alignment()實(shí)例化類傳入XY的坐標(biāo),區(qū)間為-1 到 1
      children: <Widget>[
        Container(
          width:300.0,
          height:400.0,
          color: Colors.orangeAccent,
        ),
        Container(
          width:100.0,
          height:150.0,
          color: Colors.pinkAccent
        ),
        Text('我是文本'),  //- 文本在最下面加載,才能夠顯示在最上面,如果順序不同,可能文字被其他區(qū)塊遮擋
      ],
    );
  }
}

我們?cè)O(shè)置Alignment實(shí)例化類里傳入1,1對(duì)應(yīng)為X軸和Y軸最大值.那么應(yīng)該是右下角.手機(jī)上顯示為:

image.png

容器內(nèi)有多個(gè)元素需要定位

我們?cè)谑褂?code>Stack組件進(jìn)行子組件布局的時(shí)候,沒法對(duì)單一組件進(jìn)行定位.這個(gè)時(shí)候我們就需要使用Stack結(jié)合Align或者Positioned屬性進(jìn)行實(shí)現(xiàn).

Stack配合Align實(shí)現(xiàn)單一子組件定位:
屬性 說明
alignment 配置所有子元素的顯示位置
child 子組件
class PageWidget extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Container(
      height:400.0,
      width: 300.0,
      color:Colors.orangeAccent,
      child: Stack(
        children: <Widget>[
          Align(
            alignment: Alignment.topCenter,
            child: Icon(Icons.home, size: 40, color: Colors.white),
          ),
          Align(
            alignment: Alignment(0.7, 1),
            child: Icon(Icons.search, size: 30, color: Colors.blue),
          ),
          Align(
            alignment: Alignment(0, 0),
            child: Icon(Icons.bubble_chart, size: 80, color: Colors.red),
          ),
          Align(
            alignment: Alignment.centerLeft,
            child: Container(
              width:80.0,
              height:160.0,
              color: Colors.blueGrey
            ),
          ) 
        ],
      ),
    );
  }
}

實(shí)現(xiàn)效果:


image.png
Stack配合Align實(shí)現(xiàn)單一子組件定位:
屬性 說明
top 子元素距離頂部的距離
bottom 子元素距離底部的距離
left 子元素距離左邊的距離
right 子元素距離右邊的距離
child 子組件

代碼:

class PageWidget extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Container(
      height:400.0,
      width: 300.0,
      color:Colors.orangeAccent,
      child: Stack(
        children: <Widget>[
          Positioned(
            left:10,
            child: Icon(Icons.home, size: 40, color: Colors.white),
          ),
          Positioned(
            bottom: 50,
            child: Icon(Icons.search, size: 30, color: Colors.blue),
          ),
          Positioned(
            bottom: 100,
            child: Icon(Icons.bubble_chart, size: 80, color: Colors.red),
          ),
          Positioned(
            left:50,
            top:50,
            child: Container(
              width:80.0,
              height:160.0,
              color: Colors.blueGrey
            ),
          ) 
        ],
      ),
    );
  }
}

頁面效果:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容