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("漂浮按钮"),
            )
          ],
        ),
      ),
    );
  }
}
  • 效果图:
相关推荐
程序员老刘6 小时前
2025年Flutter状态管理新趋势:AI友好度成为技术选型第一标准
flutter·ai编程·客户端
AGG_Chan10 小时前
flutter专栏--深入了解widget原理
开发语言·javascript·flutter
墨客希12 小时前
通俗易懂的理解Vue.js
vue.js·flutter
—Qeyser14 小时前
Flutter bottomNavigationBar 底部导航栏
flutter
ZFJ_张福杰14 小时前
【Flutter】APP的数据安全(基于Flutter 交易所APP的总结)
flutter·web3·区块链·app·交易所
Bryce李小白4 天前
Flutter 自定义 View 权威指引
flutter
恋猫de小郭4 天前
Fluttercon EU 2025 :Let‘s go far with Flutter
android·开发语言·flutter·ios·golang
SoaringHeart5 天前
Flutter进阶:自定义一个 json 转 model 工具
前端·flutter·dart
许泽宇的技术分享5 天前
Flutter + Ollama:开启本地AI的全平台新纪元 —— 从零剖析一款现代化AI客户端的技术奥秘
人工智能·flutter