17

我是 Flutter 开发的新手,我试图在CupertinoPicker里面showCupertinoModalPopup点击触发CupertinoButton

选择 后Provinsi (Province),我可以Province通过再次单击按钮重新选择,但它应该是我选择的项目。

这是我的代码

showCupertinoModalPopup(
  context: context,
  builder: (_) {
    return new SizedBox(
    height: MediaQuery.of(context).size.height / 2,
    child: new CupertinoPicker(
      magnification: 1.2,
      useMagnifier: true,
      itemExtent: 32.0,
      onSelectedItemChanged: (i) => setState(() => _chosenProvince = listProvince[i]),
      children: r != null && listProvince != null ? listProvince.map((prov) {
      return new Padding(
        padding: const EdgeInsets.all(4.0),
        child: new Text(
        prov.name,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
          style: new TextStyle(
          fontSize: 20.0,
        ),
      ),
    );
  }).toList(): [],),);});

initialValue什么CupertinoPicker要设置的吗?

4

2 回答 2

31

您可以使用FixedExtentScrollController来设置initialValue。参考这个

于 2018-09-18T13:31:06.127 回答
21

如 Dinesh Balasubramanian 所述,您可以FixedExtentScrollController使用initialValue

它看起来像这样,例如从第四个元素开始:

showCupertinoModalPopup(
  context: context,
  builder: (_) {
    return new SizedBox(
    height: MediaQuery.of(context).size.height / 2,
    child: new CupertinoPicker(
      scrollController: FixedExtentScrollController(initialItem: 3),
      magnification: 1.2,
      useMagnifier: true,
      itemExtent: 32.0,
      onSelectedItemChanged: (i) => setState(() => _chosenProvince = listProvince[i]),
      children: r != null && listProvince != null ? listProvince.map((prov) {
      return new Padding(
        padding: const EdgeInsets.all(4.0),
        child: new Text(
        prov.name,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
          style: new TextStyle(
          fontSize: 20.0,
        ),
      ),
    );
  }).toList(): [],),);});
于 2020-01-09T19:06:04.657 回答