• Flutter 中的常见的按钮组件 以及自定义按钮组件


    Flutter 里有很多的 Button 组件很多,常见的按钮组件有:RaisedButton、FlatButton、

    IconButton、OutlineButton、ButtonBar、FloatingActionButton 等。

    RaisedButton :凸起的按钮,其实就是 Material Design 风格的 Button FlatButton :扁平化的按钮
    OutlineButton:线框按钮
    IconButton :图标按钮

    ButtonBar:按钮组 FloatingActionButton:浮动按钮


    Flutter 按钮组件中的一些属性

    属性名称

    值类型

    属性值

    onPressed

    VoidCallback ,一般接收一个 方法

    必填参数,按下按钮时触发的回调,接收一个 方法,传 null 表示按钮禁用,会显示禁用相关 样式

    child

    Widget

    文本控件

    textColor

    Color

    文本颜色

    color

    Color

    按钮的颜色

    disabledColor
    

    Color

    按钮禁用时的颜色

    disabledTextColor
    

    Color

    按钮禁用时的文本颜色

    splashColor

    Color

    点击按钮时水波纹的颜色

    highlightColor
    

    Color

    点击(长按)按钮后按钮的颜色
    

    elevation

    double

    阴影的范围,值越大阴影范围越大
    

    padding

     

    内边距

    shape

     

    设置按钮的形状

    shape: RoundedRectangleBorder(

         borderRadius:
    BorderRadius.circular(10),
    )
    
    shape: CircleBorder(
          side: BorderSide(
    
              color: Colors.white,
          )
    

    )

    import 'package:flutter/material.dart';
    
    class ButtonDemoPage extends StatelessWidget {
      const ButtonDemoPage({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("按钮演示页面"),
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.settings),
                  onPressed: (){
    
                  },
                )
              ],
            ),
            body: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    RaisedButton(
                      child: Text('普通按钮'),
                      onPressed: () {
                        print("普通按钮");
                      },
                    ),
                    SizedBox( 5),
                    RaisedButton(
                      child: Text('颜色按钮'),
                      color: Colors.blue,
                      textColor: Colors.white,
                      onPressed: () {
                        print("有颜色按钮");
                      },
                    ),
                    SizedBox( 5),
                    RaisedButton(
                      child: Text('阴影按钮'),
                      color: Colors.blue,
                      textColor: Colors.white,
                      elevation: 20,
                      onPressed: () {
                        print("有阴影按钮");
                      },
                    ),
                    SizedBox( 5),
                    RaisedButton.icon(
                        icon: Icon(Icons.search),
                        label: Text('图标按钮'),
                        color: Colors.blue,
                        textColor: Colors.white,
                        // onPressed: null,
                        onPressed: () {
                          print("图标按钮");
                        })
                  ],
                ),
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      height: 50,
                       400,
                      child: RaisedButton(
                        child: Text('宽度高度'),
                        color: Colors.blue,
                        textColor: Colors.white,
                        elevation: 20,
                        onPressed: () {
                          print("宽度高度");
                        },
                      ),
                    )
                  ],
                ),
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      child: Container(
                        height: 60,
                        margin: EdgeInsets.all(10),
                        child: RaisedButton(
                          child: Text('自适应按钮'),
                          color: Colors.blue,
                          textColor: Colors.white,
                          elevation: 20,
                          onPressed: () {
                            print("自适应按钮");
                          },
                        ),
                      ),
                    )
                  ],
                ),
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    RaisedButton(
                        child: Text('圆角按钮'),
                        color: Colors.blue,
                        textColor: Colors.white,
                        elevation: 20,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)),
                        onPressed: () {
                          print("圆角按钮");
                        }),
                    Container(
                      height: 80,
                      child: RaisedButton(
                          child: Text('圆形按钮'),
                          color: Colors.blue,
                          textColor: Colors.white,
                          elevation: 20,
                          splashColor: Colors.red,
                          shape:
                              CircleBorder(side: BorderSide(color: Colors.white)),
                          onPressed: () {
                            print("圆形按钮");
                          }),
                    ),
                    FlatButton(
                      child: Text("按钮"),
                      color: Colors.blue,
                      textColor: Colors.yellow,
                      onPressed: () {
                        print('FlatButton');
                      },
                    ),
                    SizedBox( 10),
                    OutlineButton(
                        child: Text("按钮"),
                        //  color: Colors.red,  //没有效果
                        //  textColor: Colors.yellow,
                        onPressed: () {
                          print('FlatButton');
                        })
                  ],
                ),
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      child: Container(
                        margin: EdgeInsets.all(20),
                        height: 50,
                        child: OutlineButton(child: Text("注册"), onPressed: () {}),
                      ),
                    )
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    ButtonBar(
                      children: <Widget>[
    
                        RaisedButton(
                          child: Text('登录'),
                          color: Colors.blue,
                          textColor: Colors.white,
                          elevation: 20,
                          onPressed: () {
                            print("宽度高度");
                          },
                        ),
                        RaisedButton(
                          child: Text('注册'),
                          color: Colors.blue,
                          textColor: Colors.white,
                          elevation: 20,
                          onPressed: () {
                            print("宽度高度");
                          },
                        ),
                        MyButton(text: "自定义按钮",height: 60.0, 100.0,pressed: (){
                          print('自定义按钮');
                        })
                        
                      ],
                    )
                  ],
                )
              ],
            ));
      }
    }
    
    
    //自定义按钮组件
    
    class MyButton extends StatelessWidget {
      final text;
      final pressed;
      final width;
      final height;
      const MyButton({this.text='',this.pressed=null,this.width=80,this.height=30}) ;
    
      @override
      Widget build(BuildContext context) {
        return Container(
          height: this.height,
           this.width,
          child: RaisedButton(
            child: Text(this.text),
            onPressed:this.pressed ,
          ),
        );
      }
    }
  • 相关阅读:
    Linux ln命令
    VSCodeUserSetupx641.67.2
    Where windows subsystem for linux (WSL) File/Directory Location?
    Bash 脚本实例:获取符号链接的目标位置
    叮,GitHub 到账 550 美元「GitHub 热点速览 v.22.26」
    用 40 块搞个游戏机“万能卡”「GitHub 热点速览 v.22.27」
    《HelloGitHub》第 75 期
    termux开启ssh服务
    buildroot使用介绍
    adb常用命令
  • 原文地址:https://www.cnblogs.com/loaderman/p/11249855.html
Copyright © 2020-2023  润新知