簡體   English   中英

Flutter - 警報對話框中的 CupertinoPicker

[英]Flutter - CupertinoPicker in an alert dialog

我現在被我正在嘗試創建的小應用程序困住了。

當用戶點擊一個圖標時,應該會得到一個帶有 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,
                                          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')
            ],
          ),
        ),
      ],
    ),
  ),
),

截屏

代碼已經過測試,現在工作得很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM