当前位置:   article > 正文

Flutter - 按钮_flutter 按钮

flutter 按钮

普通按钮

  • TextButton - 文本按钮
  • OutlineButton - 空心按钮
  • ElevatedButton - 凸起按钮

按钮属性

属性类型说明
onPressedFunction点击事件
onLongPressFunction长按事件
childWidget子组件
styleButtonStyle自定义样式

ButtonStyle 属性

属性类型说明
textStyleMaterialStateProperty字体样式
foregroundColorMaterialStateProperty按钮点击时字体样式
backgroundColorMaterialStateProperty背景色
shadowColorMaterialStateProperty阴影
elevationMaterialStateProperty阴影长度
sideMaterialStateProperty边框
shapeMaterialStateProperty圆角
minimumSizeMaterialStateProperty按钮大小
overlayColorMaterialStateProperty水波纹的颜色

主题相关的按钮

  • TextButtonTheme - 文本按钮
  • OutlinedButtonTheme- 空心按钮
  • ElevatedButtonTheme - 凸起按钮

注意:如果设置style 样式,则主题样式不生效

IconButton - 图标按钮

  • TextButton.icon - 图标文本按钮
  • OutlineButton.icon - 图标空心按钮
  • ElevatedButton.icon - 图标凸起按钮
  • IconButton - 图标按钮

其他按钮

  • ButtonBar - 按钮组
  • BackButton - 回退按钮
  • CloseButton - 关闭按钮
  • FloatingActionButton - 浮动按钮

注意:浮动按钮配合Scaffold使用,与appBar、body 等是同级

代码示例

在这里插入图片描述

class Body extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(20),
      child: Wrap(
        spacing: 10,
        children: [
          TextButton(
            onPressed: () {
              print('点击了TextButton');
            }, 
            onLongPress: () {
              print('长按了TextButton');
            },
            child: Text('我是文本按钮'),
          ),
          ElevatedButton(
            onPressed: () {
              print('点击了ElevatedButton');
            }, 
            onLongPress: () {
              print('长按了ElevatedButton');
            },
            child: Text('我是ElevatedButton'),
          ),
          OutlineButton(
            onPressed: () {
              print('点击了OutlineButton');
            }, 
            onLongPress: () {
              print('长按了OutlineButton');
            },
            child: Text('我是OutlineButton'),
          ),
          ElevatedButton(
            onPressed: () {
              print('点击了OutlineButton');
            }, 
            onLongPress: () {
              print('长按了OutlineButton');
            },
            child: Text('自定义颜色'),
            style: ButtonStyle(
              textStyle: MaterialStateProperty.all(
                TextStyle(
                  fontSize: 30
                )
              ),
              foregroundColor: MaterialStateProperty.resolveWith(
                (states) {
                  if (states.contains(MaterialState.pressed)) {
                    // 按下按钮时的前景色
                    return Colors.red;
                  }
                  return Colors.red;
                } 
              ), 
              // 背景色
              backgroundColor: MaterialStateProperty.resolveWith(
                (states) {
                  if (states.contains(MaterialState.pressed)) {
                    // 按下按钮时的前景色
                    return Colors.green;
                  }
                  return Colors.white;
                } 
              ), 
              // 阴影
              shadowColor: MaterialStateProperty.all(Colors.yellow),
              // 阴影长度
              elevation: MaterialStateProperty.all(20),
              // 边框
              side:MaterialStateProperty.all(
                BorderSide(
                  color: Colors.green,
                  width: 3,
                )
              ),
              // 圆角
              shape: MaterialStateProperty.all(
                StadiumBorder(
                  side: BorderSide(
                    color: Colors.green,
                    width: 3,
                  )
                )
              ),
              // 设置按钮大小
              minimumSize: MaterialStateProperty.all(Size(150, 60)),
              // 设置水波纹的颜色
              overlayColor: MaterialStateProperty.all(Colors.purple),
            )
          ),
          // 主题按钮
          OutlinedButtonTheme(
            data: OutlinedButtonThemeData(
              style: ButtonStyle(
                overlayColor: MaterialStateProperty.all(Colors.purple),
              ),
            ),
            child: OutlinedButton(
              child: Text('我时主题按钮'),
              onPressed: () {
                print('点击了OutlinedButton');
              },
            ),
          ),
          // 图标按钮
          IconButton(
            icon:Icon(Icons.accessible_outlined),
            onPressed: () {
              print('点击了图标按钮');
            },
            color: Colors.red, // 颜色
            splashColor: Colors.lime, // 水波纹
            highlightColor: Colors.pink, // 高亮时候颜色
            tooltip: "怎么了", // 提示文字
          ),
          // 按钮组
          Container(
            color: Colors.pink,
            width: double.infinity,
            child: ButtonBar(
              children: [
                ElevatedButton(
                  onPressed:(){},
                  child: Text('按钮组1')
                ),
                 ElevatedButton(
                  onPressed:(){},
                  child: Text('按钮组2')
                )
              ]
            )
          ),
          // 回退按钮
          BackButton(
            color: Colors.red,
          ),
          // 关闭按钮
          CloseButton(),
        ]
      ),
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Button'),
      ),
      body: Body(),
      floatingActionButton: FloatingActionButton(
        onPressed: (){},
        child: Text('我是浮动按钮'),
      ),
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/302616
推荐阅读
相关标签
  

闽ICP备14008679号