当前位置: 首页 > article >正文

Flutter 按钮组件 TextButton 详解

目录

1. 引言

2. TextButton 的基本用法

3. 主要属性

4. 自定义按钮样式

4.1 修改文本颜色

4.2 添加背景色

4.3 修改按钮形状和边距

4.4 样式定制

5. 高级应用技巧

5.1 图标+文本组合

5.2  主题统一配置

5.3 动态交互

6. 性能优化与注意事项

6.1 点击区域优化

6.2 避免过度重建

6.3 无障碍支持

 6.4 点击无响应

相关推荐


1. 引言

        在 Flutter 中,TextButton 是一种无背景的按钮,适用于次要或轻量级操作。它的外观更加简洁,仅包含文字,适合用作辅助性操作,如“取消”或“了解更多”。相比 ElevatedButtonTextButton 没有阴影和背景色,更加简约。

2. TextButton 的基本用法

    TextButton 需要 onPressed 事件和 child 组件。

TextButton(
  onPressed: () {
    print('TextButton 被点击');
  },
  child: Text('点击我'),
)

    如果 onPressed 设为 null,按钮会变为不可点击状态。

TextButton(
  onPressed: null,
  child: Text('不可点击'),
)

3. 主要属性

属性说明
onPressed按钮点击时的回调函数
onLongPress长按时触发的回调
child按钮的内容,如 TextIcon
style自定义按钮样式

示例:

TextButton(
  onPressed: () {},
  onLongPress: () => print('长按按钮'),
  child: Text('长按试试'),
)

4. 自定义按钮样式

4.1 修改文本颜色

TextButton(
  style: TextButton.styleFrom(
    primary: Colors.blue, // 文字颜色
  ),
  onPressed: () {},
  child: Text('自定义颜色'),
)

4.2 添加背景色

TextButton(
  style: TextButton.styleFrom(
    backgroundColor: Colors.blue,
    primary: Colors.white,
  ),
  onPressed: () {},
  child: Text('带背景色的 TextButton'),
)

4.3 修改按钮形状和边距

TextButton(
  style: TextButton.styleFrom(
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(20),
    ),
    padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
  ),
  onPressed: () {},
  child: Text('圆角按钮'),
)

4.4 样式定制

TextButton(
            style: ButtonStyle(
              // 文字颜色(包括禁用状态)
              foregroundColor: WidgetStateProperty.resolveWith<Color>(
                    (Set<WidgetState> states) {
                  if (states.contains(WidgetState.disabled)) return Colors.grey;
                  return Colors.blue;
                },
              ),
              // 背景色
              backgroundColor: WidgetStateProperty.all(Colors.transparent),
              // 水波纹颜色
              overlayColor: WidgetStateProperty.all(Colors.blue.withOpacity(0.1)),
              // 内边距
              padding: WidgetStateProperty.all(EdgeInsets.symmetric(horizontal: 16)),
              // 边框形状
              shape: WidgetStateProperty.all(
                RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
            ),
            onPressed: () {},
            child: Text('自定义样式'),
          )

5. 高级应用技巧

5.1 图标+文本组合

          TextButton.icon(
            icon: Icon(Icons.add_box_rounded, size: 20),
            label: Text('添加好友'),
            onPressed: () {},
            style: ButtonStyle(
              padding: WidgetStateProperty.all(
                EdgeInsets.symmetric(vertical: 12, horizontal: 20),
              ),
            ),

5.2  主题统一配置

MaterialApp(
  theme: ThemeData(
    textButtonTheme: TextButtonThemeData(
      style: ButtonStyle(
        foregroundColor: MaterialStateProperty.all(Colors.purple),
        textStyle: MaterialStateProperty.all(
          TextStyle(fontWeight: FontWeight.bold)),
      ),
    ),
  ),
)

5.3 动态交互

// 点击缩放动画
TextButton(
  onPressed: () {},
  child: AnimatedContainer(
    duration: Duration(milliseconds: 200),
    transform: isPressed ? Matrix4.diagonal3Values(0.95, 0.95, 1) : null,
    child: Text('动态按钮'),
  ),
)

6. 性能优化与注意事项

6.1 点击区域优化

        默认最小尺寸为 48x48(Material规范),可通过 minimumSize 调整:

style: ButtonStyle(
      minimumSize: MaterialStateProperty.all(Size(100, 50)
    ),

6.2 避免过度重建

        对静态按钮使用 const 优化:

const TextButton(
  onPressed: _handleClick,
  child: Text('静态按钮'),
)

6.3 无障碍支持

const TextButton(
  onPressed: _handleClick,
  child: Text('静态按钮'),
)

 6.4 点击无响应

  • 检查 onPressed 是否为 null

  • 确认父组件未被 IgnorePointer 或 AbsorbPointer 包裹

  • 检测是否被其他组件覆盖(如透明层)

相关推荐

Flutter 基础组件 Text 详解-CSDN博客文章浏览阅读1.1k次,点赞42次,收藏25次。Text 组件是 Flutter 中最常用的 UI 组件之一,用于显示文本内容。它支持样式自定义、多行显示、溢出控制等功能,适用于各种文本场景。本文将详细介绍 Text 组件的使用方式及其重要参数。 https://shuaici.blog.csdn.net/article/details/146067083Flutter 基础组件 Scaffold 详解-CSDN博客文章浏览阅读494次,点赞21次,收藏23次。Scaffold 主要在 MaterialApp 主题下使用,它是实现Material Design基本视觉布局结构的Widget,它为应用提供了一个可定制的结构,包括 AppBar(应用栏)、Drawer(侧边栏)、FloatingActionButton(浮动按钮)、BottomNavigationBar(底部导航栏) 等。本文将详细解析 Scaffold 的功能和使用方法。 https://shuaici.blog.csdn.net/article/details/146067470

原文地址:https://blog.csdn.net/g984160547/article/details/146068020
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/583900.html

相关文章:

  • UFW 配置 Ubuntu 防火墙并设置防火墙规则
  • Spring Boot中引入Redis,以及RedisUtils完整工具类
  • 基于STM32F407ZGT6的硬件平台,(可选CubeMX) + PlatformIO软件开发的FreeRTOS部署指南
  • 什么是OF
  • 深入理解JavaScript构造函数与原型链:从原理到最佳实践
  • 《论语别裁》第01章 学而(24)五字串通五经
  • UE5与U3D引擎对比分析
  • hadoop第3课(hdfs shell)
  • 麒麟系统如何安装Anaconda
  • Day15:二叉树的后续遍历序列
  • C#中类‌的核心定义
  • 【存储中间件】Redis核心技术与实战(一):Redis入门与应用(常用数据结构:字符串String、哈希Hash、列表List)
  • LLM:了解大语言模型
  • OBS推WebRTC流,并添加毫秒级时间显示
  • K8S中的etcd数据库备份与恢复
  • 树莓百度百科更新!宜宾园区新业务板块全解析
  • 建筑兔零基础自学记录45|获取高德/百度POI-1
  • AI理论基础
  • 重生之我在学Vue--第12天 Vue 3 性能优化实战指南
  • SpaceSync智能排班:重构未来办公空间的神经中枢