当前位置: 首页 > article >正文

Flutter按钮控件(六)

1、常见按钮

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;

  
  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

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;

  
  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("漂浮按钮"),
            )
          ],
        ),
      ),
    );
  }
}
  • 效果图:
    自定义按钮样式

http://www.kler.cn/news/368782.html

相关文章:

  • 自由学习记录(15)
  • 龙芯+FreeRTOS+LVGL实战笔记(新)——13LVGL字体转换
  • Python数据分析——Numpy
  • linux下gpio模拟spi三线时序
  • 【笔记】软件测试09——接口测试
  • 鸿蒙原生应用开发及部署:首选华为云,开启HarmonyOS NEXT App新纪元
  • 冒泡排序和二分查找--go
  • 报错解决:opene3d draw_geometries(): incompatible function arguments.
  • 智能工厂的设计软件 意识能力(被动综合/主动把握/折衷解决):意识形态及认知计算机科学的架构、系统和运用
  • Linux学习_7
  • C++学习路线(二十六)
  • Mysql 数据库架构
  • 每天五分钟深度学习框架pytorch:从底层搭建多项式线性回归模型
  • 云原生后端(Cloud-Native Backend)
  • 【Web开发】什么是Nuxt? 利用Nuxt快速搭建前端项目
  • R语言笔记(二):向量
  • MySQL之数据库设计
  • ReactNative0.76版本发布,默认开启新架构
  • 【K8S系列】Kubernetes Service 基础知识 详细介绍
  • java和嵌入式现在哪个好?
  • 【深入理解SpringCloud微服务】Sentinel实战与原理剖析
  • 重修设计模式-行为型-迭代器模式
  • 了解光耦合器输入输出关系---腾恩科技
  • 区块链国赛题目--食品溯源(模块三)
  • 租房管理智能化:Spring Boot系统开发指南
  • VMware16去虚拟化 过CF 理论过TX游戏 WIN10过检测虚拟机