首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >仅当我重新加载整个应用程序时,才会显示DropdownButtonFormField的选定值

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

Stack Overflow用户
提问于 2019-12-06 03:02:56
回答 1查看 331关注 0票数 0

我需要此警报对话框来显示

代码语言:javascript
运行
复制
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();
                    });
                  }
                },
              )
            ],
          ),
        );
      });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-06 03:29:39

您只需要构建一个扩展StatefulWidget的类,并将对话框中的内容移动到build方法中,

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
showDialog(
....
builder: (context)=> DialogContent()
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59201750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档