flutter学习(7)Stack组件

Flutter学习(7)Stack组件

一.Stack组件

flutter学习(7)Stack组件_第1张图片

  • demo

    class LayoutDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Stack(children: [
          Container(
            height: 400,
            width: 300,
            color: Colors.red,
          ),
          Text("如果不是Stack组件,那么这行文本应该显示在Container的下面")
        ]);
      }
    }
    
    • 效果

      flutter学习(7)Stack组件_第2张图片
    • 再往上堆叠一些东西

      比如文本

      import 'package:flutter/material.dart';
      
      void main() => runApp(MyApp());
      
      class MyApp extends StatelessWidget {
        const MyApp({Key? key}) : super(key: key);
      
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
              home: Scaffold(
            appBar: AppBar(title: Text('FlutterDemo')),
            body: LayoutDemo(),
          ));
        }
      }
      
      class LayoutDemo extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return Stack(children: [
            Container(
              height: 400,
              width: 300,
              color: Colors.red,
            ),
            Text("如果不是Stack组件,那么这行文本应该显示在Container的下面"),
            Text("文本堆在文本上面")
          ]);
        }
      }
      
      

      flutter学习(7)Stack组件_第3张图片

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('FlutterDemo')),
      body: LayoutDemo(),
    ));
  }
}

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Stack(
        alignment: Alignment.center,
        children: [
          Container(
            height: 400,
            width: 300,
            color: Colors.red,
          ),
          Text('我是一个文本', style: TextStyle(fontSize: 40, color: Colors.white))
        ],
      ),
    );
  }
}

flutter学习(7)Stack组件_第4张图片

  • 如果把Text和Container的顺序换一下,Text就被盖住了

    class LayoutDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Stack(
            alignment: Alignment.center,
            children: [
              Text('我是一个文本', style: TextStyle(fontSize: 40, color: Colors.white)),
              Container(
                height: 400,
                width: 300,
                color: Colors.red,
              ),
            ],
          ),
        );
      }
    }
    
    flutter学习(7)Stack组件_第5张图片
  • Text恢复回去

    修改Alignment为alignment: Alignment.bottomLeft,效果如下

    flutter学习(7)Stack组件_第6张图片

Alignment指定参数

  • 使用Alignment的构造函数

    class LayoutDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Stack(
            alignment: Alignment(0, 0),
            children: [
              Container(
                height: 400,
                width: 300,
                color: Colors.red,
              ),
              Text('我是一个文本', style: TextStyle(fontSize: 10, color: Colors.white))
            ],
          ),
        );
      }
    }
    
    

    flutter学习(7)Stack组件_第7张图片

    flutter学习(7)Stack组件_第8张图片

    (0,0)是居中

二.Flutter Stack Align

flutter学习(7)Stack组件_第9张图片

  • 当里面有多个组件仍然用Stack组件的Alignment属性来控制的话,所有的组件都会到同一个方位,仍然会重叠

    class LayoutDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
            child: Container(
          height: 400,
          width: 300,
          color: Colors.red,
          child: Stack(
            alignment: Alignment.center,
            children: [
              Icon(Icons.home, size: 40, color: Colors.white),
              Icon(Icons.search_off, size: 30, color: Colors.cyanAccent),
              Icon(Icons.send, size: 50, color: Colors.black)
            ],
          ),
        ));
      }
    }
    

    flutter学习(7)Stack组件_第10张图片

  • 此时可以用Align组件控制每一个组件的位置

    
    class LayoutDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
            child: Container(
          height: 400,
          width: 300,
          color: Colors.red,
          child: Stack(
            children: [
              Align(
                alignment: Alignment.center,
                child: Icon(Icons.home, size: 40, color: Colors.white),
              ),
              Align(
                alignment: Alignment.topLeft,
                child: Icon(Icons.search_off, size: 30, color: Colors.cyanAccent),
              ),
              Align(
                  alignment: Alignment.bottomLeft,
                  child: Icon(Icons.send, size: 50, color: Colors.black))
            ],
          ),
        ));
      }
    }
    
    

    flutter学习(7)Stack组件_第11张图片

三、Flutter Stack Positioned

flutter学习(7)Stack组件_第12张图片

没写的属性值默认为0

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
        child: Container(
      height: 400,
      width: 300,
      color: Colors.red,
      child: Stack(
        children: [
          Positioned(
            left: 100,
            child: Icon(Icons.home, size: 40, color: Colors.white),
          ),
          Positioned(
            bottom: 10,
            child: Icon(Icons.search_off, size: 30, color: Colors.cyanAccent),
          ),
          Positioned(
              top: 304,
              right: 10,
              child: Icon(Icons.send, size: 50, color: Colors.black))
        ],
      ),
    ));
  }
}

flutter学习(7)Stack组件_第13张图片

你可能感兴趣的:(#,flutter移动端开发,flutter,android,android,studio)