RaisedButton

  • Material Design 的按钮,一个凸起的材质矩形按钮
  1. RaisedButton({
  2. // 键
  3. Key key,
  4. // 按钮点击事件监听,当返回null则认为是禁用
  5. @required VoidCallback onPressed,
  6. // 点击按钮时返回true,当按钮松开手,或焦点离开按钮时会返回false
  7. ValueChanged<bool> onHighlightChanged,
  8. // 按钮文本颜色主题(未设置按钮文本颜色时有效),与ThemeData值有关。
  9. ButtonTextTheme textTheme,
  10. // 按钮文本颜色
  11. Color textColor,
  12. // 按钮禁用时文本颜色
  13. Color disabledTextColor,
  14. // 按钮的颜色
  15. Color color,
  16. // 按钮禁用时的颜色
  17. Color disabledColor,
  18. // 具有输入焦点的按钮的颜色(没搞清楚作用,感觉是Web端的属性)
  19. Color focusColor,
  20. // 指针悬停在按钮上时的颜色(没搞清楚作用,感觉是Web端的属性)
  21. Color hoverColor,
  22. // 当按钮处于按下状态时,高亮突出的颜色
  23. Color highlightColor,
  24. // 水波纹溅射颜色
  25. Color splashColor,
  26. // 和字体颜色有关,Brightness枚举类型-dark和light(在未设置字体颜色时,dark模式文字为白色,light文字为黑色)
  27. Brightness colorBrightness,
  28. // 和阴影投影范围有关
  29. double elevation,
  30. // 焦点高度(应该是聚焦情况下,对应elevation的值)
  31. double focusElevation,
  32. // 悬停高度(应该是悬停情况下,对应elevation的值)
  33. double hoverElevation,
  34. // 高亮高度(高亮情况下,对应elevation的值)
  35. double highlightElevation,
  36. // 禁用高度(按钮禁用情况下,对应elevation的值)
  37. double disabledElevation,
  38. // 按钮内边距
  39. EdgeInsetsGeometry padding,
  40. // 按钮的形状
  41. ShapeBorder shape,
  42. // 剪切行为(设置溢出部分内容是否剪切,且以何种方式剪切)
  43. Clip clipBehavior,
  44. // 焦点节点(暂时不知道有什么用处)
  45. FocusNode focusNode,
  46. // 自动聚焦(暂时不知道有什么用处)
  47. bool autofocus = false,
  48. // 点击范围大小(MaterialTapTargetSize枚举类型padded拓展最小点击范围为48px*48px,按实际最小来)、该属性具体适用范围不明
  49. MaterialTapTargetSize materialTapTargetSize,
  50. // 动画时长(定义[形状]和[高程]的动画更改的持续时间,默认为200ms)
  51. Duration animationDuration,
  52. // 按钮的内容,不一定是Text,也可以是Icon等Widget
  53. Widget child,
  54. })
  • 按钮效果图

image.png

  • textTheme属性全解.RaisedButton.textTheme属性值为ButtonTextTheme枚举类的对象,总共有三个值,分别为normal、accent及primary三个值。normal:按钮文本是黑色还是白色,取决于[ThemeData.brightness];accent:按钮文本为[ThemeData.accentColor]

  • 按钮禁用:当设置onPressed属性值为null时,则按钮为禁用状态,没有单独的属性设置按钮是否禁用

  • clipBehavior属性,用于从child溢出是否剪裁,Clip枚举类型不同值效果截图Clip.none(不剪裁)、Clip.hardEdget(剪裁但是边缘未开启抗锯齿模式)、Clip.antiAlias(剪裁且开启抗锯齿模式)、Clip.antiAliasWithSaveLayer(功能与Clip.antiAlias基本相同,该模式具有缓存,且处理比较慢)

image.pngimage.pngimage.pngimage.png

  • animationDuration按钮形状与elevation高度动画变更持续时间效果,以下为200ms和5000ms效果展示:

动画默认201.gif动画默认5000ms.gif

FloatingActioButton

  • 一个原形图标按钮,它悬停在内容之上,以展示应用程序中的主要动作。FloatingActionButton通常用于Scaffold.floatingActionButton字段。
  1. FloatingActionButton({
  2. // 主键
  3. Key key,
  4. // 一般为Text或者Icon
  5. this.child,
  6. // 长按后文字提示
  7. this.tooltip,
  8. // 前景颜色
  9. this.foregroundColor,
  10. // 背景颜色
  11. this.backgroundColor,
  12. // 聚焦颜色
  13. this.focusColor,
  14. // 悬停颜色
  15. this.hoverColor,
  16. // 水波纹溅射颜色
  17. this.splashColor,
  18. // hero动画标记
  19. this.heroTag = const _DefaultHeroTag(),
  20. // 和阴影投影范围有关
  21. this.elevation,
  22. // 焦点高度(应该是聚焦情况下,对应elevation的值)
  23. this.focusElevation,
  24. // 悬停高度(应该是悬停情况下,对应elevation的值)
  25. this.hoverElevation,
  26. // 高亮高度(高亮情况下,对应elevation的值)
  27. this.highlightElevation,
  28. // 禁用高度(按钮禁用情况下,对应elevation的值)
  29. this.disabledElevation,
  30. // 按钮点击事件监听,值为null时,按钮为禁用模式
  31. @required this.onPressed,
  32. // 控制此按钮的大小。默认情况下,浮动操作按钮是非迷你按钮,其高度和宽度为56.0逻辑像素。迷你浮动动作按钮的高度和宽度为40.0逻辑像素,布局宽度和高度为48.0逻辑像素。
  33. this.mini = false,
  34. // 按钮形状
  35. this.shape,
  36. // 剪切行为(设置溢出部分内容是否剪切,且以何种方式剪切)、具体参考RaisedButton.clipBehavior属性说明
  37. this.clipBehavior = Clip.none,
  38. // 焦点节点(暂时不知道有什么用处)
  39. this.focusNode,
  40. // 自动聚焦(暂时不知道有什么用处)
  41. this.autofocus = false,
  42. // 点击范围大小(MaterialTapTargetSize枚举类型padded拓展最小点击范围为48px*48px,按实际最小来)、该属性具体适用范围不明
  43. this.materialTapTargetSize,
  44. // 是否是拓展(作用不是很清楚,和悬浮按钮点击事件有关)
  45. this.isExtended = false,
  46. })
  • FloatingActionButton按钮效果图

image.pngimage.png

FlatButton

  • 一个扁平的Material按钮,属性基本与RaiseButton相同
  1. FlatButton({
  2. // 键
  3. Key key,
  4. // 按钮点击事件监听,当返回null则认为是禁用
  5. @required VoidCallback onPressed,
  6. // 点击按钮时返回true,当按钮松开手,或焦点离开按钮时会返回false
  7. ValueChanged<bool> onHighlightChanged,
  8. // 按钮文本颜色主题(未设置按钮文本颜色时有效),与ThemeData值有关。
  9. ButtonTextTheme textTheme,
  10. // 按钮文本颜色
  11. Color textColor,
  12. // 按钮禁用时文本颜色
  13. Color disabledTextColor,
  14. // 按钮的颜色
  15. Color color,
  16. // 按钮禁用时的颜色
  17. Color disabledColor,
  18. // 具有输入焦点的按钮的颜色(没搞清楚作用,感觉是Web端的属性)
  19. Color focusColor,
  20. // 指针悬停在按钮上时的颜色(没搞清楚作用,感觉是Web端的属性)
  21. Color hoverColor,
  22. // 当按钮处于按下状态时,高亮突出的颜色
  23. Color highlightColor,
  24. // 水波纹溅射颜色
  25. Color splashColor,
  26. // 和字体颜色有关,Brightness枚举类型-dark和light(在未设置字体颜色时,dark模式文字为白色,light文字为黑色)
  27. Brightness colorBrightness,
  28. // 按钮内边距
  29. EdgeInsetsGeometry padding,
  30. // 按钮形状
  31. ShapeBorder shape,
  32. // 剪切行为(设置溢出部分内容是否剪切,且以何种方式剪切)、具体参考RaisedButton.clipBehavior属性说明
  33. Clip clipBehavior,
  34. // 焦点节点(暂时不知道有什么用处)
  35. FocusNode focusNode,
  36. // 自动聚焦(暂时不知道有什么用处)
  37. bool autofocus = false,
  38. // 点击范围大小(MaterialTapTargetSize枚举类型padded拓展最小点击范围为48px*48px,按实际最小来)、该属性具体适用范围不明
  39. MaterialTapTargetSize materialTapTargetSize,
  40. // 一般是Text、Icon这些,也可以是其他Widget
  41. @required Widget child,
  42. })
  • FlatButton(扁平按钮)效果图

image.png

IconButton

  • 一个Material图标按钮,点击时会有水波动画
  1. IconButton({
  2. // 键
  3. Key key,
  4. // 图标大小
  5. this.iconSize = 24.0,
  6. // 按钮内边距
  7. this.padding = const EdgeInsets.all(8.0),
  8. // 图标位于按钮部件的位置
  9. this.alignment = Alignment.center,
  10. // 图标,Icon或ImageIcon对象
  11. @required this.icon,
  12. // 图标的颜色
  13. this.color,
  14. // 具有输入焦点的按钮图标的颜色(没搞清楚作用,感觉是Web端的属性)
  15. this.focusColor,
  16. // 指针悬停在按钮图标上是的颜色(没搞清楚作用,感觉是Web端的属性)
  17. this.hoverColor,
  18. // 当按钮处于按下状态时,高亮突出的颜色,出现后会迅速消失
  19. this.highlightColor,
  20. // 飞溅的水波纹颜色
  21. this.splashColor,
  22. // 图标按钮禁用时,图标的颜色
  23. this.disabledColor,
  24. // 按钮点击监听事件
  25. @required this.onPressed,
  26. // 焦点节点(不是很清楚怎么用)
  27. this.focusNode,
  28. // 是否自动聚焦
  29. this.autofocus = false,
  30. // 长按后提示工具
  31. this.tooltip,
  32. })
  • IconButton不同状态的颜色比较及使用实例
    1. IconButton(
    2. icon: const Icon(Icons.add_alert),
    3. onPressed: () {
    4. // 点击事件处理
    5. },
    6. )

image.png

PopupMenuButton

  • 当菜单隐藏式,点击或调用onSelected时显示一个弹出式菜单列表
  1. PopupMenuButton({
  2. // 键
  3. Key key,
  4. // PopupMenuItemBuilder<T>,一个返回PopMenuItem的集合(弹出菜单的子Widget按钮)
  5. @required this.itemBuilder,
  6. // 菜单项的值(如果有),在菜单打开时突出显示。
  7. this.initialValue,
  8. // 点击Item的点击事件监听
  9. this.onSelected,
  10. // 取消关闭弹窗监听
  11. this.onCanceled,
  12. // 长按弹出的提示文字
  13. this.tooltip,
  14. // 和阴影有关
  15. this.elevation,
  16. this.padding = const EdgeInsets.all(8.0),
  17. // 自定义按钮样式,不能与icon属性同时使用,二者选一,如果按钮样式自定义选择这种方式
  18. this.child,
  19. // 按钮的图标
  20. this.icon,
  21. // 弹出窗位置偏移量,如果不设置默认弹出位于右上角顶边
  22. this.offset = Offset.zero,
  23. // 按钮是否能点击
  24. this.enabled = true,
  25. // 弹出框的形状
  26. this.shape,
  27. // 弹出框的背景颜色
  28. this.color,
  29. })
  • PopupMenuButton使用实例
  1. class ScaffoldDemoPage extends StatefulWidget {
  2. ScaffoldDemoPage({Key key}) : super(key: key);
  3. @override
  4. State<StatefulWidget> createState() {
  5. return _ScaffoldDemoState();
  6. }
  7. }
  8. class _ScaffoldDemoState extends State<ScaffoldDemoPage>with SingleTickerProviderStateMixin {
  9. final _scaffoldKey = GlobalKey<ScaffoldState>();
  10. // 选中的菜单值
  11. Choice _selectedChoice = choices[0];
  12. // 菜单项点击事件
  13. void _select(Choice choice) {
  14. setState(() { // Causes the app to rebuild with the new _selectedChoice.
  15. _selectedChoice = choice;
  16. });
  17. _scaffoldKey.currentState
  18. .showSnackBar(SnackBar(content: Text('点击了${choice.title}')));
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. key: _scaffoldKey,
  24. appBar: AppBar(
  25. title: Text('标题'),
  26. actions: <Widget>[
  27. IconButton(
  28. icon: const Icon(Icons.add_alert),
  29. tooltip: 'Show Snackbar',
  30. onPressed: () {
  31. _scaffoldKey.currentState
  32. .showSnackBar(SnackBar(content: Text('点击了')));
  33. },
  34. ),
  35. IconButton(
  36. icon: const Icon(Icons.navigate_next),
  37. tooltip: 'Next page',
  38. onPressed: () {
  39. openPage(context);
  40. },
  41. ),
  42. // 这里是PopuMenuButton的使用,Choice为菜单项实体类
  43. PopupMenuButton<Choice>(
  44. // PopuMenuButton按钮样式
  45. icon: const Icon(Icons.more_vert)
  46. // PopuMenuButton长按提示文字
  47. tooltip: '设置',
  48. // 菜单项点击事件监听
  49. onSelected: _select,
  50. // 菜单项部件构建函数
  51. itemBuilder: (BuildContext context) {
  52. return choices.skip(2).map((Choice choice) {
  53. return PopupMenuItem<Choice>(
  54. value: choice,
  55. child: Text(choice.title),
  56. );
  57. }).toList();
  58. },
  59. // 菜单窗口位置偏离量
  60. offset: Offset(0, 40),
  61. // 菜单窗口形状设置
  62. shape: RoundedRectangleBorder(borderRadius: BorderRadiusDirectional.all(Radius.circular(20.0))),
  63. ),
  64. ],
  65. ),
  66. body: Center(
  67. child: Text('AppBar测试'),
  68. ),
  69. );
  70. }
  71. void openPage(BuildContext context) {
  72. Navigator.push(context, MaterialPageRoute(
  73. builder: (BuildContext context) {
  74. return Scaffold(
  75. appBar: AppBar(
  76. title: const Text('Next page'),
  77. ),
  78. body: const Center(
  79. child: Text(
  80. 'This is the next page',
  81. style: TextStyle(fontSize: 24),
  82. ),
  83. ),
  84. );
  85. },
  86. ));
  87. }
  88. }
  89. // 为菜单项实体类
  90. class Choice {
  91. const Choice({this.title, this.icon});
  92. final String title;
  93. final IconData icon;
  94. }
  95. // 菜单项实体类对象集合
  96. const List<Choice> choices = const <Choice>[
  97. const Choice(title: 'Car', icon: Icons.directions_car),
  98. const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  99. const Choice(title: 'Boat', icon: Icons.directions_boat),
  100. const Choice(title: 'Bus', icon: Icons.directions_bus),
  101. const Choice(title: 'Train', icon: Icons.directions_railway),
  102. const Choice(title: 'Walk', icon: Icons.directions_walk),
  103. ];

Popmenubutton.gif

ButtonBar

  • 水平排列的按钮组,通常可以用在Dialog底部确定和取消按钮组合的地方

    1. ButtonBar({
    2. // 键
    3. Key key,
    4. // 对齐方式,默认右对齐
    5. this.alignment = MainAxisAlignment.end,
    6. // 枚举类型MainAxisSize
    7. this.mainAxisSize = MainAxisSize.max,
    8. // 按钮集合
    9. this.children = const <Widget>[],
    10. })
  • ButtonBar.mainAxisSize中MainAxisSize,max(宽度占满父容器)与MainAxisSize.min(宽度自适应)差异

image.pngimage.png