Flutter实战专题

Flutter基础篇之六-Buttons

2020-10-11  本文已影响0人  Michale_Zuo

 Flutter提供了一些便利的按钮样式

    VoidCallback onPressed,// 点击回调
    Color textColor,//文字颜色
    Color color,//按钮颜色
    Color disabledColor,//不可点击是按钮的颜色
    Color highlightColor,//高亮的颜色
    EdgeInsetsGeometry padding,//内边距
    ShapeBorder shape,//设置按钮的形状
    Widget child,//按钮里的内容
RaisedButton(
      onPressed: () {},
      child: Text('不可点击按钮', style: TextStyle(fontSize: 16),),
    );
image.png

如果要设置按钮为不可点击状态的话,需要把onPressed回调函数设置为null

RaisedButton(
      onPressed: null,
      child: Text('不可点击按钮', style: TextStyle(fontSize: 16),),
    );
image.png

Color背景色

RaisedButton(
      color: Colors.cyan,
      child: const Text(  '可点击按钮', style: TextStyle(fontSize: 16, color: Colors.black),
      ),
      onPressed: () {},
    );

image.png
class IconBtn extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton.icon(
      onPressed: () {
        print('点我了');
      },
      color: Colors.red,
      label: Text(
        '文字',
        style: TextStyle(fontSize: 16),
      ),
      icon: Icon(
        Icons.ac_unit,
        size: 30,
        color: Colors.cyan,
      ),
    );
  }
}

class CircleBtn extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 70,
      height: 70,
      child: FloatingActionButton(
        onPressed: () {
          print('点击了圆形按钮');
        },
        child: Text(
          '圆形按钮',
          style: TextStyle(fontSize: 14, color: Colors.red),
        ),
      ),
    );
  }
}
image.png
class DropDownBtn extends StatefulWidget {
  @override
  _DropDownBtnState createState() => _DropDownBtnState();
}

class _DropDownBtnState extends State<DropDownBtn> {
  String dropdownValue = '扫一扫';
  @override
  Widget build(BuildContext context) {
    return DropdownButton(
      value:dropdownValue ,
        items: _downTitles
            .map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(value: value, child: Text(value));
        }).toList(),
        onChanged:(String newValue) {
          setState(() {
            dropdownValue = newValue;
          });
        }
    );
  }
}

具体代码请看Demo地址

上一篇下一篇

猜你喜欢

热点阅读