Flutter-警报对话框中的CupertinoPicker

问题描述

我现在正沉迷于要尝试创建的小应用程序。

用户点击图标时,应该获得一个带有2个按钮(“确定”和“取消”)的警报对话框,并且在警报框中,显示一个Cupertino Picker。在下面,您将找到代码。我收到此错误消息。

断言失败:第85行pos 15:“ children!= null”:不正确。

    class Engage extends StatefulWidget {
  Engage ({Key key}) : super(key:key);
  @override
  _EngageState createState() => _EngageState();
}

class _MyEngageState extends State<MyEngage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10.0),decoration: Boxdecoration(
          border: Border.all(color: Colors.grey[350])),child: Column(
       children: [
         Padding(
           padding: const EdgeInsets.all(3.0),child: Container(
              // margin: const EdgeInsets.all(30.0),padding: const EdgeInsets.all(10.0),decoration: Boxdecoration(
                   border: Border.all(color: Colors.grey[350])
               ),child : Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
                  GestureDetector(
                    child: Column(
                      children: [
                        IconButton(
                         splashColor: Colors.lightGreenAccent,icon : Image.asset('assets/icons/icon1',height: iconHeighEngage,),onpressed:(){
                              showDialog(
                              context: context,builder: (BuildContext context) {
                              return AlertDialog(
                                 title: Text('TEST'),content: Container(
                              height: 350,child: Column(
                              children: <Widget>[
                                       Cupertinopicker(),FlatButton(
                                      child: Text("OK"),onpressed: () {
                              Navigator.pop(context);
                              },)
                              ],));
                              });
                              },Text('TEST')],

解决方法

尝试一下!

Padding(
  padding: const EdgeInsets.all(3.0),child: Container(
    // margin: const EdgeInsets.all(30.0),padding: const EdgeInsets.all(10.0),decoration: BoxDecoration(
        border: Border.all(color: Colors.grey[350])),child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
        GestureDetector(
          child: Column(
            children: [
              IconButton(
                splashColor: Colors.lightGreenAccent,icon: Icon(
                  Icons.add,),onPressed: () {
                  showDialog(
                      context: context,builder: (BuildContext ctx) {
                        return AlertDialog(
                            title: Text('My Titile'),content: Container(
                              height: 350,width: 350.0,child: Column(
                                children: <Widget>[
                                  CupertinoPicker(
                                    itemExtent: 200.0,onSelectedItemChanged:
                                        (int value) {
                                      print("Test");
                                    },children: <Widget>[
                                      FlatButton(
                                        child: Container(
                                          color:
                                              Colors.orangeAccent,height: 160.0,child: Center(
                                              child: Text(
                                            "OK",style: TextStyle(
                                                fontSize: 20.0),)),onPressed: () {
                                          Navigator.pop(context);
                                        },)
                                    ],],);
                      },);
                },Text('TEST')
            ],)

注意:问题是,您没有传递 CupertinoPicker()

的参数

编辑:

首先初始化

int selected = 0;

然后:

Padding(
  padding: const EdgeInsets.all(3.0),child: Container(
    padding: const EdgeInsets.all(10.0),decoration:
        BoxDecoration(border: Border.all(color: Colors.grey[350])),onPressed: () {
                  showDialog(
                    context: context,builder: (BuildContext ctx) {
                      return StatefulBuilder(
                        builder: (context,setState) {
                          return AlertDialog(
                            backgroundColor: Colors.lightBlueAccent,title: Text(
                              'My Dialog',style: TextStyle(
                                color: Colors.white,fontSize: 30.0,fontWeight: FontWeight.bold,content: Container(
                              height: 350.0,child: Column(
                                children: <Widget>[
                                  Expanded(
                                    child: CupertinoPicker(
                                      useMagnifier: true,magnification: 1.5,backgroundColor: Colors.white,itemExtent: 40.0,onSelectedItemChanged: (int index) {
                                        print(selected);
                                        setState(() {
                                          selected = index;
                                        });
                                        print(selected);
                                      },children: <Widget>[
                                        Text(
                                          "Text 1",style: TextStyle(
                                              color: selected == 0
                                                  ? Colors.blue
                                                  : Colors.black,fontSize: 22.0),Text(
                                          "Text 2",style: TextStyle(
                                              color: selected == 1
                                                  ? Colors.blue
                                                  : Colors.black,Text(
                                          "Text 3",style: TextStyle(
                                              color: selected == 2
                                                  ? Colors.blue
                                                  : Colors.black,)
                                ],);
                        },);
                    },Text('Add')
            ],

Screenshot

该代码已经过测试,现在可以正常运行!

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...