Flutter:如何将新项目添加到CupertinoPicker

时间:2020-03-28 11:22:29

标签: flutter dart

我有来自JSON API的列表数据,我想在列表之前添加新的项目,例如“选择项目”。我如何在CupertinoPicker上执行此操作 这是我的代码

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

1 个答案:

答案 0 :(得分:0)

您可以像这样使用一列来包装您的选择元素,

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,
    );