一,概述

由于Flutter是跨平台的,所以有适用于Android和iOS的两种风格的组件。一套是Google极力推崇的Material,一套是iOS的Cupertino风格的组件。无论哪种风格,都是通用的。

二,Material与Cupertino风格比较

控件

Material

Cupertino

Button

RaisedButton

CupertinoButton

DatePick

showDatePicker

CupertinoDatePicker

从多年与设计师打交道来看,App更青睐于iOS,很多Android的App做的跟iOS一样一样的,就连设计个按钮图标啥的都是一样。

三,Material Style

  • RaisedButton(Material风格的按钮)

    • 属性  

      RaisedButton({
      Key key,
      //点击按钮的回调出发事件
      @required VoidCallback onPressed,
      //水波纹高亮变化回调
      ValueChanged<bool> onHighlightChanged,
      //按钮的样式(文字颜色、按钮的最小大小,内边距以及shape)[ Used with [ButtonTheme] and [ButtonThemeData] to define a button's base
      //colors, and the defaults for the button's minimum size, internal padding,and shape.]
      ButtonTextTheme textTheme,
      //文字颜色
      Color textColor,
      //按钮被禁用时的文字颜色
      Color disabledTextColor,
      //按钮的颜色
      Color color,
      //按钮被禁用时的颜色
      Color disabledColor,
      //按钮的水波纹亮起的颜色
      Color highlightColor,
      //水波纹的颜色
      Color splashColor,
      //按钮主题高亮
      Brightness colorBrightness,
      //按钮下面的阴影长度
      double elevation,
      //按钮高亮时的下面的阴影长度
      double highlightElevation,
      double disabledElevation,
      EdgeInsetsGeometry padding,
      ShapeBorder shape,
      Clip clipBehavior = Clip.none,
      MaterialTapTargetSize materialTapTargetSize,
      Duration animationDuration,
      Widget child,
      }
    • 一张图了解RaisedButton

图示RaisedButton
    • 示例代码

      RaisedButton(
      textTheme: ButtonTextTheme.accent,
      color: Colors.teal,
      highlightColor: Colors.deepPurpleAccent,
      splashColor: Colors.deepOrangeAccent,
      colorBrightness: Brightness.dark,
      elevation: 50.0,
      highlightElevation: 100.0,
      disabledElevation: 20.0,
      onPressed: () {
      //TODO
      },
      child: Text(
      'RaisedButton',
      style: TextStyle(color: Colors.white, fontSize: ),
      ),
      )
  • FloatingActionButton(悬浮按钮)

    • 属性

      FloatingActionButton({
      Key key,
      // 按钮上的组件,可以是Text、icon, etc.
      this.child,
      //长按提示
      this.tooltip,
      // child的颜色(尽在child没有设置颜色时生效)
      this.foregroundColor,
      //背景色,也就是悬浮按键的颜色
      this.backgroundColor,
      this.heroTag = const _DefaultHeroTag(),
      //阴影长度
      this.elevation = 6.0,
      //高亮时阴影长度
      this.highlightElevation = 12.0,
      //按下事件回调
      @required this.onPressed,
      //是小图标还是大图标
      this.mini = false,
      //按钮的形状(例如:矩形Border,圆形图标CircleBorder)
      this.shape = const CircleBorder(),
      this.clipBehavior = Clip.none,
      this.materialTapTargetSize,
      this.isExtended = false,
      })
    • 示例代码

      FloatingActionButton(
      child: Icon(Icons.access_alarm),
      tooltip: "ToolTip",
      foregroundColor: Colors.white,
      backgroundColor: Colors.deepPurple,
      shape: const Border(),
      onPressed: () {
      //click callback
      },
      )
    • 效果
           
  • FlatButton

一个扁平的Material按钮,属性跟RaisedButton类似。

    • 属性

      FlatButton({
      Key key,
      @required VoidCallback onPressed,
      ValueChanged<bool> onHighlightChanged,
      ButtonTextTheme textTheme,
      Color textColor,
      Color disabledTextColor,
      Color color,
      Color disabledColor,
      Color highlightColor,
      Color splashColor,
      Brightness colorBrightness,
      EdgeInsetsGeometry padding,
      ShapeBorder shape,
      Clip clipBehavior = Clip.none,
      MaterialTapTargetSize materialTapTargetSize,
      @required Widget child,
      })
    • 示例代码

      FlatButton(
      onPressed: () {},
      child: Text(
      "FlatBtn",
      style: TextStyle(fontSize: , color: Colors.deepPurple),
      ));
    • 效果

  • IconButton

图标按钮,按下时会产生水波纹效果。

    • 属性
      这几个属性跟前面讲的几个差不多,这里就不再讲了。
    • 示例代码
      IconButton({
      Key key,
      this.iconSize = 24.0,
      this.padding = const EdgeInsets.all(8.0),
      this.alignment = Alignment.center,
      @required this.icon,
      this.color,
      this.highlightColor,
      this.splashColor,
      this.disabledColor,
      @required this.onPressed,
      this.tooltip
      })
    • 效果

  • DropdownButton

Material Style 下拉菜单按钮

    • DropdownButton使用

      DropdownButton({
      Key key,
      //要显示的列表
      List<DropdownMenuItem<T>> @required this.items,
      //下拉菜单选中的值(注意:在初始化时,要么为null,这时显示默认hint的值;
      // 如果自己设定值,则值必须为列表中的一个值,如果不在列表中,会抛出异常)
      T value,
      //默认显示的值
      Widget hint,
      Widget disabledHint,
      //选中时的回调
      ValueChanged<T> @required this.onChanged,
      this.elevation = ,
      this.style,
      this.iconSize = 24.0,
      this.isDense = false,
      this.isExpanded = false,
      })
    • items使用方法

      • 写法一

        //返回城市列表,
        List<DropdownMenuItem> _getItems() {
        List<DropdownMenuItem> items = new List();
        //value 表示DropdownButton.onChanged的返回值
        items.add(DropdownMenuItem(child: Text("北京"), value: "BJ"));
        items.add(DropdownMenuItem(child: Text("上海"), value: "SH"));
        items.add(DropdownMenuItem(child: Text("广州"), value: "GZ"));
        items.add(DropdownMenuItem(child: Text("深圳"), value: "SZ"));
        return items;
        }
      • 写法二

        //返回城市列表,
        List<DropdownMenuItem<String>> _getCityList() {
        var list = ["北京", "上海", "广州", "深圳"];
        return list.map((item) => DropdownMenuItem(
        value: item,
        child: Text(item),
        )).toList();
        }
    • 使用方法

      由于我们在点击每一个条目后,展示的选中条目会变化,所以DropdownButton应当继承StatefulWidget,在选中条目后也就是onChange的回调中使用setState((){})更新对象的状态。

      • DropdownButton

        class FlutterDropdownButtonStatefulWidget extends StatefulWidget {
        @override
        State<StatefulWidget> createState() {
        return _DropdownState();
        }
        }
      • _DropdownState

        //下划线开头表示私有
        class _DropdownState extends State<FlutterDropdownButtonStatefulWidget> {
        // 下拉菜单选中的值(注意:在初始化时,要么为null,这时显示默认hint的值;
        // 如果自己设定值,则值必须为列表中的一个值,如果不在列表中,会抛出异常)
        String selectValue; @override
        Widget build(BuildContext context) {
        return DropdownButton(
        //要显示的条目
        items: _getItems(),
        //默认显示的值
        hint: Text("请选择城市"),
        //下拉菜单选中的值(注意:在初始化时,要么为null,这时显示默认hint的值;
        // 如果自己设定值,则值必须为列表中的一个值,如果不在列表中,会抛出异常)
        value: selectValue,
        onChanged: (itemValue) {//itemValue为选中的值
        print("itemValue=$itemValue");
        _onChanged(itemValue);
        },
        );
        }
        _onChanged(String value) {
        //更新对象的状态
        setState(() {
        selectValue = value;
        });
        }
        }
      • print log

      • 最终效果

  • PopupMenuButton

    当菜单隐藏时,点击并且调用onSelected时显示一个弹出式菜单列表

    • 属性

      PopupMenuButton({
      Key key,
      //构建弹出式列表数据
      PopupMenuItemBuilder<T> @required this.itemBuilder,
      //初始值
      T initialValue,
      //选中时的回调
      PopupMenuItemSelected<T> onSelected;,
      //当未选中任何条目后弹窗消失时的回调
      final PopupMenuCanceled onCanceled;,
      //
      this.tooltip,
      //弹窗阴影高度
      this.elevation = 8.0,
      //边距
      this.padding = const EdgeInsets.all(8.0),
      //弹窗的位置显示的组件,默认为三个点...
      this.child,
      //跟child效果一致
      this.icon,
      //弹窗偏移位置
      this.offset = Offset.zero,
      })
    • 示例demo
      import 'package:flutter/material.dart';
      
      class FlutterPopupMenuButton extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => _PopupMenuState();
      } const List<String> models = const <String>['白天模式', '护眼模式', '黑夜模式']; class _PopupMenuState extends State<FlutterPopupMenuButton> {
      String title = models[]; List<PopupMenuEntry<String>> _getItemBuilder() {
      return models.map((item) => PopupMenuItem<String>(
      child: Text(item),
      value: item,//value一定不能少
      )).toList();
      } void _select(String value) {
      setState(() {
      title = value;
      });
      } @override
      Widget build(BuildContext context) {
      return MaterialApp(
      home: Scaffold(
      appBar: AppBar(
      title: Text(title),
      actions: <Widget>[
      PopupMenuButton<String>(
      onSelected: _select,
      itemBuilder: (BuildContext context) {
      return _getItemBuilder();
      },
      )
      ],
      ),
      ),
      );
      } // List<PopupMenuEntry> _getItemBuilder() {
      // List<PopupMenuEntry> list = List();
      // list.add(PopupMenuItem(
      // child: Text("白天模式"),
      // value: "Day",
      // )
        );
      // list.add(PopupMenuItem(
      // child: Text("黑夜模式"),
      // value: "Night",
      // )
      );
      // return list;
      // }
      }
    • 示例效果
  • ButtonBar

    水平排列的按钮组

    • 属性  

      const ButtonBar({
      Key key,
      //子组件的间隔样式
      this.alignment = MainAxisAlignment.end,
      this.mainAxisSize = MainAxisSize.max,
      //子children
      this.children = const <Widget>[],
      })
    • 示例代码

      class FlutterButtonBar extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
      return ButtonBar(children: <Widget>[
      Text("ButtonBar0"),
      Icon(Icons.ac_unit),
      Text("ButtonBar1")
      ],
      );
      }
      }
    • 效果

【Flutter学习】之button按钮的更多相关文章

  1. Android学习之Button按钮在程序运行时全部变大写的处理

    问题: 在layout布局文件中,我们命名的按钮名称是“button1”,程序运行过后,在app上显示出来的是“BUTTON1”,先看源代码和效果: 按钮源代码: 运行效果: 解决办法: 方法一: 在 ...

  2. Android学习起步 - Button按钮及事件处理

    按钮和文本框算是比较简单的控件了,以下主要讲按钮的事件响应,三种写法(匿名内部类响应事件.外部类响应事件.本类直接响应事件) 点击按钮后文本框中会显示 ”按钮被单击了”,先看效果: 以下是这个界面的布 ...

  3. Android学习笔记-Button(按钮)

    Button是TextView的子类,所以TextView上很多属性也可以应用到Button 上!我们实际开发中对于Button的,无非是对按钮的几个状态做相应的操作,比如:按钮按下的时候 用一种颜色 ...

  4. Android 自定义Button按钮显示样式(正常、按下、获取焦点)

    现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的.今天学习自定义Button按钮样式.Button样式修改的是Button的背景 ...

  5. Flutter学习笔记(11)--文本组件、图标及按钮组件

    如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 文本组件 文本组件(text)负责显示文本和定义显示样式,下表为text常见属性 Text组件属性及描述 属性名 类型 默认 ...

  6. Flutter学习笔记(26)--返回拦截WillPopScope,实现1秒内点击两次返回按钮退出程序

    如需转载,请注明出处:Flutter学习笔记(26)--返回拦截WillPopScope,实现1秒内点击两次返回按钮退出程序 在实际开发中,为了防止用户误触返回按钮导致程序退出,通常会设置为在1秒内连 ...

  7. Flutter学习笔记(5)--Dart运算符

    如需转载,请注明出处:Flutter学习笔记(5)--Dart运算符 先给出一个Dart运算符表,接下来在逐个解释和使用.如下:                            描述       ...

  8. Flutter学习笔记(13)--表单组件

    如需转载,请注明出处:Flutter学习笔记(13)--表单组件 表单组件是个包含表单元素的区域,表单元素允许用户输入内容,比如:文本区域,下拉表单,单选框.复选框等,常见的应用场景有:登陆.注册.输 ...

  9. Flutter学习笔记(14)--StatefulWidget简单使用

    如需转载,请注明出处:Flutter学习笔记(14)--StatefulWidget简单使用 今天上班没那么忙,突然想起来我好像没StatefulWidget(有状态组件)的demo,闲来无事,写一个 ...

  10. Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 今天的内容是Scaffold脚手架.AppBar组件.BottomNavigationBa ...

随机推荐

  1. RESTful_基础知识

    目录 目录 前言 RESTful REST原则 REST的Web原则 分层系统原则 RESTful的实现 SOA 面向服务的体系结构 RPC样式 Web服务 RPC的实现过程 SOAP 简单对象访问协 ...

  2. ADFS 2016 & Dynamics CRM

    参考:https://blog.csdn.net/vic0228/article/details/80188291 webapp 获取token https://adfs.demo.local/adf ...

  3. 04 | 基础篇:经常说的 CPU 上下文切换是什么意思?(下)

    上一节,我给你讲了 CPU 上下文切换的工作原理.简单回顾一下,CPU 上下文切换是保证 Linux 系统正常工作的一个核心功能,按照不同场景,可以分为进程上下文切换.线程上下文切换和中断上下文切换. ...

  4. Game on a Tree Gym - 102392F(树上最大匹配)

    思路: 本质是求一个树上的最大匹配能否覆盖所有的点. dfs遍历,用qian[]数组记录当前节点的子树内有几个没有匹配的点(初始化为-1因为可以匹配掉一个子树中未匹配的点),pipei[]数组记录当前 ...

  5. JSP基础--九大内置对象

    JSP九大内置对象 Object findAttribute(String name):依次在page.request.session.application范围查找名称为name的数据,如果找到就停 ...

  6. CentOS下编译Lua使得其支持动态链接

    在Linux下编译Lua时,我一般都是使用的make generic,这样编译没有什么问题,运行lua的程序也都OK,但是,这样在加载外部的C动态 链接库,却总是报下面的错误 dynamic libr ...

  7. 批量调整word 图片大小

    打开文档后,按Alt+F11,在左边Porject下找到ThisDocument,右键插入模块,贴上下面的 Sub Macro()For Each iShape In ActiveDocument.I ...

  8. CVE-2014-4014 Linux Kernel Local Privilege Escalation PoC

    /**  * CVE-2014-4014 Linux Kernel Local Privilege Escalation PoC  *  * Vitaly Nikolenko  * http://ha ...

  9. 【问题解决方案】Linux中进入目录下文件夹

    win系统中直接 cd+空格+文件夹名 Linux下 cd+空格+./+文件名 其中句点表示"当前目录" 除非在根目录不加,或者把路径写全用绝对路径进入 Linux下切换路径的相关 ...

  10. 道路模型--linear-parabolic model

    读过很多道路追踪的论文,经常都需要道路模型的建模.我不知道是不是因为自己太笨还是怎样,好多人建的模型我实在无法理解他的用意何在,而且我真的深刻怀疑他们那些模型的参数是不是真的可以求出来.就比如这篇文章 ...