0

我现在被我正在尝试创建的小应用程序困住了。

当用户点击一个图标时,应该会得到一个带有 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')],
                    ),
                    ),
4

1 回答 1

0

试试这个!

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,
                                          width: 350.0,
                                          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])),
    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 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,
                              width: 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,
                                              fontSize: 22.0),
                                        ),
                                        Text(
                                          "Text 3",
                                          style: TextStyle(
                                              color: selected == 2
                                                  ? Colors.blue
                                                  : Colors.black,
                                              fontSize: 22.0),
                                        ),
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ),
                          );
                        },
                      );
                    },
                  );
                },
              ),
              Text('Add')
            ],
          ),
        ),
      ],
    ),
  ),
),

截屏

代码已经过测试,现在工作得很好!

于 2020-10-06T09:37:35.357 回答