flutter-Button

ElevatedButton

即"漂浮"按钮,它默认带有阴影和灰色背景。按下后,阴影会变大

圆角设置

 ElevatedButton(
   style: ButtonStyle(
     shape: MaterialStateProperty.all(RoundedRectangleBorder(
       borderRadius: BorderRadius.circular(20)))),
        onPressed: (() {}),
        child: Text("ElevatedButton"),
   )

边框颜色和宽度

 ElevatedButton(
     style: ButtonStyle(
     side: MaterialStateProperty.all(
      BorderSide(color: Colors.red,width: 4))),
                  onPressed: (() {}),
                  child: Text("ElevatedButton"),
                )


设置按钮背景

ElevatedButton(
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.yellow)),
                  onPressed: (() {}),
                  child: Text("ElevatedButton"),
                )

设置按钮字体颜色

 ElevatedButton(
                  style: ButtonStyle(
                    foregroundColor: MaterialStateProperty.all(Colors.yellow)),
                  onPressed: (() {}),
                  child: Text("ElevatedButton"),
                )

指定按钮的宽高

  ElevatedButton(
                  style: ButtonStyle(
                   fixedSize:MaterialStateProperty.all(Size(200,300))),
                  onPressed: (() {}),
                  child: Text("ElevatedButton"),
                )

只指定宽

fixedSize:MaterialStateProperty.all(Size.fromWidth(200))),

只指定高

fixedSize:MaterialStateProperty.all(Size.fromHeight(300))),

minimumSize和maximumSize 显示按钮的最小或最大的尺寸,fixedSize任受它们的约束

 ElevatedButton(
                  style: ButtonStyle(
                   fixedSize:MaterialStateProperty.all(Size.fromWidth(300))
                   ,maximumSize: MaterialStateProperty.all(Size.fromWidth(100))),
                  onPressed: (() {}),
                  child: Text("ElevatedButton"),
                )


最大宽度设置100,但实际宽度设置300,最终显示100的宽度 

设置按钮字体大小

                ElevatedButton(
                  style: ButtonStyle(
                    textStyle: MaterialStateProperty.all(TextStyle(fontSize: 30)),
                   ),
                  onPressed: (() {}),
                  child: Text("ElevatedButton"),
                )

设置按钮内间距

在上面设置字体大小为30的前提下

                ElevatedButton(
                  style: ButtonStyle(
                    textStyle: MaterialStateProperty.all(TextStyle(fontSize: 30)),
                    padding: MaterialStateProperty.all(EdgeInsets.all(30)),
                   ),
                  onPressed: (() {}),
                  child: Text("ElevatedButton"),
                )

在指定宽度的前提下,设置按钮里面文字显示的位置

 按钮里文字右边居中显示:

 ElevatedButton(
                  style: ButtonStyle(
                    alignment:Alignment.centerRight,
                    fixedSize: MaterialStateProperty.all(Size.fromWidth(300))
                   ),
                  onPressed: (() {}),
                  child: Text("Button"),
                )

 按钮里文字左边居中显示:

  ElevatedButton(
                  style: ButtonStyle(
                    alignment:Alignment.centerLeft,
                    fixedSize: MaterialStateProperty.all(Size.fromWidth(300))
                   ),
                  onPressed: (() {}),
                  child: Text("Button"),
                )

Alignment属性

/// The top left corner.
  static const Alignment topLeft = Alignment(-1.0, -1.0);


  /// The center point along the top edge.
  static const Alignment topCenter = Alignment(0.0, -1.0);


  /// The top right corner.
  static const Alignment topRight = Alignment(1.0, -1.0);


  /// The center point along the left edge.
  static const Alignment centerLeft = Alignment(-1.0, 0.0);


  /// The center point, both horizontally and vertically.
  static const Alignment center = Alignment(0.0, 0.0);


  /// The center point along the right edge.
  static const Alignment centerRight = Alignment(1.0, 0.0);


  /// The bottom left corner.
  static const Alignment bottomLeft = Alignment(-1.0, 1.0);


  /// The center point along the bottom edge.
  static const Alignment bottomCenter = Alignment(0.0, 1.0);


  /// The bottom right corner.
  static const Alignment bottomRight = Alignment(1.0, 1.0);

阴影颜色,来点青青草原色

 ElevatedButton(
                  style: ButtonStyle(
                  shadowColor:MaterialStateProperty.all(Colors.green)
                   ),
                  onPressed: (() {}),
                  child: Text("Button"),
                )


不是很明显

  • elevation    // 阴影值

ElevatedButton(
                  style: ButtonStyle(
                  elevation :   MaterialStateProperty.all(10),            
                  shadowColor:MaterialStateProperty.all(Colors.red)
                   ),
                  onPressed: (() {}),
                  child: Text("Button"),
                )

现在明显多了

overlayColor 高亮色 按下时候的颜色

 ElevatedButton(
              style: ButtonStyle(
                overlayColor: MaterialStateProperty.all(Colors.red),
              ),
              onPressed: (() {}),
              child: Text("Button"),
            )


OutlinedButton 

  OutlinedButton(
      onPressed: (() {}),
     child: Text("OutlinedButton"),
    )

 TextButton

 TextButton(
      onPressed: (() {}),
       child: Text("TextButton"),
      )

附上官网地址:ButtonStyle class - material library - Dart API

Flutter中,`MenuItemButton`并不是原生提供的控件,而是来自某些第三方库,比如`flutter_staggered_grid_view`或者自定义的UI组件包。`MenuItemButton`通常用于构建类似于菜单式的交互元素,用户可以选择其中的一个项作为操作。 这种按钮通常包含一个`DropdownMenu`或类似的选择列表,用户可以点击下拉箭头展开选项,并从中选择一个。当你需要提供一组选项供用户选择时,可能会用到这样的结构: ```dart import 'package:flutter/material.dart'; class MenuItemButtonExample extends StatelessWidget { @override Widget build(BuildContext context) { final List<String> items = ['Option 1', 'Option 2', 'Option 3']; return DropdownButtonMenuItem<String>( value: 'Option 1', child: Text('Item 1'), onTap: () { // 当用户选择此选项时,可以执行相应的动作 print('Selected: Option 1'); }, isDisabled: false, // 可选,设置按钮是否禁用 groupValue: items, // 通常用作`DropdownButton`中的组值 // 可能会有`MenuItemButton`包装这个DropdownButtonMenuItem itemBuilder: (context, index) { return MenuItemButton( onPressed: () { setState(() { // 更新当前选中的值 items[index] = items[index].toString().toUpperCase(); }); }, child: DropdownButtonMenuItem<String>( value: items[index], child: Text(items[index]), ), ); }, ); } } ``` 使用`MenuItemButton`时需要注意的是,需要配合`DropdownButton`一起使用,并管理下拉列表的状态。如果你没有找到相关的库或想要自定义组件,可以根据需求创建一个自定义的Dropdown或MenuItem组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Chen_ShengJie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值