1.UI布局的核心

任何UI布局核心在于计算子控件的大小和位置,本质是指定子控件的上、下、左、右,这直接决定了子控件的位置和大小。

2.flutter系统布局

flutter提供了多种容器与布局,像Container,SizeBox,ConstrainedBox,Expanded,Padding,Center,Row,Column,Flex等等

3.使用示例

3.1 使用示例

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(''),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            const SizedBox(height: 60),
            Image.asset('assets/images/login/app_logo.png', width: 120),
            const SizedBox(height: 10),
            const Text(
              "测试APP",
              style: TextStyle(fontSize: 20, color: Color(0xFFBECEFA)),
            ),
            const SizedBox(height: 40),
            Padding(
              padding: const EdgeInsets.only(left: 20, right: 20),
              child: _buildTextField('请输入账户名', Icons.person),
            ),
            const SizedBox(height: 20),
            Padding(
              padding: const EdgeInsets.only(left: 20, right: 20),
              child: _buildTextField('请输入密码', Icons.password_rounded),
            ),
            const SizedBox(height: 40),
            _buildCustomButton('登陆', const Color(0xff5B70CF), () {
              debugPrint('点击了登陆按钮');
            }),
            _buildCustomButton('注册', const Color(0xffF8644E), () {
              debugPrint('点击了注册按钮');
            }),
            const SizedBox(height: 60),
            const Text(
              '其他方式登录',
              style: TextStyle(
                fontSize: 16,
                color: Color(0xFFD5DFF2),
              ),
            ),
            const SizedBox(height: 20),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GestureDetector(
                onTap: () {
                  debugPrint('点击了微信登录');
                },
                child: Image.asset(
                  'assets/images/login/wechat.png',
                  width: 40,
                  height: 40,
                ),
              ),
              const SizedBox(
                width: 20,
              ),
              InkWell(
                onTap: () {
                  debugPrint('点击了QQ登录');
                },
                child: Image.asset(
                  'assets/images/login/qq.png',
                  width: 40,
                  height: 40,
                ),
              ),
              const SizedBox(
                width: 20,
              ),
              InkWell(
                onTap: () {
                  debugPrint('点击了微博登录');
                },
                child: Image.asset(
                  'assets/images/login/weibo.png',
                  width: 40,
                  height: 40,
                ),
              ),
            ]),
          ],
        ),
      ),
    );
  }

  Widget _buildTextField(String hintText, IconData iconData) {
    return Container(
      height: 60,
      color: const Color(0xFFF0F5FD),
      child: Row(
        children: <Widget>[
          Container(
            margin: const EdgeInsets.only(left: 10, right: 20),
            child: Icon(iconData),
          ),
          Expanded(
            child: TextField(
              style: const TextStyle(
                color: Color(0xffA0ACD4),
                fontSize: 20,
                // fontWeight: FontWeight.bold,
              ),
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: hintText,
                hintStyle: const TextStyle(color: Color(0xffA0ACD4)),
                labelStyle: const TextStyle(color: Color(0xffA0ACD4)),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCustomButton(
      String text, Color color, void Function()? onPressed) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(120, 30, 120, 0),
      child: SizedBox(
        height: 60,
        width: double.infinity,
        child: ElevatedButton(
          onPressed: onPressed,
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(color),
          ),
          child: Text(
            text,
            style: const TextStyle(
              fontSize: 20,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}

4.效果

image

5.其它

5.1.UI布局

UI布局是前端技术基础技能,给想入门的演示下一些基本用法,提供个简单示例,不要被系统提供的二三十种UI布局的工具所吓倒,再复杂的页面,也是常用布局的一些组合,练练就知深浅。

5.2.和其它平台对比

使用flutter系统自带UI容器和布局,构建页面,是不是很自然?相比Android/iOS/各有优势,不再赘述。相比windows,Linux,大家可以自己体会。