亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

?Flutter | 1.9 全新組件 ToggleButtons

標簽:
WebApp

Flutter 官宣发布了 1.9 正式版。

随之而来的有一些全新的组件和对于 web 的支持等等。

那我们今天就来看一下这其中的一个组件 --「ToggleButtons」。


ToggleButtons

首先按照惯例,看看官方对于这个组件是怎么说的:

Creates a horizontal set of toggle buttons.

It displays its widgets provided in a [List] of [children] horizontally.

创建一组水平的切换按钮。

它水平的显示 children 列表中提供的小部件。

其实这段文本是在源码中翻出来的,现在在网上搜 「ToggleButtons」 还是搜不出来官方文档的。

构造函数

还是按照惯例看一下构造函数:


const ToggleButtons({

  Key key,

  @required this.children,

  @required this.isSelected,

  this.onPressed,

  this.color,

  this.selectedColor,

  this.disabledColor,

  this.fillColor,

  this.focusColor,

  this.highlightColor,

  this.hoverColor,

  this.splashColor,

  this.focusNodes,

  this.renderBorder = true,

  this.borderColor,

  this.selectedBorderColor,

  this.disabledBorderColor,

  this.borderRadius,

  this.borderWidth,

}) :

assert(children != null),

assert(isSelected != null),

assert(children.length == isSelected.length),

super(key: key);


解释一下每个参数:

1.children:不多介绍了,一个 Widget 的集合2.isSelected:List<bool>,每个切换按钮相应的状态,true 为选中,该字段的长度必须和 children 的长度一致3.onPressed:切换按钮的点击事件,如果为 null, 则该控件的状态为 disable4.color:Text / Icon 状态为已启用并且未选中时的颜色5.selectedColor:不用多说,选中时的颜色6.disabledColor:未启用时的颜色7.fillColor:选中按钮的背景颜色8.focusColor:当按钮中具有输入焦点时填充的颜色9.highlightColor:点击时的颜色10.hoverColor:当按钮上有指针悬停时用于填充按钮的颜色11.splashColor:点击后的颜色12.focusNodes:每一个按钮的焦点13.renderBorder:是否在每个切换按钮周围呈现边框14.borderColor:边框颜色15.selectedBorderColor:选中的边框颜色16.disabledBorderColor:不可用时边框颜色17.borderRadius:边框半径18.borderWidth:边框宽度

这参数太多了,但是其实也没什么难度。

第一个示例

在组件介绍的下面有很多的代码,我们一一来看。

先看第一个:

Here is an implementation that allows for multiple buttons to be simultaneously selected, while requiring none of the buttons to be selected.

这里有一个实现,它允许同时选择多个按钮,而不需要选择任何一个按钮。

看一下代码:


List<bool> isSelected = [true, false, false];


ToggleButtons(

  children: <Widget>[

    Icon(Icons.ac_unit),

    Icon(Icons.call),

    Icon(Icons.cake),

  ],

  onPressed: (int index) {

    setState(() {

      isSelected[index] = !isSelected[index];

    });

  },

  isSelected: isSelected,

),


运行效果如下:

https://img1.sycdn.imooc.com//5da197cb000113a403020592.jpg

其中最重要的代码就是:

1.添加了 「onPress」方法2.在「onPress」回调中刷新每一个切换按钮的值

第二个示例

再来看第二个示例:

Here is an implementation that requires mutually exclusive selection, but allows for none of the buttons to be selected.

这是一个互斥选择的实现,但允许一个都不选择。

代码如下:


ToggleButtons(

  children: <Widget>[

    Icon(Icons.ac_unit),

    Icon(Icons.call),

    Icon(Icons.cake),

  ],

  onPressed: (int index) {

    setState(() {

      for (int buttonIndex = 0; buttonIndex < isSelected.length; buttonIndex++) {

        if (buttonIndex == index) {

          isSelected[buttonIndex] = !isSelected[buttonIndex];

        } else {

          isSelected[buttonIndex] = false;

        }

      }

    });

  },

  isSelected: isSelected,

),


效果如下:

https://img1.sycdn.imooc.com//5da197ce0001a35903010343.jpg

该示例展示了只能选择一个、并且可以不选 demo,主要逻辑如下:

循环所有的切换按钮的值,如果是当前 index,则置反,如果不是,则置为 false。

第三个示例

最后一个示例,

代码如下:


ToggleButtons(

  children: <Widget>[

    Icon(Icons.ac_unit),

    Icon(Icons.call),

    Icon(Icons.cake),

  ],

  onPressed: (int index) {

    int count = 0;

    isSelected.forEach((bool val) {

      if (val) count++;

    });


    if (isSelected[index] && count < 2)

      return;


    setState(() {

      isSelected[index] = !isSelected[index];

    });

  },

  isSelected: isSelected,

),


效果如下:

https://img1.sycdn.imooc.com//5da197d00001825202010576.jpg

逻辑其实都在 「onPressed」中,导致的结果不一样。

最后

这里我没有改变外观之类的,只是借用了官方的 demo,其实想改变外观之类的,回头看看构造函数,我想了一下,基本能用到的都提供了。


References

[1] Bilibili - 2019Google上海开发者大会 9月11日分会场4!!!!!!: https://www.bilibili.com/video/av67381317?from=search&seid=13299433060914177168
[2] Bilibili - 2019Google上海开发者大会 9月11日分会场4!!!!!!: https://www.bilibili.com/video/av67381317?from=search&seid=13299433060914177168
[3] Bilibili - 2019Google上海开发者大会 9月11日分会场4!!!!!!: https://www.bilibili.com/video/av67381317?from=search&seid=13299433060914177168
[4] Bilibili - 2019Google上海开发者大会 9月11日分会场4!!!!!!: https://www.bilibili.com/video/av67381317?from=search&seid=13299433060914177168
[5] Bilibili - 2019Google上海开发者大会 9月11日分会场4!!!!!!: https://www.bilibili.com/video/av67381317?from=search&seid=13299433060914177168
[6] Bilibili - 2019Google上海开发者大会 9月11日分会场4!!!!!!: https://www.bilibili.com/video/av67381317?from=search&seid=13299433060914177168


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
移動開發工程師
手記
粉絲
30
獲贊與收藏
45

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消