仅当我重新加载整个应用程序时,DropdownButtonFormField的选定值才会显示

时间:2019-12-05 19:02:56

标签: flutter flutter-layout

我需要此警报对话框才能在

中显示所选项目
Widget _changePrPar() {
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return SingleChildScrollView(
          child: AlertDialog(
            key: alertDialogKey,
            title: ListTile(
              leading: Icon(Icons.account_circle),
              title: Text("Mon premier Dialogue"),
            ),
            content: Form(
              key: formKey,
              child: Column(
                children: <Widget>[
                  TextFormField(
                    decoration: InputDecoration(
                        labelText: "Nom et Prenom",
                        hintText: "Ex. Ndoume Desmon"),
                    validator: (val) =>
                        val.isEmpty ? "The Name cannot be empty" : null,
                    onSaved: (val) => nomP = val,
                  ),

                  TextFormField(
                      decoration: InputDecoration(
                          labelText: "User Name",
                          hintText: "Ex. desmon215"),
                      validator: (val) => val.isEmpty
                          ? "The user name cannot be empty"
                          : null,
                      onSaved: (val) => pseudo = val),

                  //My Problems start here, the value selected does not appear,
                  ListTile(
                    title: Text(
                      "Gender",
                      style: TextStyle(color: Colors.grey),
                    ),
                    contentPadding: EdgeInsets.symmetric(vertical: 9),
                    subtitle: DropdownButtonFormField<String>(
                      items: Sexe.map((l) => DropdownMenuItem(
                            child: Text(l),
                            value: l,
                          )).toList(),
                      value: sexyVal,
                      onSaved: (val) => sexyVal = val,
                      onChanged: (v) {
                        setState(() {
                          sexyVal = v;
                        });
                      },
                    ),
                  ),
                  TextFormField(
                      decoration: InputDecoration(
                          labelText: "Telephone",
                          hintText: "Ex. 654521455"),
                      validator: (val) =>
                          val.length > 9 && val.startsWith("6")
                              ? null
                              : "The phoe number is not valid..",
                      onSaved: (val) => tel = val),
                  TextFormField(
                      decoration: InputDecoration(
                          labelText: "Email",
                          hintText: "Ex. abc@xyzer.com"),
                      validator: (val) =>
                          val.contains("@") && val.endsWith(".com")
                              ? null
                              : "Invalid e-mail",
                      onSaved: (val) => mailP = val),
                  TextFormField(
                      controller: passCtl,
                      decoration: InputDecoration(
                        suffix: IconButton(
                          icon: Icon(
                            Icons.cancel,
                            color: Colors.grey,
                          ),
                          onPressed: () {
                            passCtl.clear();
                          },
                        ),
                        labelText: "PassWord",
                        hintText: "8 caracters minimum..",
                      ),
                      maxLengthEnforced: true,
                      maxLength: 10,
                      obscureText: true,
                      validator: (val) =>
                          val.length < 8 ? "Password too short" : null,
                      onSaved: (val) => pass = val),
                  TextFormField(
                    controller: dateCtl,
                    decoration: InputDecoration(
                      suffix: IconButton(
                        icon: Icon(
                          Icons.cancel,
                        ),
                        onPressed: () {
                          dateCtl.clear();
                        },
                      ),
                      labelText: "Date of birth",
                      hintText: "Ex. Insert your dob",
                    ),
                    onTap: () async {
                      DateTime date = DateTime(1900);
                      FocusScope.of(context).requestFocus(new FocusNode());
                      date = await showDatePicker(
                          context: context,
                          initialDate: DateTime.now(),
                          firstDate: DateTime(1900),
                          lastDate: DateTime(2100));

                      // pour calculer l'age a partir de la date de naissance
                      age = DateTime.now().difference(date).inDays;
                      Age = (age / 360).floor();
                      _date = date.toIso8601String();
                      dateCtl.text = _date.substring(0, 10);
                    },
                    onSaved: (val) => _date = Age.toString(),
                  ),
                ],
              ),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text("Cancel"),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              FlatButton(
                child: Text("Submit"),
                onPressed: () {
                  final form = formKey.currentState;
                  if (form.validate()) {
                    setState(() {
                      form.save();
                      Navigator.of(context).pop();
                    });
                  }
                },
              )
            ],
          ),
        );
      });
}

1 个答案:

答案 0 :(得分:0)

您只需要构建一个扩展input { tcp { port => 4560 codec => json_lines } } 的类,并将对话框的内容移动到build方法中,

StatefulWidget

现在将其返回到class DialogContent extends StatefulWidget { @override _DialogContentState createState() => _DialogContentState(); } class _DialogContentState extends State<DialogContent> { @override Widget build(BuildContext context) { return SingleChildScrollView( child: AlertDialog( key: alertDialogKey, title: ListTile( leading: Icon(Icons.account_circle), title: Text("Mon premier Dialogue"), ), content: Form( key: formKey, child: Column( children: <Widget>[ TextFormField( decoration: InputDecoration( labelText: "Nom et Prenom", hintText: "Ex. Ndoume Desmon"), validator: (val) => val.isEmpty ? "The Name cannot be empty" : null, onSaved: (val) => nomP = val, ), TextFormField( decoration: InputDecoration( labelText: "User Name", hintText: "Ex. desmon215"), validator: (val) => val.isEmpty ? "The user name cannot be empty" : null, onSaved: (val) => pseudo = val), //My Problems start here, the value selected does not appear, ListTile( title: Text( "Gender", style: TextStyle(color: Colors.grey), ), contentPadding: EdgeInsets.symmetric(vertical: 9), subtitle: DropdownButtonFormField<String>( items: Sexe.map((l) => DropdownMenuItem( child: Text(l), value: l, )).toList(), value: sexyVal, onSaved: (val) => sexyVal = val, onChanged: (v) { setState(() { sexyVal = v; }); }, ), ), TextFormField( decoration: InputDecoration( labelText: "Telephone", hintText: "Ex. 654521455"), validator: (val) => val.length > 9 && val.startsWith("6") ? null : "The phoe number is not valid..", onSaved: (val) => tel = val), TextFormField( decoration: InputDecoration( labelText: "Email", hintText: "Ex. abc@xyzer.com"), validator: (val) => val.contains("@") && val.endsWith(".com") ? null : "Invalid e-mail", onSaved: (val) => mailP = val), TextFormField( controller: passCtl, decoration: InputDecoration( suffix: IconButton( icon: Icon( Icons.cancel, color: Colors.grey, ), onPressed: () { passCtl.clear(); }, ), labelText: "PassWord", hintText: "8 caracters minimum..", ), maxLengthEnforced: true, maxLength: 10, obscureText: true, validator: (val) => val.length < 8 ? "Password too short" : null, onSaved: (val) => pass = val), TextFormField( controller: dateCtl, decoration: InputDecoration( suffix: IconButton( icon: Icon( Icons.cancel, ), onPressed: () { dateCtl.clear(); }, ), labelText: "Date of birth", hintText: "Ex. Insert your dob", ), onTap: () async { DateTime date = DateTime(1900); FocusScope.of(context).requestFocus(new FocusNode()); date = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime(1900), lastDate: DateTime(2100)); // pour calculer l'age a partir de la date de naissance age = DateTime.now().difference(date).inDays; Age = (age / 360).floor(); _date = date.toIso8601String(); dateCtl.text = _date.substring(0, 10); }, onSaved: (val) => _date = Age.toString(), ), ], ), ), actions: <Widget>[ FlatButton( child: Text("Cancel"), onPressed: () { Navigator.of(context).pop(); }, ), FlatButton( child: Text("Submit"), onPressed: () { final form = formKey.currentState; if (form.validate()) { setState(() { form.save(); Navigator.of(context).pop(); }); } }, ) ], ), ); }); } }

showDialog