iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 23
0

本篇文章同步發表在 HKT 線上教室 部落格,線上影音教學課程已上架至 Udemy-HKT線上教室。想追蹤更多相關技術資訊,歡迎到 臉書粉絲專頁 按讚追蹤喔~

哈囉~大家好,我是 KT ,今天【iT邦幫忙鐵人賽】挑戰第二十三天,KT 將為大家來介紹,Stack 堆疊元件。

Stack 堆疊佈局

Stack 堆疊佈局,可以將兩個以上的元件,層次的堆疊起來。

Stack 堆疊佈局,常用的屬性

  • alignment: 對齊屬性
  • Positioned:位置屬性

起手式範例

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('HKT線上教室'),
          ),
          body: HomePage()),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        children: <Widget>[
          Container(
            color: Colors.amber,
            child: Text('111', style: TextStyle(fontSize: 100)),
          ),
          Container(
            color: Colors.red,
            child: Text('222', style: TextStyle(fontSize: 50)),
          )
        ],
      ),
    );
  }
}

範例:設定對齊屬性

新增一個對齊屬性,貼底置中(alignment 屬性預設:對齊左上角),之後可以再試著改其他對齊屬性,如:center、bottomRight...等。

alignment: Alignment.bottomCenter

執行畫面

完整程式碼

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('HKT線上教室'),
          ),
          body: HomePage()),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[
          Container(
            color: Colors.amber,
            child: Text('111', style: TextStyle(fontSize: 100)),
          ),
          Container(
            color: Colors.red,
            child: Text('222', style: TextStyle(fontSize: 50)),
          )
        ],
      ),
    );
  }
}

範例:設定位置屬性

設定「222」文字標籤,位置屬性,左移 10

Positioned(
        left: 10,
        child: Container(
          color: Colors.red,
          child: Text('222', style: TextStyle(fontSize: 50)),
        ))

執行畫面

完整程式碼

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('HKT線上教室'),
          ),
          body: HomePage()),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        alignment: Alignment.topLeft,
        children: <Widget>[
          Container(
            color: Colors.amber,
            child: Text('111', style: TextStyle(fontSize: 100)),
          ),
          Positioned(
              left: 10,
              child: Container(
                color: Colors.red,
                child: Text('222', style: TextStyle(fontSize: 50)),
              ))
        ],
      ),
    );
  }
}

那今天【iT邦幫忙鐵人賽】就介紹到這邊囉~

順帶一提,KT 線上教室,臉書粉絲團,會不定期發佈相關資訊,不想錯過最新資訊,不要忘記來按讚,加追蹤喔!也歡迎大家將這篇文章分享給更多人喔。

我們明天見囉!!!掰掰~

參考資料

HKT 線上教室
http://tw-hkt.blogspot.com/

Background vector created by freepik
https://www.freepik.com


上一篇
Day 22:Drawer 抽屜頁面導航元件
下一篇
Day 24:ListView 列表元件
系列文
Flutter 程式設計入門實戰 30 天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言