1. Flutter中给我们预先定义好了一些按钮控件给我们用,常用的按钮如下
  2.  
  3. RaisedButton :凸起的按钮,其实就是Android中的Material Design风格的Button ,继承自MaterialButton
  4. FlatButton :扁平化的按钮,继承自MaterialButton
  5. OutlineButton :带边框的按钮,继承自MaterialButton
  6. IconButton :图标按钮,继承自StatelessWidget
  7.  
  8. 我们先来看看MaterialButton中的属性,可以看到能设置的属性还是很多的。
  9.  
  10. const MaterialButton({
  11. Key key,
  12. @required this.onPressed,
  13. this.onHighlightChanged,
  14. this.textTheme,
  15. this.textColor,
  16. this.disabledTextColor,
  17. this.color,
  18. this.disabledColor,
  19. this.highlightColor,
  20. this.splashColor,
  21. this.colorBrightness,
  22. this.elevation,
  23. this.highlightElevation,
  24. this.disabledElevation,
  25. this.padding,
  26. this.shape,
  27. this.clipBehavior = Clip.none,
  28. this.materialTapTargetSize,
  29. this.animationDuration,
  30. this.minWidth,
  31. this.height,
  32. this.child,
  33. }) : super(key: key);
  34.  
  35. 下面我们来看看常用属性
  36. 属性 值类型 说明
  37. onPressed VoidCallback ,一般接收一个方法 必填参数,按下按钮时触发的回调,接收一个方法,传null表示按钮禁用,会显示禁用相关样式
  38. child Widget 文本控件
  39. textColor Color 文本颜色
  40. color Color 按钮的颜色
  41. disabledColor Color 按钮禁用时的颜色
  42. disabledTextColor Color 按钮禁用时的文本颜色
  43. splashColor Color 点击按钮时水波纹的颜色
  44. highlightColor Color 点击(长按)按钮后按钮的颜色
  45. elevation double 阴影的范围,值越大阴影范围越大
  46. padding EdgeInsetsGeometry (抽象类) 内边距
  47. shape ShapeBorder(抽象类) 设置按钮的形状
  48. minWidth double 最小宽度
  49. height double 高度
  50.  
  51. 而在Android中如果我们要修改按钮样式的话,需要通过selectorShape等方式进行修改,相比较Flutter来说是要麻烦不少的
  52. RaisedButton
  53.  
  54. RaisedButton的构造方法如下,由于继承自MaterialButton,所以MaterialButton中的大多数属性这边都能用,且效果一致,这里就不在赘述了
  55.  
  56. const RaisedButton({
  57. Key key,
  58. @required VoidCallback onPressed,
  59. ValueChanged<bool> onHighlightChanged,
  60. ButtonTextTheme textTheme,
  61. Color textColor,
  62. Color disabledTextColor,
  63. Color color,
  64. Color disabledColor,
  65. Color highlightColor,
  66. Color splashColor,
  67. Brightness colorBrightness,
  68. double elevation,
  69. double highlightElevation,
  70. double disabledElevation,
  71. EdgeInsetsGeometry padding,
  72. ShapeBorder shape,
  73. Clip clipBehavior = Clip.none,
  74. MaterialTapTargetSize materialTapTargetSize,
  75. Duration animationDuration,
  76. Widget child,
  77. })
  78.  
  79. 下面我们来看一下属性
  80.  
  81. onPressed
  82. 接收一个方法,点击按钮时回调该方法。如果传null,则表示按钮禁用
  83.  
  84. return RaisedButton(
  85. onPressed: null,
  86. );
  87.  
  88. 如下图所示
  1.  
  2. 下面我们定义一个方法传给onPressed
  3.  
  4. _log() {
  5. print("点击了按钮");
  6. }
  7.  
  8. @override
  9. Widget build(BuildContext context) {
  10. return RaisedButton(
  11. onPressed: _log,
  12. );
  13. }
  14.  
  1.  
  2. 点击按钮后会调用log方法。
  1.  
  2. child
  3. 按钮文本控件,一般都是传一个Text Widget
  4.  
  5. return RaisedButton(
  6. onPressed: _log,
  7. child: Text("浮动按钮"),
  8. );
  9.  
  1.  
  2. color
  3. 按钮的颜色
  4.  
  5. return RaisedButton(
  6. onPressed: _log,
  7. child: Text("浮动按钮"),
  8. color: Colors.red,
  9. );
  10.  
  1.  
  2. textColor
  3. 按钮的文本颜色
  4.  
  5. return RaisedButton(
  6. onPressed: _log,
  7. child: Text("浮动按钮"),
  8. color: Colors.red,
  9. textColor: Colors.white,
  10. );
  11.  
  1.  
  2. splashColor
  3. 点击按钮时水波纹的颜色
  4.  
  5. return RaisedButton(
  6. onPressed: _log,
  7. child: Text("浮动按钮"),
  8. color: Colors.red,
  9. textColor: Colors.white,
  10. splashColor: Colors.black,
  11.  
  12. );
  13.  
  1.  
  2. highlightColor
  3. 高亮颜色,点击(长按)按钮后的颜色
  4.  
  5. return RaisedButton(
  6. onPressed: _log,
  7. child: Text("浮动按钮"),
  8. color: Colors.red,
  9. textColor: Colors.white,
  10. splashColor: Colors.black,
  11. highlightColor: Colors.green,
  12. );
  13.  
  1.  
  2. elevation
  3. 阴影范围,一般不会设置太大
  4.  
  5. return RaisedButton(
  6. onPressed: _log,
  7. child: Text("浮动按钮"),
  8. color: Colors.red,
  9. textColor: Colors.white,
  10. splashColor: Colors.black,
  11. highlightColor: Colors.green,
  12. elevation: 30,
  13. );
  14.  
  1.  
  2. padding
  3. 内边距,其接收值的类型是EdgeInsetsGeometry类型的,EdgeInsetsGeometry是一个抽象类,我们来看看其实现类
  1.  
  2. 我们一般都用EdgeInsets类中的方法来设置padding
  3. 常用方法如下
  4.  
  5. //单独设置左上右下的间距,四个参数都要填写
  6. const EdgeInsets.fromLTRB(this.left, this.top, this.right, this.bottom);
  7.  
  8. //单独设置左上右下的间距,四个均为可选参数
  9. const EdgeInsets.only({
  10. this.left = 0.0,
  11. this.top = 0.0,
  12. this.right = 0.0,
  13. this.bottom = 0.0
  14. });
  15.  
  16. //一次性设置上下左右的间距
  17. const EdgeInsets.all(double value)
  18. : left = value, top = value, right = value, bottom = value;
  19.  
  20. 示例:
  21.  
  22. EdgeInsets.all()
  23.  
  24. padding: EdgeInsets.all(20)
  25.  
  1. EdgeInsets.fromLTRB()
  2.  
  3. padding: EdgeInsets.fromLTRB(0,30,20,40)
  4.  
  1.  
  2. EdgeInsets.only()
  3.  
  4. padding: EdgeInsets.only(top: 30)
  5.  
  1.  
  2. shape
  3. shape用来设置按钮的形状,其接收值是ShapeBorder类型,ShapeBorder是一个抽象类,我们来看看有哪些实现类
  4.  
  1.  
  2. 可以看到,实现类还是很多的,我们主要来看看常用的即可。
  3.  
  4. BeveledRectangleBorder 带斜角的长方形边框
  5. CircleBorder 圆形边框
  6. RoundedRectangleBorder 圆角矩形
  7. StadiumBorder 两端是半圆的边框
  8.  
  9. 我们来简单用一用,在用之前我们先来看一下
  10. 常用的两个属性
  11.  
  12. side 用来设置边线(颜色,宽度等)
  13. borderRadius 用来设置圆角
  14.  
  15. side接收一个BorderSide类型的值,比较简单,这里就不介绍了,看一下构造方法
  16.  
  17. const BorderSide({
  18. this.color = const Color(0xFF000000),
  19. this.width = 1.0,
  20. this.style = BorderStyle.solid,
  21. })
  22.  
  23. borderRadius 接收一个BorderRadius类型的值,常用方法如下
  1.  
  2. 我们可以把borderRadius分为上下左右四个方向,下面的方法都是对这四个方向进行设置,
  3.  
  4. all 配置所有方向
  5. cricular 环形配置,跟all效果差不多,直接接收double类型的值
  6. horizontal 只配置左右方向
  7. only 可选左上,右上,左下,右下配置
  8. vertical 只配置上下方向
  9.  
  10. 具体配置大家自行尝试
  11.  
  12. 我们来简单用一下
  13.  
  14. BeveledRectangleBorder
  15. 带斜角的长方形边框
  16.  
  17. shape: BeveledRectangleBorder(
  18. side: BorderSide(
  19. color: Colors.white,
  20. ),
  21. borderRadius: BorderRadius.all(Radius.circular(10))
  22. ),
  23.  
  1.  
  2. CircleBorder
  3. 圆形边框
  4.  
  5. shape: CircleBorder(
  6. side: BorderSide(
  7. color: Colors.white,
  8. ),
  9. ),
  10.  
  1.  
  2. RoundedRectangleBorder
  3. 圆角矩形
  4.  
  5. shape: RoundedRectangleBorder(
  6. borderRadius: BorderRadius.all(Radius.circular(10)),
  7. ),
  8.  
  1.  
  2. StadiumBorder
  3. 两端是半圆的边框
  4.  
  5. shape: StadiumBorder(),
  6.  
  1. FlatButton
  2.  
  3. FlatButtonRaisedButton用法基本一致,下面我们就直接用一下
  4.  
  5. /*扁平按钮*/
  6. class FlatBtn extends StatelessWidget {
  7. _log() {
  8. print("点击了扁平按钮");
  9. }
  10.  
  11. @override
  12. Widget build(BuildContext context) {
  13. return FlatButton(
  14. onPressed: _log,
  15. child: Text("扁平按钮"),
  16. color: Colors.blue,
  17. textColor: Colors.black,
  18. shape: RoundedRectangleBorder(
  19. side: BorderSide(
  20. color: Colors.white,
  21. width: 1,
  22. ),
  23. borderRadius: BorderRadius.circular(8)),
  24. );
  25. }
  26. }
  27.  
  1. OutlineButton
  2.  
  3. 注意,OutlineButton是一个有默认边线且背景透明的按钮,也就是说我们设置其边线和颜色是无效的,其他属性跟MaterialButton中属性基本一致
  4.  
  5. 下面我们直接来使用
  6.  
  7. /*带边线的按钮*/
  8. class outlineBtn extends StatelessWidget {
  9. _log() {
  10. print("点击了边线按钮");
  11. }
  12.  
  13. @override
  14. Widget build(BuildContext context) {
  15. // TODO: implement build
  16. return OutlineButton(
  17. onPressed: _log,
  18. child: Text("边线按钮"),
  19. textColor: Colors.red,
  20. splashColor: Colors.green,
  21. highlightColor: Colors.black,
  22. shape: BeveledRectangleBorder(
  23. side: BorderSide(
  24. color: Colors.red,
  25. width: 1,
  26. ),
  27. borderRadius: BorderRadius.circular(10),
  28. ),
  29. );
  30. }
  31. }
  32.  
  33. 效果如下:
  1. IconButton
  2.  
  3. IconButton是直接继承自StatelessWidget的,默认没有背景
  4. 我们来看一下他的构造方法
  5.  
  6. const IconButton({
  7. Key key,
  8. this.iconSize = 24.0,
  9. this.padding = const EdgeInsets.all(8.0),
  10. this.alignment = Alignment.center,
  11. @required this.icon,
  12. this.color,
  13. this.highlightColor,
  14. this.splashColor,
  15. this.disabledColor,
  16. @required this.onPressed,
  17. this.tooltip
  18. })
  19.  
  20. 可以看到,icon是必填参数
  21.  
  22. icon接收一个Widget,但是一般我们都是传入一个Icon Widget
  23.  
  24. final Widget icon;
  25.  
  26. 其他属性跟MaterialButton中的属性用法基本一致
  27.  
  28. 我们来用一下
  29.  
  30. /*图标按钮*/
  31. class IconBtn extends StatelessWidget {
  32. _log() {
  33. print("点击了图标按钮");
  34. }
  35.  
  36. @override
  37. Widget build(BuildContext context) {
  38. return IconButton(
  39. icon: Icon(Icons.home),
  40. onPressed: _log,
  41. color: Colors.blueAccent,
  42. highlightColor: Colors.red,
  43. );
  44. }
  45. }
  46.  
  47. 效果如下
  48.  
  1.  
  2. 我们也可以传一个Text或其他Widget,这个大家自行尝试吧
  3.  
  4. FlutterButton内容大概就是这些

Flutter基础Widget之按钮(RaisedButton、FlatButton、OutlineButton,IconButton)的更多相关文章

  1. Flutter 基础组件:按钮

    前言 Material组件库中提供了多种按钮组件如RaisedButton.FlatButton.OutlineButton等,它们都是直接或间接对RawMaterialButton组件的包装定制,所 ...

  2. Flutter 基础控件

    内容: Button Image.Icon Switch.Checkbox TextField Form 1.Button RaisedButton 漂浮按钮 FlatButton 扁平按钮 Outl ...

  3. Flutter 基础组件:输入框和表单

    前言 Material组件库中提供了输入框组件TextField和表单组件Form. 输入框TextField 接口描述 const TextField({ Key key, // 编辑框的控制器,通 ...

  4. Bootstrap<基础十四> 按钮下拉菜单

    使用 Bootstrap class 向按钮添加下拉菜单.如需向按钮添加下拉菜单,只需要简单地在在一个 .btn-group 中放置按钮和下拉菜单即可.也可以使用 <span class=&qu ...

  5. Flutter基础用法解析

    解析开始 Flutter中一切皆widget,一切皆组件.学习Flutter中,必须首先了解Flutter的widget.先从最基本的MaterialApp和Scaffold开始了解 1 Materi ...

  6. Flutter 基础组件:Widget简介

    概念 在Flutter中几乎所有的对象都是一个Widget.与原生开发中"控件"不同的是,Flutter中的Widget的概念更广泛,它不仅可以表示UI元素,也可以表示一些功能性的 ...

  7. Flutter基础系列之混合开发(二)

    1.混合开发的场景 1.1作为独立页面加入 这是以页面级作为独立的模块加入,而不是页面的某个元素. 原生页面可以打开Flutter页面 Flutter页面可以打开原生页面 1.2作为页面的一部分嵌入 ...

  8. Flutter中的浮动按钮 FloatingActionButton

    FloatingActionButton 简称 FAB ,可以实现浮动按钮,也可以实现类似闲鱼 app 的底部凸起导航 . 常用属性 FloatingActionButton的常用属性,同flutte ...

  9. Flutter调研(1)-Flutter基础知识

    工作需要,因客户端有部分页面要使用flutter编写,需要QA了解一下flutter相关知识,因此,做了flutter调研,包含安装,基础知识与demo编写,第二部分是安装与环境配置. —— Flut ...

随机推荐

  1. spring 配置中相关属性的含义:

    1:parent元素属性 一个bean定义可能会包含大量的配置信息,包括容器相关的信息(比如初始化方法,静态工厂方法等等)以及构造函数参数和属性的值.一个child bean定义是一个能够从paren ...

  2. Phpstorm 无法自动断点 Exception

    困扰了很久的问题了. ... 今天终于解决了,直接说解决方案吧. 在 php.ini 中增加一行配置: xdebug.default_enable = 1,然后重启Apache服务器:sudo ser ...

  3. Oracle数据库误删文件导致rman备份报错RMAN-06169解决办法

    Oracle数据库误删文件导致rman备份报错RMAN-06169解决办法 可能是误删文件导致在使用rman备份时候出现以下提示 RMAN-06169: could not read file hea ...

  4. explain 分析 聚合统计语句的性能

    EXPLAIN SELECT COUNT(1) FROM question; id select_type table partitions type possible_keys key key_le ...

  5. sql_q.format

    field_q_insert = 'id, title, number, created,content'sql_q = 'INSERT INTO testquestion ({}) VALUES ( ...

  6. 关于Python装饰器内层函数为什么要return目标函数的一些个人见解

    https://blog.csdn.net/try_test_python/article/details/80802199 前几天在学装饰器的时候,关于装饰器内层函数调用目标函数时是否return目 ...

  7. Spark-RDD算子

    一.Spark-RDD算子简介 RDD(Resilient Distributed DataSet)是分布式数据集.RDD是Spark最基本的数据的抽象. scala中的集合.RDD相当于一个不可变. ...

  8. 【JEECG技术博文】JEECG表单配置-树形表单

    表单配置支持树型表单了,详细效果例如以下图:

  9. IOS使用Core-Plot画折线图

    关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...

  10. innobackupex 还原和备份实例

    InnoDB 和非 InnoDB 文件的备份都是通过拷贝文件来做的,但是实现的方式不同,前者是以page为粒度做的(xtrabackup),后者是 cp 或者 tar 命令(innobackupex) ...