当按下某个按钮时,我想显示 CupertinoPicker。
但是我不知道我应该在哪里实现 CupertinoPicker。

以下代码是代码示例,我不知道我写在哪里调用。

showCupertinoModalPopup<String>(
  context: context,
  builder:(BuildContext context){
    return _buildBottomPicker(CupertinoPicker(
      onSelectedItemChanged: (value){
        setState((){
          selectedValue = value;
        });
      },
      itemExtent: 32.0,
      children: const[
        Text('Item01'),
        Text('Item02'),
        Text('Item03'),
      ],
    ));
  },
);

最佳答案

这是按下按钮时启动cupertino选择器的完整可行演示。希望这能解决您的疑问。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WeightSelect(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class WeightSelect extends StatefulWidget {
  @override
  _WeightSelectState createState() => _WeightSelectState();
}

class _WeightSelectState extends State<WeightSelect> {
  int selectedValue;

  showPicker() {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return CupertinoPicker(
            backgroundColor: Colors.white,
            onSelectedItemChanged: (value) {
              setState(() {
                selectedValue = value;
              });
            },
            itemExtent: 32.0,
            children: const [
              Text('Item01'),
              Text('Item02'),
              Text('Item03'),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Cupertino picker demo"),
      ),
      body: Column(
        children: <Widget>[
          const SizedBox(height: 10.0),
          Text("Selected: $selectedValue"),
          const SizedBox(height: 10.0),
          Center(
            child: RaisedButton(
              onPressed: showPicker,
              child: Text("Show picker"),
            ),
          ),
        ],
      ),
    );
  }
}

关于flutter - 如何调用 CupertinoPicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59558536/

10-10 05:22