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

Flutter---Button

常见的三种按钮

TextButton扁平按钮

ElevatedButton凸起按钮

FloatingActionButton悬浮按钮

实现的效果图

常见的属性

children: [SizedBox(height: 20), // 间距控制// 1. 扁平按钮TextButton(onPressed: () => print('TextButton点击'),//点击回调//按钮风格设置style: TextButton.styleFrom(foregroundColor: Colors.red,//文字/图标颜色backgroundColor: Colors.grey[200],//背景色padding: EdgeInsets.all(16),//内边距shape: RoundedRectangleBorder( // 形状borderRadius: BorderRadius.circular(8),),),//按钮内容child: Text('扁平按钮'),),SizedBox(height: 20), // 间距控制// 2. 凸起按钮ElevatedButton(onPressed: () => print('ElevatedButton点击'),style: ElevatedButton.styleFrom(backgroundColor: Colors.blue, // 背景色foregroundColor: Colors.white, // 文字颜色elevation: 5, // 阴影高度minimumSize: Size(20, 50),       // 最小尺寸padding: EdgeInsets.all(16),       // 内边距shape: RoundedRectangleBorder(     // 形状borderRadius: BorderRadius.circular(10),),),child: Text('凸起按钮'),),SizedBox(height: 20),// 3. 悬浮按钮FloatingActionButton(onPressed: () => print('FAB点击'),child: Icon(Icons.add),),],

带图标的按钮

效果图

详细代码

//带图标的按钮
child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: [// 图标+文字按钮ElevatedButton.icon(onPressed: () {},icon: Icon(Icons.download),label: Text('下载'),),// 纯图标按钮IconButton(onPressed: () => print('图标按钮点击'),icon: Icon(Icons.settings),tooltip: '设置', // 长按提示),],),          

代码实例

main.dart

import 'dart:io';import 'package:flutter/material.dart'; //导入Material设计组件库void main() {runApp(const MyApp()); //应用入口
}class MyApp extends StatelessWidget { //应用根目录 (StatelessWidget不可变UI)const MyApp({super.key});//默认构造函数@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo', //应用标题(显示在任务管理器)theme: ThemeData(   //全局主题配置//colorScheme:自动生成的配色方案,textTheme:预定义的文字样式colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),),//首页,应用的第一个页面home: const MyHomePage(title: 'Flutter Demo Home Page'),//构造MyHomePage传入标题);}
}class MyHomePage extends StatefulWidget { //主页(有状态),(StatefulWidget可变UI)//构造函数const MyHomePage({super.key, required this.title});//接收title参数,required表示强制必须传递该参数final String title; //存储通过构造函数传入的标题@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> { //主页状态管理@overrideWidget build(BuildContext context) {return Scaffold( //页面骨架appBar: AppBar(//顶部应用栏组件//将AppBar的背景色设置为当前主题的反转主色backgroundColor: Theme.of(context).colorScheme.inversePrimary,title: Text(widget.title),//使用传入的标题),body: Center(//居中布局child: Column( //垂直排列子组件//mainAxisAlignment: MainAxisAlignment.center,children: [SizedBox(height: 20), // 间距控制// 1. 扁平按钮TextButton(onPressed: () => print('TextButton点击'),//点击回调//按钮风格设置style: TextButton.styleFrom(foregroundColor: Colors.red,//文字/图标颜色backgroundColor: Colors.grey[200],//背景色padding: EdgeInsets.all(16),//内边距shape: RoundedRectangleBorder( // 形状borderRadius: BorderRadius.circular(8),),),//按钮内容child: Text('扁平按钮'),),SizedBox(height: 20), // 间距控制// 2. 凸起按钮ElevatedButton(onPressed: () => print('ElevatedButton点击'),style: ElevatedButton.styleFrom(backgroundColor: Colors.blue, // 背景色foregroundColor: Colors.white, // 文字颜色elevation: 5, // 阴影高度minimumSize: Size(20, 50),       // 最小尺寸padding: EdgeInsets.all(16),       // 内边距shape: RoundedRectangleBorder(     // 形状borderRadius: BorderRadius.circular(10),),),child: Text('凸起按钮'),),SizedBox(height: 20),// 3. 悬浮按钮FloatingActionButton(onPressed: () => print('FAB点击'),child: Icon(Icons.add),),],),),);}
}

http://www.dtcms.com/a/482747.html

相关文章:

  • 网站建设需要敲代码吗信息流推广实施策划书
  • 广元网站建设电子商务网站建设主要内容
  • 4.4-中间件之gRPC
  • 网络与通信安全课程复习汇总1——课程导入
  • Qt模型控件:QTreeViewQTreeWidget
  • 河南省城乡建设厅官网廊坊快速优化排名
  • 网站建设应该考虑哪些问题企信宝
  • 青海做网站最好的公司互联网推广引流
  • 一天一个设计模式——装饰器模式
  • 婚恋交友 APP 核心功能分析:从匹配逻辑到用户体验的全链路设计
  • 用一个 prompt 搭建带 React 界面的 Java 桌面应用
  • 宁波建网站价格wordpress注明网站
  • wordpress添加网站地图黑龙江新闻头条最新消息
  • 机械臂装配自动化推动紧固件设计革新
  • JSAR 入门教程:从零开始开发空间天气小摆件
  • 【Pytorch】什么是梯度
  • 核间通信机制
  • 吕口*云蛇激光*VS*薄利魔刀*武打算法的方案
  • CSP-S模拟赛五总结(实际难度远低于提高组)
  • 网站建设服务器的配置wordpress 输出sql
  • 邵阳建设网站公司app在线生成平台 免费
  • 如何在第三方网站做推广什么关键词可以搜到那种
  • 深度解析 PostgreSQL 中的 ctid、xmin、xmax:从原理到实战
  • 2-sat
  • KPI、OKR 和 GS 的区别
  • 坂田网站建设费用明细wordpress 最近登录地址
  • 网站开发技术微信公众平台如何绑定网站
  • electron+react+esbuild入门项目
  • iOS 应用加固与苹果软件混淆指南,如何防止 IPA 被反编译与二次打包?
  • jsp电商网站怎么做网络营销是什么部门