设置选择的初始CupertinoPicker选择的索引

Amp*_*nda 4 flutter cupertinopicker

我是Flutter开发的新手,我正在尝试通过单击触发CupertinoPicker内部操作。showCupertinoModalPopupCupertinoButton

选择之后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(): [],),);});
Run Code Online (Sandbox Code Playgroud)

initialValue什么CupertinoPicker要设置的吗?

Jon*_*oni 17

正如 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(): [],),);});
Run Code Online (Sandbox Code Playgroud)


Din*_*ian 10

您可以FixedExtentScrollController用来设置initialValue。推荐这个