【Flutter】Flutter 布局组件 ( Wrap 组件 | Expanded 组件 )(一)

简介: 【Flutter】Flutter 布局组件 ( Wrap 组件 | Expanded 组件 )(一)

文章目录

一、Wrap 组件

二、Expanded 组件

三、完整代码示例

四、相关资源





一、Wrap 组件


Wrap 组件 : 该组件是可换行的水平线性布局组件 , 与 Row 组件间类似 , 但是可以换行 ;


class Wrap extends MultiChildRenderObjectWidget {
  /// Creates a wrap layout.
  ///
  /// By default, the wrap layout is horizontal and both the children and the
  /// runs are aligned to the start.
  ///
  /// The [textDirection] argument defaults to the ambient [Directionality], if
  /// any. If there is no ambient directionality, and a text direction is going
  /// to be necessary to decide which direction to lay the children in or to
  /// disambiguate `start` or `end` values for the main or cross axis
  /// directions, the [textDirection] must not be null.
  Wrap({
    Key key,
    this.direction = Axis.horizontal,
    this.alignment = WrapAlignment.start,
    this.spacing = 0.0, // 水平方向间距
    this.runAlignment = WrapAlignment.start,
    this.runSpacing = 0.0, // 垂直方向间距
    this.crossAxisAlignment = WrapCrossAlignment.start,
    this.textDirection,
    this.verticalDirection = VerticalDirection.down,
    List<Widget> children = const <Widget>[], // 子组件集合
  }) : super(key: key, children: children);
}


Wrap 组件用法 :


设置水平间距 : spacing 字段 ;
设置垂直间距 : runSpacing 字段 ;
设置布局中的子组件 : children 字段 ;
// 可自动换行的水平线性布局
Wrap(
  // 设置水平边距
  spacing: 间距值 ( double 类型 ),
  // 设置垂直间距
  runSpacing: 间距值 ( double 类型 ),
  children: <Widget>[
    设置若干子组件 
  ]
)


代码示例 : Chip 组件用法参考 【Flutter】StatelessWidget 组件 ( CloseButton 组件 | BackButton 组件 | Chip 组件 ) 博客 ;


// 可自动换行的水平线性布局
Wrap(
  // 设置水平边距
  spacing: 40,
  // 设置垂直间距
  runSpacing: 10,
  children: <Widget>[
    Chip(
      // 设置主体标签文本
      label: Text("宋江"),
      // 设置左侧圆形头像
      avatar: CircleAvatar(
        // 设置背景颜色
        backgroundColor: Colors.green.shade600,
        child: Text("宋"),
      ),
    ),
    Chip(
      // 设置主体标签文本
      label: Text("卢俊义"),
      // 设置左侧圆形头像
      avatar: CircleAvatar(
        // 设置背景颜色
        backgroundColor: Colors.green.shade600,
        child: Text("卢"),
      ),
    ),
    Chip(
      // 设置主体标签文本
      label: Text("吴用"),
      // 设置左侧圆形头像
      avatar: CircleAvatar(
        // 设置背景颜色
        backgroundColor: Colors.green.shade600,
        child: Text("吴"),
      ),
    ),
    Chip(
      // 设置主体标签文本
      label: Text("公孙胜"),
      // 设置左侧圆形头像
      avatar: CircleAvatar(
        // 设置背景颜色
        backgroundColor: Colors.green.shade600,
        child: Text("公孙"),
      ),
    ),
    Chip(
      // 设置主体标签文本
      label: Text("关胜"),
      // 设置左侧圆形头像
      avatar: CircleAvatar(
        // 设置背景颜色
        backgroundColor: Colors.green.shade600,
        child: Text("关"),
      ),
    ),
  ],
),


运行效果 :


image.png






二、Expanded 组件


Expanded 组件 : 该组件可以自动识别父容器的方向 , 在垂直或水平方向上填充剩余空间 ;
class Expanded extends Flexible {
  /// Creates a widget that expands a child of a [Row], [Column], or [Flex]
  /// so that the child fills the available space along the flex widget's
  /// main axis.
  const Expanded({
    Key key,
    int flex = 1,
    @required Widget child,
  }) : super(key: key, flex: flex, fit: FlexFit.tight, child: child);
}


Expanded 组件 在 Row 组件 中会自动填充水平方向上的剩余空间 ;


Expanded 组件 在 Column 组件 中会自动填充垂直方向上的剩余空间 ;


代码示例 :


// 普通样式的 Row
Row(
  children: <Widget>[
    Container(
      // 背景设置成黑色
      decoration: BoxDecoration(
        color: Colors.black,
      ),
      // 字体设置成黄色
      child: Text(
        "Text 原始样式",
        style: TextStyle(color: Colors.yellow),
      ),
    ),
  ],
),
// 空行
SizedBox(
  width: 10,
  height: 20,
),
// 使用了 Exoanbded 组件的 Row
Row(
  children: <Widget>[
    Expanded(
      child: Container(
        // 背景设置成黑色
        decoration: BoxDecoration(
          color: Colors.black,
        ),
        // 字体设置成黄色
        child: Text(
          "Text 原始样式",
          style: TextStyle(color: Colors.yellow),
        ),
      ),
    ),
  ],
),
// 空行


执行效果 :

image.png



第一个组件是 Row 中没有使用 Expanded 组件的情况 ;


第二个组件是 Row 中使用了 Expanded 组件的情况 ;




目录
相关文章
|
13天前
|
编解码 前端开发 开发者
【Flutter前端技术开发专栏】Flutter中的响应式设计与自适应布局
【4月更文挑战第30天】Flutter框架助力移动应用实现响应式设计与自适应布局,通过层次化布局系统和`Widget`树管理,结合`BoxConstraints`定义尺寸范围,实现自适应。利用`MediaQuery`获取设备信息,调整布局以适应不同屏幕。`FractionallySizedBox`按比例设定尺寸,`LayoutBuilder`动态计算布局。借助这些工具,开发者能创建跨屏幕尺寸、方向兼容的应用,提升用户体验。
【Flutter前端技术开发专栏】Flutter中的响应式设计与自适应布局
|
13天前
|
开发框架 前端开发 数据安全/隐私保护
【Flutter 前端技术开发专栏】Flutter 中的布局与样式设计
【4月更文挑战第30天】本文探讨了Flutter的布局和样式设计,关键点包括:1) 布局基础如Column、Row和Stack用于创建复杂结构;2) Container、Center和Expanded等常用组件的作用;3) Theme和Decoration实现全局样式和组件装饰;4) 实战应用如登录界面和列表页面的构建;5) 响应式布局利用MediaQuery和弹性组件适应不同屏幕;6) 性能优化,避免过度复杂设计。了解并掌握这些,有助于开发者创建高效美观的Flutter应用。
【Flutter 前端技术开发专栏】Flutter 中的布局与样式设计
|
13天前
|
前端开发 搜索推荐 UED
【Flutter前端技术开发专栏】Flutter中的高级UI组件应用
【4月更文挑战第30天】探索Flutter的高级UI组件,如`TabBar`、`Drawer`、`BottomSheet`,提升应用体验和美观度。使用高级组件能节省开发时间,提供内置交互逻辑和优秀视觉效果。示例代码展示了如何实现底部导航栏、侧边导航和底部弹出菜单。同时,自定义组件允许个性化设计和功能扩展,但也带来性能优化和维护挑战。参考Flutter官方文档和教程,深入学习并有效利用这些组件。
【Flutter前端技术开发专栏】Flutter中的高级UI组件应用
|
17天前
|
编解码 算法 开发者
Flutter的布局系统:深入探索布局Widget与布局原则
【4月更文挑战第26天】Flutter布局系统详解,涵盖布局Widget(Row/Column、Stack、GridView/ListView、CustomSingleChildLayout)和布局原则(弹性布局、约束优先、流式布局、简洁明了)。文章旨在帮助开发者理解并运用Flutter的布局系统,创建适应性强、用户体验佳的界面。通过选择合适的布局Widget和遵循原则,可实现复杂且高效的UI设计。
|
18天前
|
Dart
Flutter 中优雅切换应用主题的组件
【Flutter】使用adaptive_theme组件优雅切换应用主题:封装MaterialApp,支持light/dark/system模式,自定义色彩,保存选择,含调试按钮。安装配置、设置主题、监听切换、自定义颜色、读取配置步骤详细。代码示例与学习路径推荐。[查看完整教程](https://flutter.ducafecat.com/blog/flutter-app-theme-switch)
Flutter 中优雅切换应用主题的组件
|
4月前
|
容器
Flutter笔记:Box协议的布局约束原理与应用
Flutter笔记:Box协议的布局约束原理与应用
49 0
|
4月前
|
开发者 索引 容器
Flutter开发笔记:Flutter 布局相关组件
Flutter开发笔记:Flutter 布局相关组件
135 0
|
4月前
|
iOS开发
Flutter 组件(三)按钮类组件
Flutter 组件(三)按钮类组件
168 0
|
Dart Android开发
【Flutter】StatefulWidget 组件 ( 创建 StatefulWidget 组件 | MaterialApp 组件 | Scaffold 组件 )(一)
【Flutter】StatefulWidget 组件 ( 创建 StatefulWidget 组件 | MaterialApp 组件 | Scaffold 组件 )(一)
161 0
【Flutter】StatefulWidget 组件 ( 创建 StatefulWidget 组件 | MaterialApp 组件 | Scaffold 组件 )(一)
|
Dart 开发者
【Flutter】StatefulWidget 组件 ( 创建 StatefulWidget 组件 | MaterialApp 组件 | Scaffold 组件 )(二)
【Flutter】StatefulWidget 组件 ( 创建 StatefulWidget 组件 | MaterialApp 组件 | Scaffold 组件 )(二)
196 0