Flutter Button 按钮

目录

参数详解

代码示例

效果图

完整代码 


 在 Flutter 里有很多的 Button,包括了:MaterialButton、RaisedButton、FloatingActionButton、FlatButton、IconButton、ButtonBar、DropdownButton 等。

一般常用的 Button 是 MaterialButton、IconButton、FloatingActionButton。

RaisedButton:凸起的按钮,其实就是 Material Design 风格的 Button
FlatButton:扁平化的按钮
OutlineButton:线框按钮
IconButton:图标按钮
ButtonBar:按钮组
FloatingActionButton:浮动按钮
DropdownButton:下拉框按钮

 

参数详解

属性说明
onPressed点击事件监听,传 null 表示按钮禁用
onHighlightChanged水波纹高亮变化回调,按下返回true,抬起返回false
textTheme定义按钮主题
textColor按钮文字颜色
disabledTextColor无效按钮文字颜色
color按钮颜色
disabledColor无效按钮颜色
focusColor获取焦点 按钮颜色
hoverColor不知道啥玩应儿
highlightColor长按 按钮颜色
splashColor点击 水波纹 颜色
colorBrightness官网:用于此按钮的主题亮度。默认为主题的亮度
elevation阴影
focusElevation
hoverElevation
highlightElevation
disabledElevation
padding内边距
shape设置形状,如圆角,圆形等
clipBehavior

剪裁

Clip.antiAlias:剪辑具有抗锯齿功能

Clip.antiAliasWithSaveLayer:在剪辑后立即剪辑具有抗锯齿和saveLayer

Clip.hardEdge:剪辑,但不应用抗锯齿。

Clip.none:不剪辑。

focusNode 
materialTapTargetSize 
animationDuration 
child 

OutlineButton 特性

borderSide

线框    线颜色 ,如红色:BorderSide(color: Colors.red,),

clipBehavior

相框风格,如:Clip.antiAlias

RaisedButton.icon 特性

icon

图标

label

通常是文字

IconButton 特性

icon

图标

color

图标颜色

tooltip

长按文字提示

DropdownButton 特性    DropdownButton

hint

提示语

value

当前值

iconSize

下拉框图片大小
icon右边图标  默认为下三角

items

下拉框数据集合

onChanged

监听

FloatingActionButton 特性

child子元素,一般为 Icon,不推荐使用文字
tooltip长按文字提示
backgroundColor背景颜色(默认使用主题颜色)
mini是否是 mini 类型默认 false
设置位置,在外部使用(与FloatingActionButton同级)floatingActionButtonLocation。

FloatingActionButtonLocation.centerDocked: 底部剧中  与底部无间距

FloatingActionButtonLocation.centerFloat: 底部剧中  与底部有间距

FloatingActionButtonLocation.endDocked:右下角  与底部无间距

FloatingActionButtonLocation.endFloat:右下角  与底部有间距

FloatingActionButtonLocation.endTop:右上角  

FloatingActionButtonLocation.startTop:左上角

代码示例

return Container(
      padding: EdgeInsets.all(8),
      child: Column(
        children: <Widget>[
          Wrap(
            spacing: 8,
            runSpacing: 8,
            children: <Widget>[
              RaisedButton(
                child: Text('普通按钮'),
                onHighlightChanged:(bool b) {
                  print(b);
                },
                onPressed: (){},
              ),

              RaisedButton(
                child: Text('带颜色'),
                onPressed: (){},
                textColor: Colors.white,
                color: Colors.blue[200],
              ),

              RaisedButton(
                child: Text('带颜色带阴影'),
                onPressed: (){},
                textColor: Colors.white,
                color: Colors.blue[200],
                elevation: 15,
              ),

              Container(
                width: 300,
                height: 50,
                child: RaisedButton(
                  child: Text('设置宽高'),
                  onPressed: (){},
                  textColor: Colors.white,
                  color: Colors.blue[500],
                  elevation: 15,
                ),
              ),

              RaisedButton.icon(
                icon: Icon(Icons.account_box),
                label: Text('我前边有图标'),
                onPressed: () {},
              ),

              RaisedButton(
                child: Text('圆角按钮'),
                onPressed: (){},
                color: Colors.red,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)
                ),
              ),

              Container(
                width: 100,
                height: 100,
                child: RaisedButton(
                    child: Text('圆形按钮'),
                    onPressed: (){},
                    color: Colors.red,
                    shape: CircleBorder(
                      // side: BorderSide(color: Colors.yellow[500])
                    )
                ),
              ),

              RaisedButton(
                child: Text('水波纹'),
                onPressed: (){},
                color: Colors.blue[200],
                splashColor:Colors.yellow[100],
              ),

              RaisedButton(
                child: Text('长按变色'),
                onPressed: (){},
                color: Colors.blue[200],
                highlightColor:Colors.red[500],
              ),

              FlatButton(
                child: Text('扁平按钮'),
                onPressed: (){},
                color: Colors.blue[200],
              ),

              OutlineButton(
                child: Text('边框按钮'),
                onPressed: (){},
                textColor: Colors.red,
                borderSide: BorderSide(color: Colors.red,),
              ),

              IconButton(
                icon: Icon(Icons.access_alarms),
                onPressed: () {},
                color: Colors.deepOrange,
                splashColor:Colors.limeAccent,
                highlightColor:Colors.blue[300],
                tooltip:'干啥',
              ),

              DropdownButton(
                hint:new Text('请选择...'),
                value: value,//下拉菜单选择完之后显示给用户的值
                iconSize: 50.0,//设置三角标icon的大小
                items: <DropdownMenuItem>[
                  DropdownMenuItem(
                    value: '1',
                    child: Text('One'),
                  ),
                  DropdownMenuItem(
                    value: '2',
                    child: Text('Two'),
                  ),
                  DropdownMenuItem(
                    value: '3',
                    child: Text('Three'),
                  ),
                  DropdownMenuItem(
                    value: '4',
                    child: Text('four'),
                  ),
                  DropdownMenuItem(
                    value: '5',
                    child: Text('five'),
                  ),
                ],
                onChanged: (v) {
                  setState(() {
                    print(v);
                    value = v;
                  });
                },
              ),
            ],
          ),


          Container(
            color: Colors.pink[100],
            child: ButtonBar(
              children: <Widget>[
                RaisedButton(
                  child: Text('按钮一'),
                  onPressed: (){},
                  textColor: Colors.white,
                  color: Colors.blue,
                  elevation: 15,
                ),
                RaisedButton(
                  child: Text('按钮二'),
                  onPressed: (){},
                  textColor: Colors.white,
                  color: Colors.blue,
                  elevation: 15,
                ),
              ],
            ),
          ),

          Container(
            alignment: Alignment.center,
            child: Column(
              children: <Widget>[
                SizedBox(height: 30,),
                Text("一个Button事件与回调,更改数值"),

                SizedBox(height: 15,),
                Text("$count",style: TextStyle(fontSize: 50,color: Colors.purple,fontWeight:FontWeight.w800)),
                SizedBox(height: 20,),

                RaisedButton(
                  child: Text('点我',style: TextStyle(fontSize: 18),),
                  onPressed: (){setState(() {
                    count += 1;
                  });},
                  textColor: Colors.white,
                  color: Colors.blue,
                  elevation: 15,
                ),
              ],
            ),
          )
        ],
      ),
    );
        //floatingActionButton 按钮
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.red[500],
          child: Icon(Icons.add_comment),
          tooltip:'干啥',
          onPressed: () {print('我是Floating Action Button');},
        ),
        //floatingActionButton 按钮位置
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,

效果图

 

 

完整代码 

 查看完整代码

### Flutter 按钮的用法与示例 在 Flutter 中,按钮是一种常见的 UI 组件,用于触发特定的操作或事件。Flutter 提供了多种类型的按钮组件来满足不同的设计需求。以下是几种常用的按钮及其使用方法: #### 1. ElevatedButton `ElevatedButton` 是一种带有阴影效果的按钮,默认情况下具有悬浮的效果。 ```dart import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @Override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Elevated Button Example')), body: Center( child: ElevatedButton( onPressed: () { print("ElevatedButton pressed"); }, style: ElevatedButton.styleFrom(primary: Colors.blue), child: Text('Press Me'), ), ), ), ); } } ``` 此代码展示了 `ElevatedButton` 的基本用法[^1]。 #### 2. TextButton `TextButton` 是一种无背景填充的按钮,通常用于次要操作或者链接样式的行为。 ```dart TextButton( onPressed: () { print("TextButton pressed"); }, child: Text('Tap Here', style: TextStyle(color: Colors.red)), ); ``` 通过设置文字颜色和其他属性可以自定义其外观。 #### 3. IconButton 当需要一个仅显示图标的按钮时,可以选择 `IconButton`。 ```dart IconButton( icon: Icon(Icons.thumb_up), color: Colors.green, onPressed: () { print("Icon Button Pressed"); }, ); ``` 这种按钮非常适合工具栏或其他空间有限的地方。 #### 4. FloatingActionButton 浮动动作按钮 (`FloatingActionButton`) 常见于屏幕底部右下角位置,用来表示主要的动作。 ```dart floatingActionButton: FloatingActionButton( onPressed: () { print("FAB clicked!"); }, tooltip: 'Increment', child: Icon(Icons.add), ), ``` 它常被放置在一个页面的主要交互区域附近。 以上就是一些基础的按钮类型介绍和简单的实现例子。开发者可以根据实际项目的需求调整它们的设计风格以匹配整体应用的主题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马志武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值