【发布时间】:2021-04-30 03:45:39
【问题描述】:
我对 Flutter 非常陌生。
是否可以在单击按钮时在同一屏幕上构建小部件?
屏幕截图
在这里,我想要实现的是:单击其中一个按钮 - 说“使用电子邮件”代替“选择一个选项”,应该会出现一个完整的登录表单。但是在选择另一个选项时,该表单应该被另一个替换。
类似于在android中加载片段的东西。
代码:[这正是我尝试过的,可能是非常错误的。它只是点击按钮什么都不做]
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
MaterialButton(
child: Text("Using Email"),
onPressed: () {
_count = 1;
createwidget();
print(_count);
}),
MaterialButton(
onPressed: () {
_usingPhone();
},
child: Text("Using Phone"),
),
MaterialButton(child: Text("Go back to home"), onPressed: () {}),
createwidget(),//this is where i want the forms
],
),
),
),
);
}
Widget createwidget() {
print("inside create");
if(_count==0) {
print(_count);
return new Text("Select an option");
}
else if (_count == 1) {
setState(() {
return new Padding(
padding: const EdgeInsets.all(30.0),
child:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.only(
bottom: 30.0,
),
child: Text(
"LOGIN",
textAlign: TextAlign.center,
),
),
TextField(
decoration: InputDecoration(
hintText: "Enter Email",
labelText: "Email",
),
onChanged: (value) {
_email = value;
},
),
TextField(
decoration: InputDecoration(
hintText: "Enter Password", labelText: "Password"),
onChanged: (value) {
_password = value;
},
),
Row (
children: [
TextButton(
onPressed: _createuser,
child: Text("Create account")),
TextButton(
onPressed: _login,
child: Text("Login")),
],
),
],
),
);
});
}
if (_count == 2) {
}
if (_count == 3) {
}
}
【问题讨论】:
-
我们很乐意为您提供帮助。但是如果您粘贴代码和您面临的错误,我们会更容易理解。
-
非常感谢。我已经添加了代码。但是我才刚刚开始,可能全都错了。
标签: android flutter dart flutter-layout