Flutter 學(xué)習(xí)之路 - Button 控件

實驗一些常用的 Button 功能 (代碼Github地址

Button 實驗

實驗了 Button 常用的一些功能

  • RaisedButton
  • FlatButton
  • 懸浮按鈕 (FloatingActionButton)
  • 寬的懸浮按鈕 (FloatingActionButton.extended)
  • 滾動條(Slider)
  • 復(fù)選框(Checkbox)
  • 選項按鈕 (Radio Buttons)

代碼地址

RaisedButton

用 child 的方式添加文字,用 onPressed 方法監(jiān)聽按鈕行為。

// RaisedButton
Padding(
  padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
  child: new RaisedButton(
    onPressed: () {print('button click');},
    child: new Text("RaisedButton"),
  ),
),

FlatButton

和 RaisedButton 一樣用 child 的方式添加文字,用 onPressed 方法監(jiān)聽按鈕行為。
FlatButton 本身是透明且沒有突出的,所以設(shè)置了 color 使它更明顯

// FlatButton
Padding(
  padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
  child: new FlatButton(
      onPressed: () {print('button click');},
      child: new Text("FlatButton"),
      color: Color(0xFFe16552),
  ),
),

懸浮按鈕 (FloatingActionButton)

懸浮按鈕,可以通過改變 child 來設(shè)置 button 上顯示文字或者 icon。

同時這里有個要注意的點,就是 material design 建議每個屏幕只有一個 FloatingActionButton,所以如果一定要兩個(比如像我這樣寫demo),就要把這個 heroTag 設(shè)置為 null。

 //FloatingActionButton 多個FloatingActionButton的時候會報錯,要加 heroTag: null
 Padding(
   padding: const EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
   child: Row(
     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
     children: <Widget>[
       // FloatingActionButton
       new FloatingActionButton(
           onPressed: () {print('button click');},
           foregroundColor: Colors.white,
           backgroundColor: Color(0xFFf19670),
           child: new Icon(Icons.add),
           heroTag: null,
       ),
       // FloatingActionButton
       new FloatingActionButton(
           onPressed: () {print('button click');},
           foregroundColor: Colors.white,
           backgroundColor: Color(0xFFf19670),
           child: new Text("文字"),
           heroTag: null,
       ),
     ],
   ),
 ),

寬的懸浮按鈕 (FloatingActionButton.extended)

可以看我上面的動圖,有這個按鈕的效果,感覺還挺棒的哈哈。

// FloatingActionButton.extended
Padding(
  padding: const EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
  child: new FloatingActionButton.extended(
    onPressed: () {
      print('button click');
    },
    foregroundColor: Colors.white,
    backgroundColor: Colors.amber,
    icon: new Icon(Icons.flag,color: Colors.red,),
    label: new Text('FloatingActionButton.extended', maxLines: 1),
  )
),

滾動條(Slider)

Padding(
    padding: const EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
    child: new Slider(
        value: mCurrentValue,
        min: 1.0,
        max: 10.0,
        onChanged: (e) {
          setState(() {
            //四舍五入的雙精度值
            mCurrentValue = e.roundToDouble();
          });
        }
    )
),

復(fù)選框(Checkbox)

Padding(
    padding: const EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
    child: new Checkbox(
        value: isChecked,
        onChanged: (bool){
          setState(() {
            isChecked=bool;
          });
    }, activeColor: Colors.blue,
    )
),

選項按鈕 (Radio Buttons)

這里如果把新建的 Radio 的值設(shè)置為 null, 這個 Radio 就是不可選的,會顯示一個灰色的狀態(tài)。

// Radio
Padding(
    padding: const EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
    child: new ButtonBar(//ButtonBar:水平排列按鈕
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        //value和groupValue值一樣的話,則按鈕選中
        new Radio(value: 1,
            groupValue: groupValue1,
            onChanged: (int e) => updateGroupValue(e)),
        new Radio(value: 2,
            groupValue: groupValue1,
            onChanged: (int e) => updateGroupValue(e)),
        new Radio(value: 3,
            groupValue: groupValue1,
            onChanged: (int e) => updateGroupValue(e)),
        new Radio(value: 4,
            groupValue: groupValue1,
            onChanged: (int e) => updateGroupValue(e)),
        //value 值為 null 則表示按鈕不可用
        new Radio(value: null, groupValue: null, onChanged: null)
      ],
    )
),

遇到的問題

感覺對于控件,各個參數(shù)主要是看官網(wǎng)文檔,然后自己改改, 所以還是記錄一下遇到的問題。

問題一: 當(dāng)多個 FloatingActionButton 的時候程序運行會黑屏

解決方案:把 heroTag 設(shè)置為 null

new FloatingActionButton(
    onPressed: () {print('button click');},
    foregroundColor: Colors.white,
    backgroundColor: Color(0xFFf19670),
    child: new Icon(Icons.add),
    heroTag: null,
),

看了官網(wǎng)的說明,大意是 material design 建議每個屏幕只有一個 FloatingActionButton,所以如果一定要兩個(比如像我這樣寫demo),就把這個 heroTag 設(shè)置為 null。

官網(wǎng)對于 heroTag 的說明: link

Flutter 學(xué)習(xí)之路 Github 地址

https://github.com/draftbk/flutter_road

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

相關(guān)閱讀更多精彩內(nèi)容

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,888評論 2 45
  • Flutter是Google開發(fā)的一套全新的跨平臺、開源UI框架(本質(zhì)上就是sdk)。 支持iOS、Android...
    HarveyLegend閱讀 8,587評論 1 42
  • UI Awesome-MaterialDesign – MaterialDesignCenter改名為Awesom...
    程序亦非猿580230閱讀 5,987評論 2 88
  • 什么是好的產(chǎn)品 創(chuàng)造了長期的用戶價值。形成閉環(huán)的商業(yè)模式或生態(tài)。核心功能體驗具有明顯優(yōu)勢。 產(chǎn)品方法論 1. 基于...
    f76be33b5ca1閱讀 398評論 0 0

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