【问题标题】:Load a widget on button click (Flutter)在按钮单击时加载小部件 (Flutter)
【发布时间】: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


【解决方案1】:

您可以使用StatefulWidget 更新视图:

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  int _count = -1;
  String _email = '';
  String _password = '';

  @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: () {
                    setState(() {
                      _count = 1;
                    });
                    print(_count);
                  }),
              MaterialButton(
                onPressed: () {},
                child: Text("Using Phone"),
              ),
              MaterialButton(child: Text("Go back to home"), onPressed: () {}),
              createWidget(_count),
            ],
          ),
        ),
      ),
    );
  }

  Widget createWidget(int count) {
    print("inside create");
    if (count == 0) {
      print(count);
      return new Text("Select an option");
    } else if (count == 1) {
      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: () {}, child: Text("Create account")),
                TextButton(onPressed: () {}, child: Text("Login")),
              ],
            ),
          ],
        ),
      );
    }
    if (count == 2) {
      return Container();
    }
    if (count == 3) {
      return Container();
    }
    return Container();
  }
}

【讨论】:

  • 非常感谢。它完美地工作!有什么资源可以分享给初学者学习 Flutter 吗?
  • api.flutter.dev 是最好的资源,有很多细节。另外,如果对您有帮助,请采纳答案,谢谢。
  • 非常感谢!
猜你喜欢
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 2013-01-28
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
相关资源
最近更新 更多