flutter:&UI布局 (六)

简介: 本文档介绍了Flutter中的UI布局方式,包括线性布局(如Column和Row)、非线性布局(如Stack、Flex、Positioned)以及Wrap布局等。通过具体示例代码展示了如何使用这些布局组件来构建灵活多变的用户界面,例如使用Column垂直排列文本、使用Stack叠加组件、以及利用Wrap实现自动换行的按钮布局等。

前言

在现代移动应用开发中,用户界面的布局设计至关重要。Flutter作为一个高效的UI框架,提供了丰富的布局组件,能够帮助开发者轻松构建灵活多变的用户界面。本文将深入探讨Flutter中的各种UI布局方式,包括线性布局(如Column和Row)、非线性布局(如Stack、Flex、Positioned)以及Wrap布局。通过具体的示例代码,我们将展示如何利用这些布局组件来创建简洁而富有表现力的界面。

UI布局

线性布局

return const Scaffold(
    body:Column(
      children:[ Text('hello'),
        Text('world'),
        Text('welcome'),
    ])
  );
body:Row(
      children: [
        Text('hello+'),
        Text('world+'),
        Text('welcome+')
      ],
    )

平分

Widget buildColumn(){
  return Container(
    decoration: BoxDecoration(border: Border.all()),
    child: Column(
  children: [
    Expanded(child: Container(color: Colors.grey,),flex: 1,),
    Expanded(child: Container(color: Colors.red,),flex: 1,),
    Expanded(child: Container(color: Colors.blue,),flex: 1,),
  ],
    ),
  );
}

先使用一个 之后会占满

child: Row(
          children: [
            ElevatedButton(
              onPressed: () {
                print("hello");
              },
              child: Text(
                "hello",
                style: TextStyle(fontSize: 20, color: Colors.black),
              ),
            ),
            Expanded(
              child: ElevatedButton(
                onPressed: () {
                  print("hello");
                },
                child: Text(
                  "hello",
                  style: TextStyle(fontSize: 20, color: Colors.black),
                ),
              ),
            )
          ],
        ),
return  Scaffold(
    body:Container(
      child: buildColumn(),
    )
  );
return Scaffold(
      body: Row(
        children: [
          Expanded(flex:1,child:Container(
            alignment: Alignment.center,
            height: 33,
            color: Colors.blue,
            child: Text('二级导航栏',style: TextStyle(
              color: Colors.white
            ),),
          ) )
        ],
      ),
    );

非线性布局

Stack

可以容纳多个组件,以叠加的方式摆放子组件,后者居上。拥有Alignment属性,可以与Positioned组件联合使用,精准并有效摆放。同AndroidFramLayout布局相似。

@override
  Widget build(BuildContext context) {
    return Stack(
      // alignment
      alignment: Alignment.center,
      children: [
        Container(
        height: 400,
      width: 300,
      color: Colors.red,
        ),Text('hello,flutter',style: TextStyle(color: Colors.blue),),
        Container(
          height: 200,
          width: 200,
          color: Colors.yellow,
        ),Text('hello,flutter',style: TextStyle(color: Colors.green),)
      ],
    );
  }


Flex

return Container(
            child: Row(
              children: [
                Expanded(
                    flex: 2,
                    child: Container(
                  width: 100,height: 100,
                  color: Colors.red,
                )),
                Expanded(
                    flex: 1,
                    child: Container(
                      width: 100,height: 100,
                      color: Colors.blue,
                    )),
              ],
            ),
          );

positioned

层叠布局和 Web 中的绝对定位、Android 中的 Frame 布局是相似的,子组件可以根据距父容器四个角的位置来确定自身的位置。层叠布局允许子组件按照代码中声明的顺序堆叠起来。Flutter中使用StackPositioned这两个组件来配合实现绝对定位。Stack允许子组件堆叠,而Positioned用于根据Stack的四个角来确定子组件的位置

return Container(
      height: 400,
      width: 300,
      color: Colors.red,
      child: Stack(
        children: [
          Positioned(
            // 改位置的地方
            left:0,
            bottom: 0,
            child: Container(
              height: 100,
              width: 100,
              color: Colors.yellow,
            ),
          ),
          Text('hello,world'),
        ],
      ),
    );

stack  加 positioned

实现 布局  

return Stack(
      children: [
        Positioned(
          height: 40,
          width: 40,
          right: 20,
          bottom: 20,
          child: Row(
            children: [
              Expanded(
                  flex: 1,
                  child: Container(
                    alignment: Alignment.center,
                    height: 44,
                    color: Colors.blue,
                     child: Icon(Icons.circle),
                  ))
            ],
          ),
        )
      ],
    );

获取屏幕的高度

final size = MediaQuery.of(context).size;
final width =  size.width
height..一样


aspectRadio

宽高比

//  高度是容器的一半
      return AspectRatio(aspectRatio: 2/1,
      child: Container(
        color: Colors.red,
      ),
      );
  }


wrap 布局

需要用padding  包裹

return Padding(
          // 加 边距  需要在外面加
        padding: EdgeInsets.all(10),
        child:    Wrap(   // 一行满了之后  换行
        spacing: 10,  //  x axis   水平间距
        runSpacing: 10,  // y axis  垂直
        direction:  Axis.horizontal, //  默认  direction: Axis.vertical
        alignment: WrapAlignment.center, //  看 参数
        children: [
          Button("chapter one",onPressed: (){},),
          Button("chapter two",onPressed: (){},),
          Button("chapter three",onPressed: (){},),
          Button("chapter four",onPressed: (){},),
          Button("chapter five",onPressed: (){},),
          Button("chapter six",onPressed: (){},),
          Button("chapter seven",onPressed: (){},),
          Button("chapter eight",onPressed: (){},),
        ],
      ),
      );

demo

import 'package:flutter/material.dart';
class Myapp extends StatelessWidget {
  const Myapp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'hello flutter',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(title: Text("flutter app"),),
        body: LayoutDemo(),
      ),
    );
  }
}
class LayoutDemo extends StatelessWidget {
  const LayoutDemo({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
      return Padding(
          // 加 边距  需要在外面加
        padding: EdgeInsets.all(10),
        child:    Wrap(   // 一行满了之后  换行
        spacing: 10,  //  x axis   水平间距
        runSpacing: 10,  // y axis  垂直
        direction:  Axis.horizontal, //  默认  direction: Axis.vertical
        alignment: WrapAlignment.center, //  看 参数
        children: [
          Button("chapter one",onPressed: (){},),
          Button("chapter two",onPressed: (){},),
          Button("chapter three",onPressed: (){},),
          Button("chapter four",onPressed: (){},),
          Button("chapter five",onPressed: (){},),
          Button("chapter six",onPressed: (){},),
          Button("chapter seven",onPressed: (){},),
          Button("chapter eight",onPressed: (){},),
        ],
      ),
      );
    }
}
//  自定义按钮
class Button extends StatelessWidget {
  final String text;
  final void Function()? onPressed;
   Button(this.text,{Key? key,required this.onPressed}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        style:ButtonStyle(
          backgroundColor: MaterialStateProperty.all(Color.fromRGBO(241, 255, 244, 244)),
          foregroundColor: MaterialStateProperty.all(Colors.black45)
        ),
        onPressed: onPressed,
        child:Text(text));
  }
}
void main(){
  runApp(
  const Myapp()
  );
}


相关文章
|
5月前
|
编解码 前端开发 开发者
【Flutter前端技术开发专栏】Flutter中的响应式设计与自适应布局
【4月更文挑战第30天】Flutter框架助力移动应用实现响应式设计与自适应布局,通过层次化布局系统和`Widget`树管理,结合`BoxConstraints`定义尺寸范围,实现自适应。利用`MediaQuery`获取设备信息,调整布局以适应不同屏幕。`FractionallySizedBox`按比例设定尺寸,`LayoutBuilder`动态计算布局。借助这些工具,开发者能创建跨屏幕尺寸、方向兼容的应用,提升用户体验。
143 0
【Flutter前端技术开发专栏】Flutter中的响应式设计与自适应布局
|
3月前
鸿蒙使用 @Builder扩展出来的布局数据更新没法更新UI
鸿蒙使用 @Builder扩展出来的布局数据更新没法更新UI
108 1
|
23小时前
|
API 容器
Flutter UI组件库(JUI)
Flutter UI组件库(JUI)
23 17
|
4月前
|
存储 开发框架 JavaScript
深入探讨Flutter中动态UI构建的原理、方法以及数据驱动视图的实现技巧
【6月更文挑战第11天】Flutter是高效的跨平台移动开发框架,以其热重载、高性能渲染和丰富组件库著称。本文探讨了Flutter中动态UI构建原理与数据驱动视图的实现。动态UI基于Widget树模型,状态变化触发UI更新。状态管理是关键,Flutter提供StatefulWidget、Provider、Redux等方式。使用ListView等可滚动组件和StreamBuilder等流式组件实现数据驱动视图的自动更新。响应式布局确保UI在不同设备上的适应性。Flutter为开发者构建动态、用户友好的界面提供了强大支持。
85 2
|
5月前
|
开发框架 前端开发 数据安全/隐私保护
【Flutter 前端技术开发专栏】Flutter 中的布局与样式设计
【4月更文挑战第30天】本文探讨了Flutter的布局和样式设计,关键点包括:1) 布局基础如Column、Row和Stack用于创建复杂结构;2) Container、Center和Expanded等常用组件的作用;3) Theme和Decoration实现全局样式和组件装饰;4) 实战应用如登录界面和列表页面的构建;5) 响应式布局利用MediaQuery和弹性组件适应不同屏幕;6) 性能优化,避免过度复杂设计。了解并掌握这些,有助于开发者创建高效美观的Flutter应用。
154 0
【Flutter 前端技术开发专栏】Flutter 中的布局与样式设计
|
2月前
|
编解码 Android开发
【Android Studio】使用UI工具绘制,ConstraintLayout 限制性布局,快速上手
本文介绍了Android Studio中使用ConstraintLayout布局的方法,通过创建布局文件、设置控件约束等步骤,快速上手UI设计,并提供了一个TV Launcher界面布局的绘制示例。
42 1
|
2月前
|
容器 iOS开发 Linux
震惊!Uno Platform 响应式 UI 构建秘籍大公开!从布局容器到自适应设计,带你轻松打造跨平台完美界面
【8月更文挑战第31天】Uno Platform 是一款强大的跨平台应用开发框架,支持 Web、桌面(Windows、macOS、Linux)及移动(iOS、Android)等平台,仅需单一代码库。本文分享了四个构建响应式用户界面的最佳实践:利用布局容器(如 Grid)适配不同屏幕尺寸;采用自适应布局调整 UI;使用媒体查询定制样式;遵循响应式设计原则确保 UI 元素自适应调整。通过这些方法,开发者可以为用户提供一致且优秀的多设备体验。
64 0
|
3月前
|
设计模式 编解码 API
Flutter UI设计模式与实现:深入探索与实践
【7月更文挑战第20天】Flutter以其独特的声明式UI模式和丰富的UI组件库,为移动应用开发提供了强大的支持。通过深入理解Flutter的UI设计模式和实现技巧,开发者可以构建出高性能、可维护性强的UI界面。同时,随着Flutter生态的不断完善和发展,相信未来Flutter将在移动应用开发领域发挥更加重要的作用。
|
2月前
Flutter 状态管理新境界:多Provider并行驱动UI
Flutter 状态管理新境界:多Provider并行驱动UI
55 0
|
3月前
|
容器
flutter 布局管理【详解】
flutter 布局管理【详解】
33 3