0

我有一个带有 Text 小部件的按钮,在一个 showModalBottomSheet 内,它构建了另一个 showModalBottomSheet,其中包含一个 cupertinoPicker。如何返回 cupertinopicker 的值?它仅在我关闭第一个底页时返回值!而不是在 cupertinoPicker(bottomsheet) 被解雇时这样做!谢谢你们

这是我的鳕鱼

FlatButton(
                          color: Colors.white,
                          onPressed: () => showModalBottomSheet<dynamic>(
                              isScrollControlled: true,
                              context: context,
                              builder: (BuildContext context) => Container(
                                    child: Container(
                                      color: Colors.black45,

                                      child: SingleChildScrollView(
                                        child: Column(
                                          children: <Widget>[
                                            Row(
                                              children: <Widget>[
                                                Padding(
                                                  padding:
                                                      const EdgeInsets.only(
                                                          top: 50.0,
                                                          left: 120),
                                                  child: Text(
                                                    'Gå med',
                                                    style: TextStyle(
                                                        fontSize: 34,
                                                        color:
                                                            Colors.white),
                                                  ),
                                                ),
                                                SizedBox(
                                                  width: 40,
                                                ),
                                                FlatButton(
                                                  onPressed: () =>
                                                      Navigator.pop(
                                                          context),
                                                  child: Icon(
                                                    Icons.clear,
                                                    color: Colors.white,
                                                  ),
                                                )
                                              ],
                                            ),
                                            Form(
                                              key: _formKey,
                                              child: Column(
                                                mainAxisSize:
                                                    MainAxisSize.min,
                                                children: <Widget>[
                                                  Padding(
                                                    padding: EdgeInsets
                                                        .symmetric(
                                                            horizontal:
                                                                30.0,
                                                            vertical: 5.0),
                                                    child: TextFormField(
                                                      decoration: InputDecoration(
                                                          labelText:
                                                              'Restaurangens namn',
                                                          labelStyle: TextStyle(
                                                              fontWeight:
                                                                  FontWeight
                                                                      .bold)),
                                                      validator: (input) =>
                                                          input
                                                                  .trim()
                                                                  .isEmpty
                                                              ? 'please enter a valid name'
                                                              : null,
                                                      onSaved: (input) =>
                                                          _name = input,
                                                    ),
                                                  ),
                                                  Padding(
                                                    padding: EdgeInsets
                                                        .symmetric(
                                                            horizontal:
                                                                30.0,
                                                            vertical: 5.0),
                                                    child: TextFormField(
                                                      decoration: InputDecoration(
                                                          labelText:
                                                              'Email',
                                                          labelStyle: TextStyle(
                                                              fontWeight:
                                                                  FontWeight
                                                                      .bold)),
                                                      validator: (input) =>
                                                          !input.contains(
                                                                  '@')
                                                              ? 'please enter a valid email'
                                                              : null,
                                                      onSaved: (input) =>
                                                          _email = input,
                                                    ),
                                                  ),
                                                  Padding(
                                                    padding:
                                                        const EdgeInsets
                                                                .only(
                                                            left: 30.0,
                                                            right: 0.0),
                                                    child: Row(
                                                      children: <Widget>[
                                                        SizedBox(
                                                          width: 200,
                                                          child:
                                                              TextFormField(
                                                            decoration: InputDecoration(
                                                                labelText:
                                                                    'Telefon nummer',
                                                                labelStyle: TextStyle(
                                                                    fontWeight:
                                                                        FontWeight.bold)),
                                                            keyboardType:
                                                                TextInputType
                                                                    .phone,
                                                            validator: (input) => input
                                                                    .trim()
                                                                    .isEmpty
                                                                ? 'please enter a valid phone Number'
                                                                : null,
                                                            onChanged:
                                                                (input) =>
                                                                    tel =
                                                                        input,
                                                          ),
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                  Padding(
                                                    padding:
                                                        const EdgeInsets
                                                                .only(
                                                            left: 30.0,
                                                            right: 0.0),
                                                    child: Row(
                                                      children: <Widget>[
                                                        SizedBox(
                                                          width: 200,
                                                        ),
                                                        Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                      .only(
                                                                  top: 50.0,
                                                                  left: 0),
                                                          child:
                                                              CupertinoButton(
                                                            child: Text(citys[selectedcity]),
                                                            onPressed: () =>
                                                                showModalBottomSheet(
                                                              context:
                                                                  context,
                                                              builder: (BuildContext
                                                                      context) =>
                                                                  Container(
                                                                color: Colors
                                                                    .white,
                                                                height: 200,
                                                                width: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .width,
                                                                child:
                                                                    CupertinoPicker(
                                                                  onSelectedItemChanged:
                                                                      (int
                                                                          index) {
                                                                    setState(
                                                                        () {
                                                                      selectedcity = index;
                                                                      print(citys[selectedcity]);
                                                                    });
                                                                  },
                                                                  itemExtent:
                                                                      30,
                                                                  children: List<
                                                                          Widget>.generate(
                                                                      citys
                                                                          .length,
                                                                      (index) {
                                                                    return Center(
                                                                      child:
                                                                          Text(citys[index]),
                                                                    );
                                                                  }),
                                                                ),
                                                              ),
                                                            )
                                                        )
                                                        )],
                                                    ),
                                                  ),
                                                  Padding(
                                                    padding: EdgeInsets
                                                        .symmetric(
                                                            horizontal:
                                                                30.0,
                                                            vertical: 5.0),
                                                    child: TextFormField(
                                                      controller: _pass,
                                                      decoration: InputDecoration(
                                                          labelText:
                                                              'Lösenord',
                                                          labelStyle: TextStyle(
                                                              fontWeight:
                                                                  FontWeight
                                                                      .bold)),
                                                      validator: (input) =>
                                                          input.length < 6
                                                              ? 'must contain at least 6 characters'
                                                              : null,
                                                      onSaved: (input) =>
                                                          _password = input,
                                                      obscureText: true,
                                                    ),
                                                  ),
                                                  Padding(
                                                    padding: EdgeInsets
                                                        .symmetric(
                                                            horizontal:
                                                                30.0,
                                                            vertical: 0.0),
                                                    child: TextFormField(
                                                      controller:
                                                          _confirmPass,
                                                      decoration: InputDecoration(
                                                          labelText:
                                                              'Lösernord igen',
                                                          labelStyle: TextStyle(
                                                              fontWeight:
                                                                  FontWeight
                                                                      .bold)),
                                                      validator: (val) {
                                                        if (val !=
                                                            _pass.text) ;
                                                        return 'Not Match';
                                                      },
                                                      onSaved: (input) =>
                                                          _password = input,
                                                      obscureText: true,
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    height: 20,
                                                  ),
                                                  Container(
                                                    width: 140,
                                                    child: OutlineButton(
                                                      onPressed: _submit,
                                                      color: Colors.black,
                                                      padding:
                                                          EdgeInsets.all(
                                                              5.0),
                                                      child: Text(
                                                        'Gå med',
                                                        style: TextStyle(
                                                            color: Colors
                                                                .white,
                                                            fontSize: 18.0),
                                                      ),
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    width: 20,
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                          ),

                          child: Text(
                            ' Registera',
                            style: TextStyle(
                                color: Colors.black, fontSize: 18.0),
                          ))
4

1 回答 1

0

每个showModal...

  1. 是异步的并且有 Future 返回类型
  2. 将路由推送到 Navigator - 因此可以从此路由弹出结果Navigator.pop(ctx, result);

这里可用的例子

final dateTime = await showModalBottomSheet<DateTime>(builder: widget that does Navigator.pop(context, selectedDateTime) )
if (dateTime != null) { /* do what you need */ }
于 2020-06-20T17:15:28.823 回答