1.约束布局的核心

任何UI布局核心在于计算子控件的大小和位置。而约束布局的核心在于使用约束布局来计算子控件的大小和位置,

2.约束布局优势

  • 2.1. 可以减少UI而已层级,使布局层次更加扁平化,可以提升渲染性能。
  • 2.2. 所见即所得的灵活排版布局能力,可以更高效的开发UI页面。

Android 中有约束布局组件,而flutter也有开源的第三方组件https://github.com/hackware1993/Flutter_ConstraintLayout

支持Android/iOS/linux/MacOS/Web/Windows

3.pubspec.yaml添加依赖

1
    flutter_constraintlayout: ^1.7.0-stable

4.使用示例

4.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import 'package:flutter/material.dart';
import 'package:flutter_constraintlayout/flutter_constraintlayout.dart';

class ConstraintLayoutPage extends StatefulWidget {
  const ConstraintLayoutPage({Key? key}) : super(key: key);

  @override
  State<ConstraintLayoutPage> createState() => _ConstraintLayoutPageState();
}

class _ConstraintLayoutPageState extends State<ConstraintLayoutPage> {
  final ConstraintId appLogoId = ConstraintId('appLogoId');
  final ConstraintId appTitleId = ConstraintId('appTitleId');
  final ConstraintId appAccountId = ConstraintId('appAccountId');
  final ConstraintId passwordId = ConstraintId('passwordId');
  final ConstraintId loginId = ConstraintId('loginId');
  final ConstraintId registId = ConstraintId('registId');
  final ConstraintId otherId = ConstraintId('otherId');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('ConstraintLayout'),
      ),
      body: ConstraintLayout(
        children: <Widget>[
          _buildAppLogo(),
          _buildAppTitle(),
          _buildInputContainer(
            id: appAccountId,
            topConstraint: appTitleId.bottom.margin(40),
            hintText: '请输入账户名',
            icon: Icons.person,
          ),
          _buildInputContainer(
            id: passwordId,
            topConstraint: appAccountId.bottom.margin(20),
            hintText: '请输入密码',
            icon: Icons.password,
          ),
          _buildElevatedButton(
            id: loginId,
            topConstraint: passwordId.bottom.margin(70),
            buttonText: '登陆',
            backgroundColor: const Color(0xff5B70CF),
          ),
          _buildElevatedButton(
            id: registId,
            topConstraint: loginId.bottom.margin(30),
            buttonText: '注册',
            backgroundColor: const Color(0xffF8644E),
          ),
          _buildTextWithConstraints(
            id: otherId,
            text: '其他方式登录',
            topConstraint: registId.bottom.margin(60),
            centerHorizontalTo: parent,
          ),
          _buildSocialLoginRow(),
        ],
      ),
    );
    // // AppBar
  }
  // 构建logo
  Widget _buildAppLogo() {
    return Image.asset(
      'assets/images/login/app_logo.png',
      width: 120,
    ).applyConstraint(
      top: parent.top.margin(60),
      left: parent.left.margin(20),
      right: parent.right.margin(20),
      id: appLogoId,
    );
  }
  // 构建app的名称
  Widget _buildAppTitle() {
    return const Text(
      "APP Name",
      style: TextStyle(fontSize: 20, color: Color(0xFFBECEFA)),
    ).applyConstraint(
      id: appTitleId,
      top: appLogoId.bottom.margin(10),
      left: parent.left,
      right: parent.right,
    );
  }

  // 构建输入框:账号/密码
  Widget _buildInputContainer({
    required ConstraintId id,
    required ConstraintAlign topConstraint,
    required String hintText,
    required IconData icon,
  }) {
    return Container(
      color: const Color(0xFFF0F5FD),
      child: Row(
        children: [
          const SizedBox(width: 10),
          Icon(icon),
          const SizedBox(width: 20),
          Expanded(
            child: TextField(
              style: const TextStyle(
                color: Color(0xffA0ACD4),
                fontSize: 20,
              ),
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: hintText,
                hintStyle: const TextStyle(color: Color(0xffA0ACD4)),
                labelStyle: const TextStyle(color: Color(0xffA0ACD4)),
              ),
            ),
          ),
        ],
      ),
    ).applyConstraint(
      id: id,
      width: matchConstraint,
      height: 60,
      top: topConstraint,
      left: parent.left.margin(20),
      right: parent.right.margin(20),
    );
  }

  // 构建登陆/注册
  Widget _buildElevatedButton({
    required ConstraintId id,
    required ConstraintAlign topConstraint,
    required String buttonText,
    required Color backgroundColor,
  }) {
    return ElevatedButton(
      onPressed: () {
        debugPrint('点击了按钮:$buttonText');
      },
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(backgroundColor),
      ),
      child: Text(
        buttonText,
        style: const TextStyle(
          fontSize: 20,
          color: Colors.white,
        ),
      ),
    ).applyConstraint(
      id: id,
      top: topConstraint,
      width: matchConstraint,
      height: 60,
      left: parent.left.margin(120),
      right: parent.right.margin(120),
    );
  }

  // 构造说明文本
  Widget _buildTextWithConstraints({
    required ConstraintId id,
    required String text,
    required ConstraintAlign topConstraint,
    required ConstraintId centerHorizontalTo,
  }) {
    return Text(
      text,
      style: const TextStyle(
        fontSize: 16,
        color: Color(0xFFD5DFF2),
      ),
    ).applyConstraint(
      id: id,
      left: parent.left,
      right: parent.right,
      top: topConstraint,
      centerHorizontalTo: centerHorizontalTo,
    );
  }
  // 构建其它登录方式:微信,QQ,微博
  Widget _buildSocialLoginRow() {
    return 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,
          ),
        ),
      ],
    ).applyConstraint(
      width: matchParent,
      top: otherId.bottom.margin(20),
    );
  }
}

5.效果

image

6.其它

6.1. 和前文使用系统布局相比

  • 使用上,UI层级有所减少;
  • 性能上,性能提升多少和页面嵌套,复杂程度有关;对于一般简单页面,性能差异很很小。
  • 学习成本上,有一定的提升;对熟悉Android约束布局的同学,无缝切换;
  • 开发体验上,用法不当时,抛出一堆异常错误,没有系统原生布局直观,开发体验并不好。

选择使用哪种页面布局方式,可以根据项目,团队的情况,做出适当的选择;适合自己的就好。