Flutter框架跨平台鸿蒙开发——Button组件基础

一、Button组件概述

Flutter提供了多种按钮组件,满足不同场景的需求。

Button类型

Button组件
ElevatedButton
TextButton
OutlinedButton
IconButton
FloatingActionButton

二、ElevatedButton

基础用法

dart 复制代码
ElevatedButton(
  onPressed: () {},
  child: Text('点击'),
)

带图标的按钮

dart 复制代码
ElevatedButton.icon(
  icon: Icon(Icons.add),
  label: Text('添加'),
  onPressed: () {},
)

三、TextButton

基础用法

dart 复制代码
TextButton(
  onPressed: () {},
  child: Text('文本按钮'),
)

四、OutlinedButton

基础用法

dart 复制代码
OutlinedButton(
  onPressed: () {},
  child: Text('轮廓按钮'),
)

五、完整示例

dart 复制代码
class ButtonBasicsExample extends StatelessWidget {
  const ButtonBasicsExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Button基础')),
      body: ListView(
        padding: EdgeInsets.all(16),
        children: [
          Card(
            child: Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('ElevatedButton'),
                  SizedBox(height: 16),
                  Row(
                    children: [
                      ElevatedButton(
                        onPressed: () {},
                        child: Text('普通'),
                      ),
                      SizedBox(width: 16),
                      ElevatedButton.icon(
                        icon: Icon(Icons.add),
                        label: Text('带图标'),
                        onPressed: () {},
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
          SizedBox(height: 16),
          Card(
            child: Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('TextButton'),
                  SizedBox(height: 16),
                  TextButton(
                    onPressed: () {},
                    child: Text('文本按钮'),
                  ),
                ],
              ),
            ),
          ),
          SizedBox(height: 16),
          Card(
            child: Padding(
              padding: EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('OutlinedButton'),
                  SizedBox(height: 16),
                  OutlinedButton(
                    onPressed: () {},
                    child: Text('轮廓按钮'),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net

相关推荐
里欧跑得慢17 分钟前
15. Web可访问性最佳实践:让每个用户都能平等访问
前端·css·flutter·web
nashane1 小时前
HarmonyOS Wi-Fi连接用户操作监听全解析:从系统弹框到Promise回调
华为·harmonyos·harmonyos 5
Lanren的编程日记3 小时前
Flutter 鸿蒙应用数据版本管理实战:版本记录+版本回退+版本对比,实现全链路数据版本控制
flutter·华为·harmonyos
我是大聪明.4 小时前
DeepSeek V4 Pro + 华为昇腾910:国产大模型落地的性能实测与深度解析
人工智能·华为
木斯佳5 小时前
HarmonyOS 本地存储实战:记账本案例改造实现日历联动
华为·harmonyos
李游Leo6 小时前
别让一张 12MB 的照片拖垮页面:ImageSource / PixelMap / ImagePacker 的工程化处理链路
harmonyos
nashane6 小时前
HarmonyOS 6学习:画中画(PiP)状态同步与场景化实战指南
学习·pip·harmonyos·harmonyos 5
@不误正业7 小时前
鸿蒙小艺智能体开放平台实战-接入系统级AI-Agent能力
人工智能·华为·harmonyos
MonkeyKing10 小时前
Flutter列表性能极致优化:从卡顿到丝滑
flutter·dart
IntMainJhy10 小时前
「Flutter三方库sqflite的鸿蒙化适配与实战指南:从入门到踩坑的本地数据库开发全记录」
数据库·flutter·华为·信息可视化·数据库开发·harmonyos