第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Flutter 14: 圖解 ListView 不同樣式 item 及 Widget 顯隱性

標(biāo)簽:
Android

  一年一度的谷歌大会又开始了,谷歌对 Flutter 的投入力度又加大了,所以更得好好学 Flutter 了。小菜在做新闻列表方面的 Demo 时,想到会在列表中展示多种不同 item 样式,今天特意借助上一篇关于 ListView 的小例子 稍微调整一下,测试 ListView 中多种 item 样式展示方式。

webp

尝试如下

      小菜按照 Android 的想法,应该会对 Android 列表的 ViewHolder 中进行状态判断;类似的小菜想在 itemBuilder 中对布局样式进行判断,想法是可以的,只是在实践中遇到两个小小的问题,分析一下,如下:

Widget buildListData(
    BuildContext context, String strItem, Icon iconItem, int type) {
    Widget widget;    switch (type) {        case 1: 
            ...            break;        case 2: 
            ...            break;
    }    return widget;
}

实践注意

一.  item 类型显示不完整
  1. 小菜首先往 buildListData 中传入 item 样式类型,针对样式类型判断 Widget 样式,当然类型传入方式多种多样按实际情况而定;

List<int> stateItems = <int>[  1, 1, 2, 2, 0, 1, 1, 1, 0, 1, 2, 2, 1, 2,
];
  1. 小菜编辑了一个 stateItems 有 0,1,2 三种样式类型,但是在 buildListData 中只判断了 1 和 2 两种,小菜以为会隐藏掉当前 item,可结果出乎意料,在 0 及以后的 item 全部不显示,完全中断;如图:

webp

  1. 添加相应的判断处理之后即正常展示,如图:

webp

二.  GestureDetector 手势方法注意

      默认很多 Widget 没有 onTab() 方法,但是可以用 GestureDetector 来进行手势处理;小菜建议使用 GestureDetector 时针对具体的 Widget,例如对 item 进行点击操作时,对整个 item 外添加 GestureDetector,小菜当时糊涂把 GestureDetector 添加错 Widget 以为使用方式有问题,请各位注意。

webp

webp

三.  Widget 显隐性

      小菜在实际测试的过程中需要用到【Widget 显隐性】,小菜想 Flutter 最大的特点就是一切都是 Widget,同时 Widget 不可为 null(错误:Widget wi = null),于是小菜把需要隐藏的 Widget 设置宽和高为 0.0,不知道各位有没有更好的实现方式。

Widget wi;if ('图标 -> pages' == strItem) {
  wi = new Container(height: 0.0, width: 0.0);
} else {
  wi = new Text(
    strItem,
    style: new TextStyle(color: Colors.blueAccent, fontSize: 18.0),
  );
}

webp

核心源码

List<int> stateItems = <int>[  1, 1, 2, 2, 0, 1, 1, 1, 0, 1, 2, 2, 1, 2,
];
List<String> strItems = <String>[  '图标 -> keyboard', '图标 -> print', '图标 -> router',  '图标 -> pages', '图标 -> zoom_out_map', '图标 -> zoom_out',  '图标 -> youtube_searched_for', '图标 -> wifi_tethering', '图标 -> wifi_lock',  '图标 -> widgets', '图标 -> weekend', '图标 -> web', '图标 -> accessible', '图标 -> ac_unit',
];
List<Icon> iconItems = <Icon>[  new Icon(Icons.keyboard), new Icon(Icons.print), new Icon(Icons.router),  new Icon(Icons.pages), new Icon(Icons.zoom_out_map), new Icon(Icons.zoom_out),  new Icon(Icons.youtube_searched_for), new Icon(Icons.wifi_tethering), new Icon(Icons.wifi_lock),  new Icon(Icons.widgets), new Icon(Icons.weekend), new Icon(Icons.web),  new Icon(Icons.accessible), new Icon(Icons.ac_unit),
];Widget buildListData(
    BuildContext context, String strItem, Icon iconItem, int type) {
  Widget widget;  switch (type) {    case 1:
      widget = new ListTile(
        isThreeLine: false,
        leading: iconItem,
        title: new Text(strItem),
        trailing: new Icon(Icons.keyboard_arrow_right),
        onTap: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {              return new AlertDialog(
                title: new Text(                  'ListViewDemo',
                  style: new TextStyle(
                    color: Colors.black54,
                    fontSize: 18.0,
                  ),
                ),
                content: new Text('您选择的item内容为:$strItem,item 状态为 1'),
              );
            },
          );
        },
      );      break;    case 2:
      Widget wi;      if ('图标 -> pages' == strItem) {
        wi = new Container(height: 0.0, width: 0.0);
      } else {
        wi = new Text(
          strItem,
          style: new TextStyle(color: Colors.blueAccent, fontSize: 18.0),
        );
      }
      widget = new GestureDetector(
        child: new Column(
          children: <Widget>[
            iconItem,
            wi,
          ],
        ),
        onTap: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {              return new AlertDialog(
                title: new Text(                  'ListViewDemo',
                  style: new TextStyle(
                    color: Colors.black54,
                    fontSize: 18.0,
                  ),
                ),
                content: new Text('您选择的item内容为:$strItem,item 状态为 2'),
              );
            },
          );
        },
      );      break;    default:
      widget = new Container(
        height: 50.0,
        color: Colors.greenAccent,
        child: new Padding(
          padding: new EdgeInsets.all(12.0),
          child: new GestureDetector(
            child: new Text('我是状态为0的item'),
            onTap: () {
              showDialog(
                context: context,
                builder: (BuildContext context) {                  return new AlertDialog(
                    title: new Text(                      'ListViewDemo',
                      style: new TextStyle(
                        color: Colors.black54,
                        fontSize: 18.0,
                      ),
                    ),
                    content: new Text('哈哈哈!当前 item 状态为 0'),
                  );
                },
              );
            },
          ),
        ),
      );      break;
  }  return widget;
}



作者:阿策神奇


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
移動(dòng)開(kāi)發(fā)工程師
手記
粉絲
168
獲贊與收藏
165

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消