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("漂浮按钮"),
            )
          ],
        ),
      ),
    );
  }
}
  • 效果图:
相关推荐
时光慢煮5 分钟前
Flutter 编译开发 OpenHarmony 全流程实战教程-基于开源仓库GitCode 搜索工具 v1.0.3 的跨平台实践
flutter·开源·gitcode
小白阿龙9 分钟前
鸿蒙+flutter 跨平台开发——Placeholder 控件的基础使用场景
flutter·华为·harmonyos·鸿蒙
时光慢煮10 分钟前
基于 Flutter × OpenHarmony 图书馆管理系统之构建书籍管理模块
flutter·华为·开源·openharmony
IT陈图图10 分钟前
智慧图书馆的数字名片:基于 Flutter × OpenHarmony 的读者卡片构建实践
flutter·鸿蒙·openharmony
南村群童欺我老无力.21 分钟前
Flutter 框架跨平台鸿蒙开发 - 打造专业级单位换算器,支持8大类50+单位互转
flutter·华为·harmonyos
南村群童欺我老无力.23 分钟前
Flutter 框架跨平台鸿蒙开发 - 井字棋游戏开发指南(含Minimax AI)
人工智能·flutter·华为·harmonyos
小白阿龙24 分钟前
鸿蒙+Flutter 跨平台开发——围棋辅助教学APP
flutter·华为·harmonyos·鸿蒙
小白阿龙25 分钟前
鸿蒙+flutter 跨平台开发——icon控件的响应式适配实现
flutter·华为·harmonyos·鸿蒙
2501_9445257626 分钟前
Flutter for OpenHarmony数独游戏App实战:笔记功能
笔记·flutter·游戏
2401_8823515228 分钟前
Flutter for OpenHarmony 商城App实战 - 积分实现
flutter