Flutter的button的按钮ElevatedButton

前言:

        Flutter 1.22版本新增了3个按钮,TextButtonOutlinedButtonElevatedButton,虽然以前的Button没有被废弃,但还是建议使用新的Button

官网api入口:ButtonStyle

目录:

更多button按钮入口(带图标,或者就是一个图标的按钮)

使用方法:他们的使用方法都一样

1、 TextButton:

效果:

2、 OutlinedButton:

效果:

3、ElevatedButton

效果:

属性API:

1、点击事件 onPressed

2、长按事件 onLongPress 

3、属性:

textStyle  字体

backgroundColor  背景颜色 

foregroundColor  字体颜色

overlayColor  高亮色,按钮处于focused, hovered, or pressed时的颜色

side  边框

shadowColor 阴影颜色

elevation  阴影值

shape  形状-可设置圆角弧度


 

更多button按钮入口(带图标,或者就是一个图标的按钮)

使用方法:他们的使用方法都一样

1、 TextButton:

TextButton(
     child: Text("爱你"),
     onPressed: () {},
);

效果:

2、 OutlinedButton:

OutlinedButton(
     child: Text("爱你"),
     onPressed: () {},
);

效果:

3、ElevatedButton

ElevatedButton(
     child: Text("爱你"),
     onPressed: () {},
);

效果:

属性API:

1、点击事件 onPressed

ElevatedButton(
     child: Text("爱你"),
     onPressed: () {
         print('我被点击了');
    },
);

2、长按事件 onLongPress 

ElevatedButton(
     child: Text("爱你"),
     onLongPress : () {
         print('我被长按了');
    },
);

3、属性:

  •  textStyle                  //字体
  • backgroundColor     //背景色
  •  foregroundColor     //字体颜色
  • overlayColor            // 高亮色,按钮处于focused, hovered, or pressed时的颜色
  • shadowColor           // 阴影颜色
  • elevation                 // 阴影值
  • padding                  // padding
  • minimumSize         //最小尺寸
  • side                        //边框
  • shape                     //形状
  • mouseCursor         //鼠标指针的光标进入或悬停在此按钮的[InkWell]上时
  • visualDensity         // 按钮布局的紧凑程度
  • tapTargetSize        // 响应触摸的区域
  • animationDuration //[shape]和[elevation]的动画更改的持续时间。
  • enableFeedback   // 检测到的手势是否应提供声音和/或触觉反馈。例如,在Android上,点击会产生咔哒声,启用反馈后,长按会产生短暂的振动。通常,组件默认值为true。

textStyle  字体

ElevatedButton(
          child: Text("爱你"),
          style: ButtonStyle(
            textStyle: MaterialStateProperty.all(TextStyle(fontSize: 16)),                //字体
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

backgroundColor  背景颜色 

ElevatedButton(
          child: Text("爱你"),
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(Color(0xffEDFCF5)),                //背景颜色 
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

foregroundColor  字体颜色

ElevatedButton(
          child: Text("爱你"),
          style: ButtonStyle(
            foregroundColor: MaterialStateProperty.all(Color(0xff31C27C)),                //字体颜色
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

overlayColor  高亮色,按钮处于focused, hovered, or pressed时的颜色

ElevatedButton(
          child: Text("爱你"),
          style: ButtonStyle(
            overlayColor: MaterialStateProperty.all(Color(0xff31C27C)),                //字体颜色
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

side  边框

ElevatedButton(
          child: Text("爱你"),
          style: ButtonStyle(
            side: MaterialStateProperty.all(BorderSide(width: 1,color: Color(0xffffffff))),//边框
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

shadowColor 阴影颜色

ElevatedButton(
          child: Text("爱你"),
          style: ButtonStyle(
            shadowColor: MaterialStateProperty.all(Colors.red),
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

elevation  阴影值

ElevatedButton(
          child: Text("爱你"),
          style: ButtonStyle(
            elevation: MaterialStateProperty.all(10),                                     //阴影值
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

shape  形状-可设置圆角弧度

(1)棱形,如果不设置边框,可以实现圆角弧度那种效果,设置边框就是棱形

ElevatedButton(
          child: Text("审核完成"),
          style: ButtonStyle(
            side: MaterialStateProperty.all(BorderSide(width: 1,color: Color(0xffCAD0DB))),//边框
            shape: MaterialStateProperty.all(BeveledRectangleBorder(borderRadius: BorderRadius.circular(8))),//圆角弧度
          ),
          onPressed: () {
            print('我被点击了');
          },
        )

ElevatedButton(
      child: Text("学习报告"),
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(Color(0xffFFF8E5)),                //背景颜色
        foregroundColor: MaterialStateProperty.all(Color(0xffB85F23)),                //字体颜色
        overlayColor: MaterialStateProperty.all(Color(0xffFFF8E5)),                   // 高亮色
        shadowColor: MaterialStateProperty.all( Color(0xffffffff)),                  //阴影颜色
        elevation: MaterialStateProperty.all(0),                                     //阴影值
        textStyle: MaterialStateProperty.all(TextStyle(fontSize: 12)),                //字体
        side: MaterialStateProperty.all(BorderSide(width: 1,color: Color(0xffffffff))),//边框
        shape: MaterialStateProperty.all(BeveledRectangleBorder(borderRadius: BorderRadius.circular(8))),//圆角弧度
      ),
      onPressed: () {},
    );

(2)圆形

ElevatedButton(
          child: Text("审"),
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(Color(0xffffffff)),                //背景颜色
            foregroundColor: MaterialStateProperty.all(Color(0xff5E6573)),                //字体颜色
            overlayColor: MaterialStateProperty.all(Color(0xffffffff)),                   // 高亮色
            shadowColor: MaterialStateProperty.all( Color(0xffffffff)),                  //阴影颜色
            elevation: MaterialStateProperty.all(0),                                     //阴影值
            textStyle: MaterialStateProperty.all(TextStyle(fontSize: 12)),                //字体
            side: MaterialStateProperty.all(BorderSide(width: 1,color: Color(0xffCAD0DB))),//边框
            shape: MaterialStateProperty.all(
                CircleBorder(
                    side: BorderSide(
                      //设置 界面效果
                      color: Colors.green,
                      width: 280.0,
                      style: BorderStyle.none,
                    )
                )
            ),//圆角弧度
            
          ),
          onPressed: () {},
        )

(3)圆角弧度   (这个直接就是)

ElevatedButton(
          child: Text("审核完成"),
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(Color(0xffffffff)),                //背景颜色
            foregroundColor: MaterialStateProperty.all(Color(0xff5E6573)),                //字体颜色
            overlayColor: MaterialStateProperty.all(Color(0xffffffff)),                   // 高亮色
            shadowColor: MaterialStateProperty.all( Color(0xffffffff)),                  //阴影颜色
            elevation: MaterialStateProperty.all(0),                                     //阴影值
            textStyle: MaterialStateProperty.all(TextStyle(fontSize: 12)),                //字体
            side: MaterialStateProperty.all(BorderSide(width: 1,color: Color(0xffCAD0DB))),//边框
           

            shape: MaterialStateProperty.all(
                StadiumBorder(
                    side: BorderSide(
                      //设置 界面效果
                      style: BorderStyle.solid,
                      color: Color(0xffFF7F24),
                    )
                )
            ),//圆角弧度


          ),

 

 

 

参考文档1:https://blog.csdn.net/mengks1987/article/details/109480289

参考文档2:https://blog.csdn.net/weixin_34166847/article/details/91401446

到此为止!

  • 29
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
Flutter中,ButtonStyle是用来自定义按钮样式的一个类。通过使用ButtonStyle,你可以修改按钮的背景颜色、文本样式、边框等属性,以创建自己需要的按钮样式。ButtonStyle可以应用于多种类型的按钮,比如ElevatedButton、TextButton和IconButton等。\[1\] 举个例子,如果你想创建一个自定义的ElevatedButton,你可以使用ButtonStyle来设置按钮的背景颜色、文本样式等。下面是一个使用ButtonStyle的示例代码: ```dart ElevatedButton( onPressed: () {}, child: Text('Custom Button'), style: ButtonStyle( backgroundColor: MaterialStateProperty.all<Color>(Colors.blue), textStyle: MaterialStateProperty.all<TextStyle>(TextStyle(color: Colors.white)), // 其他样式属性 ), ) ``` 在上面的代码中,我们通过ButtonStyle的backgroundColor属性设置了按钮的背景颜色为蓝色,通过textStyle属性设置了按钮文本的颜色为白色。你还可以根据需要设置其他样式属性,比如边框、阴影等。 除了ElevatedButton,你也可以在TextButton和IconButton中使用ButtonStyle来自定义按钮样式。例如,下面是一个使用ButtonStyle的TextButton的示例代码: ```dart TextButton( onPressed: () {}, child: Text('Custom TextButton'), style: ButtonStyle( foregroundColor: MaterialStateProperty.all<Color>(Colors.red), // 其他样式属性 ), ) ``` 总结起来,ButtonStyle是一个用于自定义按钮样式的类,可以应用于多种类型的按钮。通过设置ButtonStyle的各种属性,你可以轻松地创建自己需要的按钮样式。\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [【Flutter】关于Button 的那些知识ElevatedButton等,以及Buttonstyle](https://blog.csdn.net/weixin_43444734/article/details/128582374)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Flutter TextButton 详细使用配置、Flutter ButtonStyle样式概述实践](https://blog.csdn.net/zl18603543572/article/details/109545733)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浩星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值