Flutter基礎(chǔ)篇之六-Buttons

?Flutter提供了一些便利的按鈕樣式

  • RaisedButton
    幾個(gè)常用的屬性:
    VoidCallback onPressed,// 點(diǎn)擊回調(diào)
    Color textColor,//文字顏色
    Color color,//按鈕顏色
    Color disabledColor,//不可點(diǎn)擊是按鈕的顏色
    Color highlightColor,//高亮的顏色
    EdgeInsetsGeometry padding,//內(nèi)邊距
    ShapeBorder shape,//設(shè)置按鈕的形狀
    Widget child,//按鈕里的內(nèi)容
RaisedButton(
      onPressed: () {},
      child: Text('不可點(diǎn)擊按鈕', style: TextStyle(fontSize: 16),),
    );
image.png

如果要設(shè)置按鈕為不可點(diǎn)擊狀態(tài)的話,需要把onPressed回調(diào)函數(shù)設(shè)置為null

RaisedButton(
      onPressed: null,
      child: Text('不可點(diǎn)擊按鈕', style: TextStyle(fontSize: 16),),
    );
image.png

Color背景色

RaisedButton(
      color: Colors.cyan,
      child: const Text(  '可點(diǎn)擊按鈕', style: TextStyle(fontSize: 16, color: Colors.black),
      ),
      onPressed: () {},
    );

image.png
  • IconButtons:可以快速創(chuàng)建一個(gè)左邊圖片右邊文字的按鈕
    image.png

    需要一個(gè)文字widget和IconWidget
class IconBtn extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton.icon(
      onPressed: () {
        print('點(diǎn)我了');
      },
      color: Colors.red,
      label: Text(
        '文字',
        style: TextStyle(fontSize: 16),
      ),
      icon: Icon(
        Icons.ac_unit,
        size: 30,
        color: Colors.cyan,
      ),
    );
  }
}

  • FloatingActionButton:可以快速創(chuàng)建圓形按鈕
    注:如果想要設(shè)置按鈕的大小,可以用Container包裝一下,設(shè)置寬高
class CircleBtn extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 70,
      height: 70,
      child: FloatingActionButton(
        onPressed: () {
          print('點(diǎn)擊了圓形按鈕');
        },
        child: Text(
          '圓形按鈕',
          style: TextStyle(fontSize: 14, color: Colors.red),
        ),
      ),
    );
  }
}
image.png
  • DropdownButton:類似于點(diǎn)擊點(diǎn)擊下拉菜單的控件
    需要繼承自StatefulWidget,因?yàn)橄吕藛吸c(diǎn)擊回調(diào)后可以得到狀態(tài)改變的回調(diào)
    DropdownButton的items屬性是菜單的集合,包含多個(gè)DropdownMenuItem,如果用戶點(diǎn)擊了item,返回value值
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;
          });
        }
    );
  }
}

具體代碼請(qǐng)看Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容