Flutter 学习之路 - Button 控件

实验一些常用的 Button 功能 (代码Github地址

Button 实验

实验了 Button 常用的一些功能

  • RaisedButton
  • FlatButton
  • 悬浮按钮 (FloatingActionButton)
  • 宽的悬浮按钮 (FloatingActionButton.extended)
  • 滚动条(Slider)
  • 复选框(Checkbox)
  • 选项按钮 (Radio Buttons)

代码地址

RaisedButton

用 child 的方式添加文字,用 onPressed 方法监听按钮行为。

// 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 方法监听按钮行为。
FlatButton 本身是透明且没有突出的,所以设置了 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 来设置 button 上显示文字或者 icon。

同时这里有个要注意的点,就是 material design 建议每个屏幕只有一个 FloatingActionButton,所以如果一定要两个(比如像我这样写demo),就要把这个 heroTag 设置为 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();
          });
        }
    )
),

复选框(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 的值设置为 null, 这个 Radio 就是不可选的,会显示一个灰色的状态。

// 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)
      ],
    )
),

遇到的问题

感觉对于控件,各个参数主要是看官网文档,然后自己改改, 所以还是记录一下遇到的问题。

问题一: 当多个 FloatingActionButton 的时候程序运行会黑屏

解决方案:把 heroTag 设置为 null

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

看了官网的说明,大意是 material design 建议每个屏幕只有一个 FloatingActionButton,所以如果一定要两个(比如像我这样写demo),就把这个 heroTag 设置为 null。

官网对于 heroTag 的说明: link

Flutter 学习之路 Github 地址

https://github.com/draftbk/flutter_road

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容