Flutter--Button按鈕學(xué)習(xí)

一托猩、介紹

在Flutter中有提供很多Button組件

RaisedButton:凸起的按鈕,繼承MaterialButton
FlatButton:扁平化的按鈕,繼承MaterialButton
OutlineButton:帶邊框的按鈕冯丙,繼承MaterialButton

FloatingActionButton:浮動(dòng)圓形按鈕酥筝,繼承StatelessWidget
IconButton:圖標(biāo)按鈕,,承StatelessWidget
DropdownButton:彈出下拉框圃酵,繼承StatefulWidget
PopupMenuButton:菜單選擇框按鈕柳畔,繼承StatefulWidget

TextButton:文本按鈕,繼承ButtonStyleButton
ElevatedButtont凸起的按鈕:繼承ButtonStyleButton
OutlinedButton帶邊框的按鈕,繼承ButtonStyleButton



Flutter 1.22版本新增了3個(gè)按鈕郭赐,TextButton薪韩、OutlinedButton、ElevatedButton,雖然以前的Button沒有被廢棄俘陷,但還是建議使用新的Button罗捎。以前的按鈕調(diào)整為統(tǒng)一的外觀比較麻煩,因此以前經(jīng)常使用自定義的按鈕岭洲,而新增的按鈕解決了此類問題宛逗,可以非常方便的設(shè)置整體外觀

1.22版本前的按鈕 主題 1.22版本后的按鈕 主題
FlatButton ButtonTheme TextButton TextButtonTheme
OutlineButton ButtonTheme OutlinedButton OutlinedButtonTheme
RaisedButton ButtonTheme ElevatedButton ElevatedButtonTheme

二、RaisedButton

2.1盾剩、RaisedButton源碼
const RaisedButton({
    Key key,
    @required VoidCallback onPressed,//點(diǎn)擊事件監(jiān)聽
    VoidCallback onLongPress,//長(zhǎng)按監(jiān)聽
    ValueChanged<bool> onHighlightChanged,//高亮變化監(jiān)聽
    MouseCursor mouseCursor,
    ButtonTextTheme textTheme,//文本主題
    Color textColor,//文本顏色
    Color disabledTextColor,//按鈕不可點(diǎn)擊時(shí)文本顏色
    Color color,//按鈕顏色
    Color disabledColor,//按鈕不可點(diǎn)擊時(shí)按鈕顏色
    Color focusColor,
    Color hoverColor,
    Color highlightColor,//高亮顏色
    Color splashColor,//水波紋顏色
    Brightness colorBrightness,//按鈕文字樣式(不設(shè)置文字顏色有效)
    double elevation,//陰影高度
    double focusElevation,
    double hoverElevation,
    double highlightElevation,//高亮?xí)r陰影高度
    double disabledElevation,//按鈕不可點(diǎn)擊時(shí)陰影高度
    EdgeInsetsGeometry padding,//內(nèi)邊距
    VisualDensity visualDensity,//Material Design中各類組件的視覺密度
    ShapeBorder shape,//設(shè)置按鈕邊框或形狀
    Clip clipBehavior = Clip.none,//剪輯
    FocusNode focusNode,
    bool autofocus = false,
    MaterialTapTargetSize materialTapTargetSize,
    Duration animationDuration,//水波紋持續(xù)時(shí)間
    Widget child,//子組件
  }) : assert(autofocus != null),
       assert(elevation == null || elevation >= 0.0),
       assert(focusElevation == null || focusElevation >= 0.0),
       assert(hoverElevation == null || hoverElevation >= 0.0),
       assert(highlightElevation == null || highlightElevation >= 0.0),
       assert(disabledElevation == null || disabledElevation >= 0.0),
       assert(clipBehavior != null),
       super(
         key: key,
         onPressed: onPressed,
         onLongPress: onLongPress,
         onHighlightChanged: onHighlightChanged,
         mouseCursor: mouseCursor,
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
         focusColor: focusColor,
         hoverColor: hoverColor,
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         elevation: elevation,
         focusElevation: focusElevation,
         hoverElevation: hoverElevation,
         highlightElevation: highlightElevation,
         disabledElevation: disabledElevation,
         padding: padding,
         visualDensity: visualDensity,
         shape: shape,
         clipBehavior: clipBehavior,
         focusNode: focusNode,
         autofocus: autofocus,
         materialTapTargetSize: materialTapTargetSize,
         animationDuration: animationDuration,
         child: child,
       );
2.2雷激、RaisedButton屬性介紹
屬性 說明
onPressed 點(diǎn)擊監(jiān)聽
onLongPress 長(zhǎng)按點(diǎn)擊監(jiān)聽
onHighlightChanged 高亮變化監(jiān)聽
textTheme 文字主題,取決于MaterialApp的theme屬性
ButtonTextTheme.normal: 文本顏色取決于ThemeData.brightness
ButtonTextTheme.accent: 文本顏色取決于ThemeData.accentColor
ButtonTextTheme.primary:文本顏色基于ThemeData.primaryColor
textColor 文本顏色
disabledTextColor 按鈕不可點(diǎn)擊時(shí)文本顏色
color 按鈕顏色
disabledColor 按鈕不可點(diǎn)擊時(shí)按鈕顏色
highlightColor 高亮顏色,即按鈕點(diǎn)擊時(shí)按鈕的顏色
splashColor 水波紋顏色
colorBrightness 按鈕文字樣式(不設(shè)置color時(shí)有效)
Brightness.light黑色
Brightness.dark白色
elevation 陰影高度
highlightElevation 高亮?xí)r陰影高度告私,即點(diǎn)擊按鈕時(shí)陰影高度
disabledElevation 按鈕不可點(diǎn)擊時(shí)屎暇,陰影的高度
padding 內(nèi)間距
visualDensity Material Design中各類組件的視覺密度
shape 設(shè)置按鈕邊框或形狀(具體使用見demo)
Border只設(shè)置邊框
BeveledRectangleBorder尖端斜角
RaisedButton圓形按鈕
RoundedRectangleBorder圓角按鈕
StadiumBorder半圓角按鈕
clipBehavior 剪切內(nèi)容
Clip.hardEdge剪輯,但不應(yīng)用抗鋸齒
Clip.antiAlias剪輯具有抗鋸齒功能
Clip.antiAliasWithSaveLayer在剪輯后立即剪輯具有抗鋸齒和saveLayer
Clip.none不剪輯

child 子組件
2.3驻粟、RaisedButton的demo
return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("Button學(xué)習(xí)"),
          ),
          body: Container(
            padding: EdgeInsets.only(left: 10, top: 0, right: 0, bottom: 0),
            child: Wrap(
              spacing: 15,
              runSpacing: 5,
              children: [
                RaisedButton(
                  onPressed: () {
                    print("普通按鈕");
                  },
                  colorBrightness: Brightness.dark,
                  child: Text("普通按鈕"),
                ),
                RaisedButton(
                  onPressed: () {
                    print("帶顏色按鈕");
                  },
                  child: Text("帶顏色按鈕"),
                  textColor: Colors.yellow,
                  color: Colors.red,
                ),
                RaisedButton(
                  onPressed: null,
                  child: Text("禁用按鈕"),
                  disabledTextColor: Colors.white,
                  disabledColor: Colors.blue,
                ),
                RaisedButton(
                  onPressed: () {},
                  child: Text("陰影效果"),
                  elevation: 20,
                  highlightElevation: 10,
                ),
                RaisedButton(
                  onPressed: () {},
                  child: Text("點(diǎn)擊變色"),
                  highlightColor: Colors.red,
                ),
                RaisedButton(
                  onPressed: () {},
                  splashColor: Colors.red,
                  child: Text("水波紋效果"),
                ),
                RaisedButton(
                  onPressed: () {},
                  shape: Border.all(
                      color: Colors.red, width: 1, style: BorderStyle.solid),
                  child: Text("設(shè)置邊框"),
                ),
                RaisedButton(
                  onPressed: () {},
                  shape: BeveledRectangleBorder(
                      side: BorderSide(
                          color: Colors.red,
                          width: 1,
                          style: BorderStyle.solid),
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: Text("尖端斜角"),
                ),
                Container(
                  height: 80,
                  width: 80,
                  child: RaisedButton(
                    onPressed: () {},
                    child: Text("圓形按鈕"),
                    shape: CircleBorder(
                        side: BorderSide(
                            color: Colors.red,
                            width: 1,
                            style: BorderStyle.solid)),
                  ),
                ),
                RaisedButton(
                  onPressed: () {},
                  child: Text("圓角按鈕"),
                  shape: RoundedRectangleBorder(
                      side: BorderSide(
                          color: Colors.red,
                          width: 1,
                          style: BorderStyle.solid),
                      borderRadius: BorderRadius.circular(10)),
                ),
                RaisedButton(
                  onPressed: () {},
                  child: Text("半圓角按鈕"),
                  shape: StadiumBorder(
                    side: BorderSide(
                        color: Colors.red, width: 1, style: BorderStyle.solid),
                  ),
                ),
              ],
            ),
          )),
    );
1111.png

2020.12.22.00.25.29.gif

2020.12.22.00.26.42.gif

三根悼、FlatButton

3.1FlatButton的源碼
const FlatButton({
    Key key,
    @required VoidCallback onPressed,
    VoidCallback onLongPress,
    ValueChanged<bool> onHighlightChanged,
    MouseCursor mouseCursor,
    ButtonTextTheme textTheme,
    Color textColor,
    Color disabledTextColor,
    Color color,
    Color disabledColor,
    Color focusColor,
    Color hoverColor,
    Color highlightColor,
    Color splashColor,
    Brightness colorBrightness,
    EdgeInsetsGeometry padding,
    VisualDensity visualDensity,
    ShapeBorder shape,
    Clip clipBehavior = Clip.none,
    FocusNode focusNode,
    bool autofocus = false,
    MaterialTapTargetSize materialTapTargetSize,
    @required Widget child,
    double height,
    double minWidth,
  }) : assert(clipBehavior != null),
       assert(autofocus != null),
       super(
         key: key,
         height: height,
         minWidth: minWidth,
         onPressed: onPressed,
         onLongPress: onLongPress,
         onHighlightChanged: onHighlightChanged,
         mouseCursor: mouseCursor,
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
         focusColor: focusColor,
         hoverColor: hoverColor,
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         padding: padding,
         visualDensity: visualDensity,
         shape: shape,
         clipBehavior: clipBehavior,
         focusNode: focusNode,
         autofocus: autofocus,
         materialTapTargetSize: materialTapTargetSize,
         child: child,
      );
3.2、FlatButton的屬性介紹蜀撑,參考RaisedButton屬性介紹

四挤巡、OutlineButton

4.1、OutlineButton源碼
const OutlineButton({
    Key key,
    @required VoidCallback onPressed,
    VoidCallback onLongPress,
    MouseCursor mouseCursor,
    ButtonTextTheme textTheme,
    Color textColor,
    Color disabledTextColor,
    Color color,
    Color focusColor,
    Color hoverColor,
    Color highlightColor,
    Color splashColor,
    double highlightElevation,
    this.borderSide,
    this.disabledBorderColor,
    this.highlightedBorderColor,
    EdgeInsetsGeometry padding,
    VisualDensity visualDensity,
    ShapeBorder shape,
    Clip clipBehavior = Clip.none,
    FocusNode focusNode,
    bool autofocus = false,
    MaterialTapTargetSize materialTapTargetSize,
    Widget child,
  }) : assert(highlightElevation == null || highlightElevation >= 0.0),
       assert(clipBehavior != null),
       assert(autofocus != null),
       super(
         key: key,
         onPressed: onPressed,
         onLongPress: onLongPress,
         mouseCursor: mouseCursor,
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         focusColor: focusColor,
         hoverColor: hoverColor,
         highlightColor: highlightColor,
         splashColor: splashColor,
         highlightElevation: highlightElevation,
         padding: padding,
         visualDensity: visualDensity,
         shape: shape,
         clipBehavior: clipBehavior,
         focusNode: focusNode,
         materialTapTargetSize: materialTapTargetSize,
         autofocus: autofocus,
         child: child,
       );
4.2酷麦、OutlineButton的屬性介紹矿卑,參考RaisedButton屬性介紹

五、FloatingActionButton

5.1沃饶、FloatingActionButton源碼
const FloatingActionButton({
    Key key,
    this.child,
    this.tooltip,
    this.foregroundColor,
    this.backgroundColor,
    this.focusColor,
    this.hoverColor,
    this.splashColor,
    this.heroTag = const _DefaultHeroTag(),
    this.elevation,
    this.focusElevation,
    this.hoverElevation,
    this.highlightElevation,
    this.disabledElevation,
    @required this.onPressed,
    this.mouseCursor,
    this.mini = false,
    this.shape,
    this.clipBehavior = Clip.none,
    this.focusNode,
    this.autofocus = false,
    this.materialTapTargetSize,
    this.isExtended = false,
  }) : assert(elevation == null || elevation >= 0.0),
       assert(focusElevation == null || focusElevation >= 0.0),
       assert(hoverElevation == null || hoverElevation >= 0.0),
       assert(highlightElevation == null || highlightElevation >= 0.0),
       assert(disabledElevation == null || disabledElevation >= 0.0),
       assert(mini != null),
       assert(clipBehavior != null),
       assert(isExtended != null),
       assert(autofocus != null),
       _sizeConstraints = mini ? _kMiniSizeConstraints : _kSizeConstraints,
       super(key: key);
5.2母廷、FloatingActionButton屬性介紹
屬性 說明
foregroundColor 前置色,一般是文字或者圖標(biāo)的顏色
backgroundColor 背景色
mini 是否mini模式
5.3糊肤、FloatingActionButton的demo

FloatingActionButton可以單獨(dú)使用
也可以作為Scaffold的floatingActionButton屬性使用琴昆,Scaffold的floatingActionButtonLocation屬性,可以配置FloatingActionButton的位置

return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("Button學(xué)習(xí)2"),
          ),
          //配合Scaffold使用
          //centerDocked 底部中間
          //endDocked 底部右側(cè)
          //centerFloat 中間偏上
          //endFloat 底部偏上
          //startTop 左側(cè)頂部
          //endTop 右側(cè)頂部
          //......
          floatingActionButtonLocation:FloatingActionButtonLocation.endTop ,
          floatingActionButton:FloatingActionButton(
            onPressed: () {},
            child: Icon(Icons.home),
            backgroundColor: Colors.red,
            foregroundColor: Colors.white,
            mini: true,
          ) ,
          body: Container(
            padding: EdgeInsets.only(left: 10, top: 0, right: 0, bottom: 0),
            child: Wrap(
              spacing: 15,
              runSpacing: 5,
              children: [
                //單獨(dú)使用
                FloatingActionButton(
                  onPressed: () {},
                  child: Icon(Icons.people),
                  backgroundColor: Colors.red,
                  foregroundColor: Colors.yellow,
                )
              ],
            ),
          )),
    );
22222.png

六馆揉、IconButton

6.1业舍、IconButton源碼
const IconButton({
    Key key,
    this.iconSize = 24.0,
    this.visualDensity,
    this.padding = const EdgeInsets.all(8.0),
    this.alignment = Alignment.center,
    this.splashRadius,
    @required this.icon,
    this.color,
    this.focusColor,
    this.hoverColor,
    this.highlightColor,
    this.splashColor,
    this.disabledColor,
    @required this.onPressed,
    this.mouseCursor = SystemMouseCursors.click,
    this.focusNode,
    this.autofocus = false,
    this.tooltip,
    this.enableFeedback = true,
    this.constraints,
  }) : assert(iconSize != null),
       assert(padding != null),
       assert(alignment != null),
       assert(splashRadius == null || splashRadius > 0),
       assert(autofocus != null),
       assert(icon != null),
       super(key: key);
6.2、IconButton屬性介紹
屬性 說明
iconSize 圖標(biāo)大小
splashRadius 點(diǎn)擊圖標(biāo)的濺射范圍
splashColor 濺射的顏色
icon 圖標(biāo)
tooltip 提示信息升酣,按鈕長(zhǎng)按可顯示,顯示1500ms
alignment 對(duì)齊方式

其余屬性參考RaisedButton屬性勤讽。

6.3、IconButton的demo
return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("Button學(xué)習(xí)4"),
          ),
          body: Container(
            padding: EdgeInsets.only(left: 10, top: 0, right: 0, bottom: 0),
            alignment: Alignment.center,
            child: IconButton(
              iconSize: 50,
              color: Colors.blue,
              splashRadius: 50,
              splashColor: Colors.red,
              icon: Icon(Icons.home),
              onPressed: () {},
            ),
          )),
    );
1122.gif

七拗踢、DropdownButton

7.1脚牍、DropdownButton源碼
DropdownButton({
    Key key,
    @required this.items,
    this.selectedItemBuilder,
    this.value,
    this.hint,
    this.disabledHint,
    @required this.onChanged,
    this.onTap,
    this.elevation = 8,
    this.style,
    this.underline,
    this.icon,
    this.iconDisabledColor,
    this.iconEnabledColor,
    this.iconSize = 24.0,
    this.isDense = false,
    this.isExpanded = false,
    this.itemHeight = kMinInteractiveDimension,
    this.focusColor,
    this.focusNode,
    this.autofocus = false,
    this.dropdownColor,
    // When adding new arguments, consider adding similar arguments to
    // DropdownButtonFormField.
  }) : assert(items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1,
                "There should be exactly one item with [DropdownButton]'s value: "
                '$value. \n'
                'Either zero or 2 or more [DropdownMenuItem]s were detected '
                'with the same value',
              ),
       assert(elevation != null),
       assert(iconSize != null),
       assert(isDense != null),
       assert(isExpanded != null),
       assert(autofocus != null),
       assert(itemHeight == null || itemHeight >=  kMinInteractiveDimension),
       super(key: key);
7.2、DropdownButton的屬性介紹
屬性 說明
items 下拉菜單項(xiàng)
value 選中的值
hint value為空時(shí)的提示語
disabledHint 下拉按鈕不能用時(shí)的提示語
onChanged 下拉按鈕改變值的事件監(jiān)聽
onTap 點(diǎn)擊按鈕的事件監(jiān)聽
elevation 陰影高度
style 文本樣式
underline 下劃線
icon 按鈕的圖標(biāo)巢墅,默認(rèn)Icons.arrow_drop_down
iconDisabledColor 下拉按鈕不可點(diǎn)擊時(shí)诸狭,圖標(biāo)的顏色
iconEnabledColor 下拉按鈕可點(diǎn)擊時(shí)券膀,圖標(biāo)的顏色
iconSize 圖標(biāo)的大小
isDense 默認(rèn)為false該按鈕的高度與其菜單項(xiàng)的高度相同,如果為true按鈕高度是item高度的一半
isExpanded 默認(rèn)false按鈕的內(nèi)部寬度是其內(nèi)容的最小尺寸
true驯遇,則內(nèi)部寬度將被展開來填充周圍的容器芹彬。
itemHeight
dropdownColor 下拉菜單的背景顏色
7.3、DropdownButton的demo
class ButtonFul extends StatefulWidget {
  ButtonFul({Key key}) : super(key: key);

  _ButtonFulState createState() => _ButtonFulState();
}

class _ButtonFulState extends State<ButtonFul> {
  var buttonItems = ['item1', 'item2', 'item3', 'item4'];
  String _itemValue;

  List<DropdownMenuItem> _getDropdownItem() {
    return buttonItems.map((e) {
      return DropdownMenuItem(
        child: Text(e),
        value: e,
      );
    }).toList();
  }

  @override
  void initState() {
    super.initState();
    _itemValue = buttonItems[0];
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("StatefulWidget模板"),
          ),
          body: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(10),
            child: DropdownButton(
              onChanged: (value) {
                setState(() {
                  print("ysl--onChanged");
                  _itemValue = value;
                });
              },
              onTap: (){
                print("ysl--onTap");
              },
              items: _getDropdownItem(),
              value: _itemValue,
              hint: Text("請(qǐng)選擇"),
              disabledHint: Text("按鈕不可點(diǎn)擊"),
              elevation: 10,
              underline: Container(color: Colors.red,height: 1,),
              iconSize: 30,
              dropdownColor: Colors.blue,
            ),
          )),
    );
  }
}
2020.12.22.15.15.24.gif

八叉庐、PopupMenuButton

8.1舒帮、PopupMenuButton的源碼
const PopupMenuButton({
    Key key,
    @required this.itemBuilder,
    this.initialValue,
    this.onSelected,
    this.onCanceled,
    this.tooltip,
    this.elevation,
    this.padding = const EdgeInsets.all(8.0),
    this.child,
    this.icon,
    this.offset = Offset.zero,
    this.enabled = true,
    this.shape,
    this.color,
    this.captureInheritedThemes = true,
  }) : assert(itemBuilder != null),
       assert(offset != null),
       assert(enabled != null),
       assert(captureInheritedThemes != null),
       assert(!(child != null && icon != null),
           'You can only pass [child] or [icon], not both.'),
       super(key: key);
8.2、PopupMenuButton的屬性介紹
屬性 說明
itemBuilder 彈出菜單項(xiàng)的生成器陡叠,該方法返回List<PopupMenuItem>
initialValue 設(shè)置彈出菜單默認(rèn)選中項(xiàng)的值
onSelected 彈出菜單被選擇事件監(jiān)聽
onCanceled 沒用任何菜單被選擇時(shí)觸發(fā)玩郊,即點(diǎn)擊空白處觸發(fā)
tooltip 提示信息,按鈕長(zhǎng)按可顯示
elevation 陰影高度
padding 內(nèi)間距
child 子組件
icon 圖標(biāo)
offset 設(shè)置下拉菜單偏移量
enabled 是否可使用
shape 參考RaisedButton的shape屬性
color 參考RaisedButton的color屬性
8.3枉阵、PopupMenuButton的demo
class ButtonFul extends StatefulWidget {
  ButtonFul({Key key}) : super(key: key);

  _ButtonFulState createState() => _ButtonFulState();
}

class _ButtonFulState extends State<ButtonFul> {
  final popupMenuItems = ['menu1', 'menu2', 'menu3', 'menu4'];

  List<PopupMenuItem> _getPopupMenu() {
    return popupMenuItems.map((e) {
      return PopupMenuItem(
        child: Text(e),
        value: e,
      );
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("StatefulWidget模板"),
            actions: [],
          ),
          body: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.all(10),
              child: PopupMenuButton(
                itemBuilder: (context) {
                  return _getPopupMenu();
                },
                icon: Icon(Icons.add_circle),
                initialValue: popupMenuItems[0],
                elevation: 20,
                onSelected: (item) {
                  print("ysl--選擇條目");

                },
                tooltip: "tooltip",
                onCanceled: () {
                  print("ysl--點(diǎn)擊空白區(qū)域");
                },
                shape: RoundedRectangleBorder(
                    side: BorderSide(
                        color: Colors.red, width: 1, style: BorderStyle.solid),
                    borderRadius: BorderRadius.circular(10)),
                offset: Offset(10, 20),
                color: Colors.yellow,
              ))),
    );
  }
}
2020.12.22.16.08.55.gif

九译红、TextButton文本按鈕

9.1、TextButton源碼
const TextButton({
    Key key,
    @required VoidCallback onPressed,
    VoidCallback onLongPress,
    ButtonStyle style,
    FocusNode focusNode,
    bool autofocus = false,
    Clip clipBehavior = Clip.none,
    @required Widget child,
  }) : super(
    key: key,
    onPressed: onPressed,
    onLongPress: onLongPress,
    style: style,
    focusNode: focusNode,
    autofocus: autofocus,
    clipBehavior: clipBehavior,
    child: child,
  );
9.2兴溜、TextButton屬性介紹
屬性 說明
onPressed 點(diǎn)擊監(jiān)聽
onLongPress 長(zhǎng)按監(jiān)聽
style ButtonStyle樣式侦厚,詳情見下文
clipBehavior 參考RaisedButton的clipBehavior屬性
child 子組件
9.3、ButtonStyle的源碼及屬性介紹
const ButtonStyle({
    this.textStyle,
    this.backgroundColor,
    this.foregroundColor,
    this.overlayColor,
    this.shadowColor,
    this.elevation,
    this.padding,
    this.minimumSize,
    this.side,
    this.shape,
    this.mouseCursor,
    this.visualDensity,
    this.tapTargetSize,
    this.animationDuration,
    this.enableFeedback,
  });
屬性 說明
textStyle 文本樣式,設(shè)置文字的大小和粗細(xì)
backgroundColor 背景色
foregroundColor 前景色拙徽,即文本的顏色和圖標(biāo)的顏色
overlayColor 水波紋顏色
shadowColor 陰影顏色
elevation 陰影高度
padding 內(nèi)間距
minimumSize 設(shè)置按鈕的大小
side 邊框設(shè)置
shape 外邊框樣式會(huì)覆蓋side,參考RaisedButton的shape屬性刨沦,使用方法有點(diǎn)不同,具體使用見代碼
visualDensity 參考RaisedButton的visualDensity屬性
tapTargetSize 配置按鈕被按下區(qū)域的最小尺寸
animationDuration 動(dòng)畫變化的持續(xù)時(shí)間
enableFeedback 檢測(cè)到的手勢(shì)是否應(yīng)該提供聽覺和/或觸覺反饋
9.4膘怕、TextButton的demo
home: Scaffold(
          appBar: AppBar(
            title: Text("TextButton學(xué)習(xí)"),
            actions: [],
          ),
          body: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.all(10),
              child: TextButton(
                child: Text("文本按鈕"),
                onPressed: () {
                  print("onPressed");
                },
                onLongPress: () {
                  print("onLongPress");
                },
                style: ButtonStyle(
                  //設(shè)置顏色無效
                  textStyle: MaterialStateProperty.all(
                      TextStyle(fontSize: 18, color: Colors.red)),

                  foregroundColor: MaterialStateProperty.resolveWith(
                    (states) {
                      if (states.contains(MaterialState.pressed)) {
                        ////按下時(shí)的顏色
                        return Colors.red;
                      }
                      //默認(rèn)狀態(tài)使用灰色
                      return Colors.black;
                    },
                  ),

                  backgroundColor: MaterialStateProperty.resolveWith((states) {
                    //設(shè)置按下時(shí)的背景顏色
                    if (states.contains(MaterialState.pressed)) {
                      return Colors.yellow;
                    }
                    //默認(rèn)不使用背景顏色
                    return null;
                  }),
                //設(shè)置水波紋顏色
                  overlayColor: MaterialStateProperty.all(Colors.purpleAccent),
                  //設(shè)置按鈕大小
                  minimumSize: MaterialStateProperty.all(Size(150, 80)),
                  //設(shè)置邊框
                  side: MaterialStateProperty.all(
                      BorderSide(color: Colors.red, width: 1)),
                  //設(shè)置按鈕形狀
                  shape: MaterialStateProperty.all(StadiumBorder(
                    side: BorderSide(
                        color: Colors.red, width: 1, style: BorderStyle.solid),
                  )),
                ),
              ))),
2020.12.22.16.57.09.gif

十已卷、ElevatedButton

10.1、ElevatedButton的源碼
const ElevatedButton({
    Key key,
    @required VoidCallback onPressed,
    VoidCallback onLongPress,
    ButtonStyle style,
    FocusNode focusNode,
    bool autofocus = false,
    Clip clipBehavior = Clip.none,
    @required Widget child,
  }) : super(
    key: key,
    onPressed: onPressed,
    onLongPress: onLongPress,
    style: style,
    focusNode: focusNode,
    autofocus: autofocus,
    clipBehavior: clipBehavior,
    child: child,
  );
10.1淳蔼、ElevatedButton的屬性介紹,參考TextButton的屬性介紹

十一裁眯、OutlinedButton

11.1鹉梨、OutlinedButton的源碼
const OutlinedButton({
    Key key,
    @required VoidCallback onPressed,
    VoidCallback onLongPress,
    ButtonStyle style,
    FocusNode focusNode,
    bool autofocus = false,
    Clip clipBehavior = Clip.none,
    @required Widget child,
  }) : super(
    key: key,
    onPressed: onPressed,
    onLongPress: onLongPress,
    style: style,
    focusNode: focusNode,
    autofocus: autofocus,
    clipBehavior: clipBehavior,
    child: child,
  );
10.1、OutlinedButton的屬性介紹穿稳,參考TextButton的屬性介紹
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末存皂,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子逢艘,更是在濱河造成了極大的恐慌旦袋,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,383評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件它改,死亡現(xiàn)場(chǎng)離奇詭異疤孕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)央拖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門祭阀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鹉戚,“玉大人,你說我怎么就攤上這事专控∧ǖ剩” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵伦腐,是天一觀的道長(zhǎng)赢底。 經(jīng)常有香客問我,道長(zhǎng)柏蘑,這世上最難降的妖魔是什么幸冻? 我笑而不...
    開封第一講書人閱讀 56,621評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮辩越,結(jié)果婚禮上嘁扼,老公的妹妹穿的比我還像新娘。我一直安慰自己黔攒,他們只是感情好趁啸,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,741評(píng)論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著督惰,像睡著了一般不傅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赏胚,一...
    開封第一講書人閱讀 49,929評(píng)論 1 290
  • 那天访娶,我揣著相機(jī)與錄音,去河邊找鬼觉阅。 笑死崖疤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的典勇。 我是一名探鬼主播劫哼,決...
    沈念sama閱讀 39,076評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼割笙!你這毒婦竟也來了权烧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤伤溉,失蹤者是張志新(化名)和其女友劉穎般码,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體乱顾,經(jīng)...
    沈念sama閱讀 44,265評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡板祝,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,582評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了走净。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扔字。...
    茶點(diǎn)故事閱讀 38,716評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡囊嘉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出革为,到底是詐尸還是另有隱情扭粱,我是刑警寧澤,帶...
    沈念sama閱讀 34,395評(píng)論 4 333
  • 正文 年R本政府宣布震檩,位于F島的核電站琢蛤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏抛虏。R本人自食惡果不足惜博其,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,039評(píng)論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望迂猴。 院中可真熱鬧慕淡,春花似錦、人聲如沸沸毁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽息尺。三九已至携兵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間搂誉,已是汗流浹背徐紧。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留炭懊,地道東北人并级。 一個(gè)月前我還...
    沈念sama閱讀 46,488評(píng)論 2 361
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像侮腹,于是被迫代替她去往敵國(guó)和親嘲碧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,612評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容