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

相关推荐
大雷神9 分钟前
HarmonyOS ArkGraphics 2D可变帧率实操——同屏对比30、60和90 FPS动画
华为·harmonyos
OH_TPC21 分钟前
【鸿蒙优选三方库】@react-native-ohos/react-native-fast-image:高性能图片加载与缓存组件
缓存·华为·harmonyos·鸿蒙
Crazy_MT1 小时前
Android Studio 运行 Flutter iOS 真机白屏,但 Xcode 和命令行正常的排查记录
flutter·android studio
贾伟康2 小时前
【句匠|03】HarmonyOS ArkTS 每日打卡实战:实现连续天数、补签边界和本地日期判断
harmonyos·arkts·本地存储·每日打卡·学习应用
Android-Flutter2 小时前
flutter async/await 详解
android·flutter
小雨青年2 小时前
【HarmonyOS 7 平行视界深度实战】02 从普通页面到第一个平行视界 Demo
华为·harmonyos
~远在太平洋~2 小时前
06-鸿蒙系统 uitest UI 自动化指南
ui·自动化·harmonyos
OH_TPC2 小时前
OpenHarmony平台RN三方库适配情况
华为·harmonyos·鸿蒙
坚果的博客3 小时前
从 0 到 1:用 CJMP 开发「每日早报」鸿蒙应用完整实战
华为·harmonyos
SoaringHeart3 小时前
Flutter 进阶 | 组件封装:用 CustomPainter 实现光环动画组件AnimatedHalo
前端·flutter