首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter :如何向CupertinoPicker添加新项目

Flutter :如何向CupertinoPicker添加新项目
EN

Stack Overflow用户
提问于 2020-03-28 19:22:29
回答 1查看 692关注 0票数 0

我有来自JSON API的列表数据,我想在列表前添加像"Select item“这样的新项目。我如何在CupertinoPicker上做到这一点这是我的代码

代码语言:javascript
复制
CupertinoPicker( 
                                children:  data.length != 0
                                  ? new List<Widget>.generate(data.length,
                                  (int index) {
                                return new Center(
                                  child: new Text(
                                    data[index]['city'],
                                    style: TextStyle(
                                      fontSize: 22,
                                    ),
                                  ),
                                );
                              })
                              : <Widget>[Text("---")] ,
                              onSelectedItemChanged: (int index) {
                                setState(() {
                                  _myLocation = data[index]['city'];
                                  getSublocation();
                                  _mySublocation = null;
                                });
                              },
                              itemExtent: 32.0,
                            )

这张图片解释了我想要什么,enter image description here

EN

回答 1

Stack Overflow用户

发布于 2020-03-29 05:15:58

您可以使用列来包装您的select元素,如下所示:

代码语言:javascript
复制
CupertinoPicker(
      children: <Widget>[
        SelectWidget(),// replace this with the widget you want <-------------------------
        data.length == 0
            ? 
            Expanded(
                child: ListView.builder(
                  itemCount: data.length,
                  itemBuilder: (context, int index) {
                    return Container(
                      child: Center(
                        child: new Text(
                          data[index]['city'],
                          style: TextStyle(
                            fontSize: 22,
                          ),
                        ),
                      ),
                    );
                  },
                ),
              )
            : Text("---")
      ],
      onSelectedItemChanged: (int index) {
        setState(() {
          _myLocation = data[index]['city'];
          getSublocation();
          _mySublocation = null;
        });
      },
      itemExtent: 32.0,
    );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60900479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档