热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

FlutterUIbutton系Widget

Android社区提供在线查看系统原代码,及一系列的高质量文章,让您紧跟Andr

Flutter 中按钮这种 widget 种类可是不少:

RaisedButton - 凸起按钮,带阴影
RaisedButton - 扁平按钮,不带阴影
OutlineButton - 自带边框的按钮,不能设置背景色 color
MaterialButton - MD 风格的按钮,可以设置宽高值,是 Flutter 中所有按钮的基类
IconButton - 在 Icon 基础上加上点击事件,不能设置背景色和边框
FloatingActionButton - 悬浮按钮
ButtonBar 按钮组
RawMaterialButton 横向按钮组

MaterialButton

MaterialButton 是 Flutter 中所有 Button 类型的基类, MaterialButton 的参数再其他 Button 中都是通用的,我们简单看一下:

const MaterialButton({
Key key,
@required this.onPressed, // 点击事件,必须给值,写null的话表示不可用
this.onHighlightChanged,
this.textTheme, //默认文字样式
this.textColor, // 文字颜色
this.disabledTextColor, // 不可用时文字颜色
this.color, // 背景色
this.disabledColor, // 按钮被禁用时的文字颜色
this.highlightColor, // 按钮的水波纹亮起的颜色,点击之后的颜色,注意这个颜色点击之后就一直在这里了,不是那种一瞬间显示
this.splashColor, // 波浪纹颜色
this.colorBrightness, // 按钮主题高亮
this.elevation, // Z轴、阴影,正常应该小于 5
this.highlightElevation, // 按钮高亮时的下面的阴影长度
this.disabledElevation,
this.padding,
this.shape, // 按钮样式
this.clipBehavior = Clip.none,
this.materialTapTargetSize,
this.animationDuration,
this.minWidth,
this.height,
this.child, // 按钮内部widget,一般都是text
})
1. textTheme 系统默认提供的配色方案:
ButtonTextTheme.normal:

ButtonTextTheme.accent:

ButtonTextTheme.primary:

2. shape 提供了4种预设的按钮形状:
BeveledRectangleBorder

CircleBorder

RoundedRectangleBorder

StadiumBorder

3. 按钮按下时图示:

RaisedButton

RaisedButton - 凸起按钮,带阴影的那种

class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
print("AAAAAA");
},
color: Colors.blueAccent,
child: Text(
"浮动按钮",
style: TextStyle(fontSize: 30),
),
textColor: Colors.white,
elevation: 10,
disabledColor: Colors.grey,
padding: EdgeInsets.all(20),
splashColor: Colors.red,
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10),
));
}
}

FlatButton

FlatButton - 扁平按钮,不能带阴影,其他和 RaisedButton 一样

class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FlatButton(
onPressed: () {
print("AAAAAA");
},
color: Colors.blueAccent,
child: Text(
"浮动按钮",
style: TextStyle(fontSize: 30),
),
textColor: Colors.white,
disabledColor: Colors.grey,
padding: EdgeInsets.all(20),
splashColor: Colors.red,
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10),
));
}
}

OutlineButton

OutlineButton - 自带边框的按钮,不能设置背景色 color

class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new OutlineButton(
onPressed: () {
print(BorderStyle.values);
},
borderSide: BorderSide(
color: Colors.blue,
width: 2.0,
style: BorderStyle.solid
),
child: new Text('pressed'),
);
}
}

MaterialButton

MaterialButton - MD 风格的按钮,属性同上,但是可以设置宽高值

class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialButton(
minWidth: 200,
height: 80,
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
print("AAAAAA");
},
child: new Text('button'),
);
}
}

IconButton

IconButton - 在 Icon 基础上加上点击事件,不能设置背景色和边框,图就没必要放了, child 除了可以接受 Icon 之外,还可以接受 RichText

class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return IconButton(
onPressed: () {
print("AAAAAA");
},
color: Colors.red,
iconSize: 50,
icon: new Icon(Icons.star),
padding: const EdgeInsets.all(12.0)
);
}
}

FloatingActionButton


基本设置

FloatingActionButton 这个大家都是耳熟能详了吧,如果有时间,可以看看:FlatButton的Material设计理念,这是官方指导, FloatingActionButton 所有典型应用案例都在里面了,虽说是英文的,但是大家看图就知道咋回事了图随便找的

child 很随意,可以是 Text、icon, etc
tooltip 长按提示
foregroundColor child 没有设置颜色时生效
backgroundColor 背景色
elevation 阴影
highlightElevation 按下时阴影
shape 形状,设置shape时,默认的elevation将会失效,默认为CircleBorder
mini 是小图标还是大图标
heroTag hero效果使用的tag,系统默认会给所有FAB使用同一个tag,方便做动画效果
isExtended 是否为”extended”类型

@override
Widget build(BuildContext context) {
return FloatingActionButton(
child: Icon(Icons.adb),
tooltip: "tips",
backgroundColor: Colors.blueAccent,
foregroundColor: Colors.amberAccent,
onPressed: (){
print("tips!");
},
);
}
FloatingActionButton 3种样式
regular 大
mini 小
extended 扩展

regular 和 mini 就是按钮大小的事,通过 min true/false 就能切换,但是 extended 就得借助 FloatingActionButton.extended 这个专门的构造函数了,其样式是 icon 在前,文字在后,没有 child 标签了,而是 icon 和 label 了

FloatingActionButton 定位问题

android 的 FloatingActionButton 是可以定位的,Flutter 也一样,甚至还可以和顶部的 ActionBar 底部的 BottomNavigationBar 联动,经典的样式就是这样:

一共有6个位置:

一般定位:
endFloat 右小角,距离底部有一定距离
endDocked 右小角,顶着底部显示

centerFloat 居中下至,距离底部有一定距离

联动定位:
startTop 左上角,距离左边有一定距离,锚定 ActionBar 中间
endTop 右上角,距离右边有一定距离,锚定 ActionBar 中间
centerDocked 居中下至,锚定 BottomNavigationBar 中间

和 BottomNavigationBar 联动的例子

return new Scaffold(
floatingActionButton: new Builder(builder: (BuildContext context) {
return new FloatingActionButton(
child: const Icon(Icons.add),
tooltip: "Hello",
heroTag: null,
foregroundColor: Colors.white,
backgroundColor: Colors.black,
elevation: 7.0,
highlightElevation: 14.0,
);
}),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.black12,
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.red,
textTheme: Theme.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.white))),
child: new BottomNavigationBar(
fixedColor: Colors.lightBlue,
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.home), title: new Text("每日干货")),
new BottomNavigationBarItem(
icon: new Icon(Icons.category),
title: new Text("分类阅读"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.whatshot),
title: new Text("匠心写作"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.search),
title: new Text("搜索"),
),
],
type: BottomNavigationBarType.fixed,
onTap: (int selected) {
setState(() {
});
},
),
),
);

ButtonBar

这个比较简单

alignment 布局方向,默认 MainAxisAlignment.end
mainAxisSize 主轴大小,默认 MainAxisSize.max
children

RawMaterialButton

RawMaterialButton 和上面差不舵,也是一组按钮,没有背景 没有边框 有点击效果的按钮。其实写不写都没事,主要是让大家认识下,别觉得没见过

和 FlatButton 没有什么区别


推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
author-avatar
缪宇驰
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有