Flutter 中的 ToggleButtons 小部件:全面指南

Flutter 中的 ToggleButtons 小部件:全面指南

在 Flutter 中,ToggleButtons 是一种允许用户在一组选项中进行切换选择的控件。它通常用于展示一组相关选项,让用户可以快速切换选择。ToggleButtons 是一种水平排列的按钮集合,其中只有一个按钮可以被选中。它们在设计上与 Material Design 中的开关按钮相似,适用于简单的是/否、开/关或真/假类型的选择。

基础用法

ToggleButtons 最基本的用法是定义一组按钮和一个选择回调:

dart 复制代码
ToggleButtons(
  children: Map<String, Widget>.fromIterable(
    ['Yes', 'No', 'Maybe'],
    key: (value) => value,
  ),
  isSelected: [true, false, false],
  onPressed: (index) {
    // 当按钮被按下时执行的操作
  },
)

在这个例子中,我们创建了三个按钮,其中第一个按钮("Yes")默认被选中。

自定义样式

ToggleButtons 提供了一些属性来定制按钮的外观和行为:

按钮样式

  • children: 一个映射,键是按钮的标识符,值是按钮的 Widget。
  • style: 定义未选中按钮的文本样式。
  • selectedStyle: 定义选中按钮的文本样式。
dart 复制代码
ToggleButtons(
  children: <String, Widget>{
    'Yes': Text('Yes'),
    'No': Text('No'),
    'Maybe': Text('Maybe'),
  },
  style: TextStyle(color: Colors.blueGrey),
  selectedStyle: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
  // ... 其他属性
)

选中状态

  • isSelected: 一个布尔值列表,表示每个按钮是否被选中。
dart 复制代码
ToggleButtons(
  isSelected: [true, false, false],
  // ... 其他属性
)

按钮回调

  • onPressed: 当用户点击按钮时调用的回调,参数是被点击按钮的索引。
dart 复制代码
ToggleButtons(
  onPressed: (index) {
    // 根据索引处理按钮的选中逻辑
  },
  // ... 其他属性
)

实例:颜色选择器

ToggleButtons 可以用于实现颜色选择器,允许用户在一组颜色中选择:

dart 复制代码
ToggleButtons(
  children: <String, Widget>{
    'Red': Container(width: 20.0, height: 20.0, color: Colors.red),
    'Green': Container(width: 20.0, height: 20.0, color: Colors.green),
    'Blue': Container(width: 20.0, height: 20.0, color: Colors.blue),
  },
  isSelected: [true, false, false],
  onPressed: (index) {
    // 处理颜色选择逻辑
  },
)

实例:尺寸选择

使用 ToggleButtons 可以让用户选择不同的尺寸:

dart 复制代码
ToggleButtons(
  children: <String, Widget>{
    'S': Text('Small'),
    'M': Text('Medium'),
    'L': Text('Large'),
  },
  isSelected: [false, true, false],
  onPressed: (index) {
    // 根据索引更新当前选择的尺寸
  },
)

实例:动态更新选中状态

可以动态地更新 isSelected 列表来改变按钮的选中状态:

dart 复制代码
// 假设这是当前选中的尺寸索引
int selectedSizeIndex = 1;

// ... 在 Widget 中
ToggleButtons(
  children: <String, Widget>{
    'S': Text('Small'),
    'M': Text('Medium'),
    'L': Text('Large'),
  },
  isSelected: [false, true, false], // 根据 selectedSizeIndex 动态更新
  onPressed: (index) {
    setState(() {
      selectedSizeIndex = index;
    });
  },
)

#结语

ToggleButtons 是 Flutter 中一个简单而直观的小部件,它非常适合用于实现单选按钮组。通过掌握 ToggleButtons 的使用,你可以为用户提供清晰而一致的选择界面,从而提升应用的用户体验。

相关推荐
kyriewen113 小时前
你点的“刷新”是假刷新?前端路由的瞒天过海术
开发语言·前端·javascript·ecmascript·html5
skywalk81635 小时前
Kotti Next的tinyfrontend前端模仿Kotti 首页布局还是不太好看,感觉比Kotti差一点
前端
RopenYuan7 小时前
FastAPI -API Router的应用
前端·网络·python
走粥7 小时前
clsx和twMerge解决CSS类名冲突问题
前端·css
Purgatory0018 小时前
layui select重新渲染
前端·layui
2501_920627618 小时前
Flutter 框架跨平台鸿蒙开发 - 派对策划助手应用
flutter·华为·harmonyos
weixin199701080168 小时前
《中国供应商商品详情页前端性能优化实战》
前端·性能优化
里欧跑得慢9 小时前
Flutter 组件 powersync_core 的适配 鸿蒙Harmony 实战 - 驾驭极致离线优先架构、实现鸿蒙端高性能 SQL 增量同步与数据安全治理方案
flutter·harmonyos·鸿蒙·openharmony·powersync_core
芙莉莲教你写代码10 小时前
Flutter 框架跨平台鸿蒙开发 - 网络安全学习应用
学习·web安全·flutter·华为·harmonyos