Flutter按钮控件(六)

1、常见按钮

dart 复制代码
import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    home: MyHomePage(title: "按钮控件"),
  ));
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text(title),
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {},
              child: const Text("ElevatedButton"),
            ),
            // 在按钮之间添加一个高度为20的盒子,将按钮分开适当距离
            const SizedBox(height: 20),
            TextButton(
              onPressed: () {},
              child: const Text("TextButton"),
            ),
            const SizedBox(height: 20),
            FloatingActionButton(
              onPressed: () {},
              child: const Icon(Icons.account_circle),
            ),
            const SizedBox(height: 20),
            OutlinedButton(
              onPressed: () {},
              child: const Text("OutlinedButton"),
            ),
            const SizedBox(height: 20),
            IconButton(
              onPressed: () {},
              icon: const Icon(Icons.access_alarms_sharp),
            ),
          ],
        ),
      ),
    );
  }
}
  • ElevatedButton:具有默认样式(如阴影、圆角等)的实心按钮;

  • TextButton:简单的文本按钮,通常用于对话框或底部表单等位置;

  • OutlinedButton:与 ElevatedButton 类似,但是没有背景色,只有边框;

  • IconButton:使用图标而不是文本的按钮;

  • FloatingActionButton:用于主要的操作,显示为悬浮在屏幕上的圆形按钮;

  • 效果图:

2、自定义按钮样式

以ElevatedButton为例,自定义一些常见属性,具体属性查看文档:https://api.flutter.dev/flutter/material/ElevatedButton-class.html

dart 复制代码
import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    home: MyHomePage(title: "按钮控件"),
  ));
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text(title),
        ),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {},
              style: ElevatedButton.styleFrom(
                // 修改文本颜色,也可以直接修改文本颜色
                foregroundColor: Colors.blue,
                // 按钮的背景色
                backgroundColor: Colors.yellow,
                // 点击按钮波纹反馈的颜色
                overlayColor: Colors.grey,
                // 按钮阴影颜色
                shadowColor: Colors.red,
                // 设置按钮圆角
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8.0)),
              ),
              child: const Text("漂浮按钮"),
            )
          ],
        ),
      ),
    );
  }
}
  • 效果图:
相关推荐
L、2181 小时前
Flutter + OpenHarmony + 区块链:构建去中心化身份认证系统(DID 实战)
flutter·华为·去中心化·区块链·harmonyos
西西学代码1 小时前
Flutter---右滑显示删除按钮
flutter
kirk_wang2 小时前
Flutter app_settings 库在鸿蒙(OHOS)平台的适配实践与解析
flutter·移动开发·跨平台·arkts·鸿蒙
小a彤2 小时前
Flutter 原生开发指南
flutter
L、2182 小时前
Flutter + OpenHarmony 全栈实战:打造“鸿蒙智联”智能家居控制中心(系列终章)
flutter·华为·智能手机·electron·智能家居·harmonyos
song5013 小时前
鸿蒙 Flutter 日志系统:分级日志与鸿蒙 Hilog 集成
图像处理·人工智能·分布式·flutter·华为
松☆3 小时前
深入实战:Flutter + OpenHarmony 分布式软总线通信完整实现指南
分布式·flutter
段子子3 小时前
【flutter创建与配置】
flutter
song5014 小时前
鸿蒙 Flutter 插件测试:多版本兼容性自动化测试
人工智能·分布式·flutter·华为·开源鸿蒙
kirk_wang4 小时前
Flutter tobias 库在鸿蒙端的支付宝支付适配实践
flutter·移动开发·跨平台·arkts·鸿蒙