как вернуть значение из cupertinoPicker во флаттере - PullRequest
0 голосов
/ 20 июня 2020

У меня есть кнопка с виджетом Text в качестве дочернего элемента внутри showModalBottomSheet, который создает другой showModalBottomSheet, содержащий cupertinoPicker. как я могу вернуть значение cupertinopicker? он возвращает значение только тогда, когда я закрываю первый нижний лист !! вместо этого, когда cupertinoPicker (нижний лист) отклоняется !! спасибо всем

это моя треска

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),
                          ))

1 Ответ

0 голосов
/ 20 июня 2020

Каждый showModal ...

  1. является асинхронным c и имеет тип возврата 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 */ }
...