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

相关推荐
鸿蒙开发8 小时前
鸿蒙(HarmonyOS NEXT)表单校验别再手撸正则了 —— 我写了个 ArkTS 版 zod
harmonyos
TrisighT9 小时前
ArkTS 的 @BuilderParam 你八成只用了皮毛——那个尾随闭包写法差点被我当 bug 删了
harmonyos·arkts·arkui
恋猫de小郭9 小时前
Amper 正式转正 Kotlin Toolchain ,Gradle 未来何去何从
android·前端·flutter
张风捷特烈10 小时前
Flutter 类库大揭秘#02 | path_provider 各平台实现
前端·flutter
ONEDAY1 天前
HarmonyOS 多 Product 构建实践:一套代码生成多个产物
harmonyos
TT_Close1 天前
别劝退了!5秒搞定 Flutter 鸿蒙 FVM 起跑线
flutter·harmonyos·visual studio code
TrisighT1 天前
ArkTS 列表滚动时为什么会闪现旧数据?我扒了 LazyForEach 的复用逻辑
harmonyos·arkts·arkui
MonkeyKing1 天前
鸿蒙ArkTS深度剖析:ArkTS与TS/JS核心差异、静态强类型实战优势
typescript·harmonyos
TrisighT1 天前
Electron鸿蒙PC上写日志文件,我被权限和路径坑了两次
electron·harmonyos
你听得到111 天前
用户说 App 卡,但说不清在哪?我把 Flutter 监控 SDK 升级成了链路观测工作台
前端·flutter·性能优化