IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android学习笔记(五):Flutter布局 -> 正文阅读

[移动开发]Android学习笔记(五):Flutter布局

一、Column

import 'package:flutter/material.dart';

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("布局练习"),
      ),
      body: Container(    // 容器包裹Column,给个背景色
        color: Colors.grey,
        child: Column(
          verticalDirection: VerticalDirection.up,    // 倒着排序
          mainAxisSize: MainAxisSize.min,   // 最小尺寸
          crossAxisAlignment: CrossAxisAlignment.end,   // 纵轴排列方案
          mainAxisAlignment: MainAxisAlignment.end,   // 主轴排序方案
          children: [
            Container(
              color: Colors.red,
              height: 100,
              width: 100,
            ),
            Container(
              color: Colors.green,
              height: 200,
              width: 150,
            ),
            Container(
              color: Colors.yellow,
              height: 100,
              width: 100,
            ),
          ],
        ),
      )
    );
  }
}

二、Row同上

三、Flex

import 'package:flutter/material.dart';

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("布局练习"),
      ),
      body: Flex(  // 弹性布局
        direction: Axis.vertical, // 垂直布局
        children: [
          Expanded( // 弹性的。会充满父容器
            child: Container(
              color: Colors.red,
              height: 100,
              width: 200,
            ),
            flex: 1,  // 权重
          ),
          Expanded( // 弹性的。会充满父容器
            child: Container(
              color: Colors.green,
              height: 100,
              width: 200,
            ),
            flex: 2,  // 权重
          ),
          Expanded( // 弹性的。会充满父容器
            child: Container(
              color: Colors.yellow,
              height: 100,
              width: 200,
            ),
            flex: 1,  // 权重
          ),
        ],
      )
    );
  }
}

四、Wrap

import 'package:flutter/material.dart';

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("布局练习"),
      ),
      body: Container(
        child: WrapDemo(),
      )
    );
  }
}

class ErrorDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Text("data" * 200) // 会超出屏幕,报错;使用最大一行或者流式布局解决
      ],
    );
  }
}

class WrapDemo extends StatefulWidget {
  @override
  _WrapDemoState createState() => _WrapDemoState();
}

class _WrapDemoState extends State<WrapDemo> {
  List<int> list = [];
  @override
  void initState() {
    super.initState();
    for(int i = 0; i < 200; ++i) {
      list.add(i);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Wrap(
      direction: Axis.vertical,   // 垂直排列
      alignment: WrapAlignment.end,   // 从结尾开始排列
      spacing: 1,  //每个 children 间,增加间隔
      runSpacing: 1,
      children: list.map((e) => Container(
        height: 10,
        width: 10,
        child: Text(e.toString()),
        color: Colors.blue,
      )).toList(),
    );
  }
}

五、Stack

import 'package:flutter/material.dart';

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("布局练习"),
      ),
      body: Container(
        width: double.infinity,
        color: Colors.grey,
        child: StackDemo(),
      )
    );
  }
}

class StackDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: AlignmentDirectional.topEnd,
      children: [
        Container(
          color: Colors.green,
          height: 100,
          width: 100,
        ),
        Container(   // 会覆盖前一个child
          color: Colors.red,
          height: 50,	// Stack的child不设置宽高,默认占满全屏
          width: 50,
        ),
      ],
    );
  }
}

六、Positioned

import 'package:flutter/material.dart';

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("布局练习"),
      ),
      body: Container(
        width: double.infinity,
        color: Colors.grey,
        child: StackDemo(),
      )
    );
  }
}

class StackDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: AlignmentDirectional.topEnd,
      children: [
        Container(
          color: Colors.green,
          height: 100,
          width: 100,
        ),
        Container(   // 会覆盖前一个child
          color: Colors.red,
          height: 50, // Stack的child不设置宽高,默认占满全屏
          width: 50,
        ),
        Positioned(
          top: 10,
          bottom: 20,
          left: 30,
          right: 40,
          child: Container(
            color: Colors.yellow,
            height: 50, // 宽高可能会没用
            width: 50,
          )
        )
      ],
    );
  }
}

七、Align

import 'package:flutter/material.dart';

class LayoutDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("布局练习"),
      ),
      body: Container(
        color: Colors.grey,
        child: AlignDemo()
      )
    );
  }
}

class AlignDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      height: 100,
      width: 100,
      child: Align(
        alignment: Alignment(-1, 0.5),
        child: Container(
          color: Colors.green,
          width: 20,
          height: 20,
        ),
      ),
    );
  }
}
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 10:54:40  更:2021-07-23 10:57:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 11:55:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码