Flutter:如何在点击按钮时添加进度对话框?

tjrkku2a  于 2023-05-29  发布在  Flutter
关注(0)|答案(2)|浏览(177)

我正在尝试添加一个进度对话框时,点击我的按钮。
下面是我的代码:

Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Center(child: Text('Login')),
        backgroundColor: const Color(0xff2e80e4)
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 60.0),
              child: Center(
                child: Container(
                  width: 200,
                  height: 150,
                  child: Image.asset('assets/logo.jpg'),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 15),
              child: TextField(
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'Username',
                  hintText: 'Please enter your username'
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(left: 15.0, right: 15.0, top: 15.0, bottom: 0),
              child: TextField(
                obscureText: true,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'Password',
                  hintText: 'Please enter your password'
                ),
              ),
            ),
            TextButton(
                onPressed: () async{
                  await Navigator.pushNamed(context, '/forget_password');
                },
                child: Text(
                  'Forget Password',
                  style: TextStyle(color: const Color(0xff2e80e4), fontSize: 15.0),
                ),
            ),
            Container(
              height: 50.0,
                width: 250.0,
              decoration: BoxDecoration(
                color: const Color(0xff2e80e4),
                borderRadius: BorderRadius.circular(20.0)
              ),
              child: TextButton(
                onPressed: () async{
                 //here I need to start the progress dialog
                  http.Response response = await postRequest();
                  Map<String, dynamic> parsedData = jsonDecode(response.body);
                  LoginResponse loginResponse = LoginResponse.fromJson(parsedData);
                  if(loginResponse.verificationStatus)
                  {
                    await Navigator.pushNamed(context, '/home');
                  }
                  //Here I need to stop the progress dialog
                },
                child: Text(
                  'Login',
                  style: TextStyle(color: Colors.white, fontSize: 25.0),
                ),
              ),
            ),

            SizedBox(height: 100.0),
            TextButton(
              onPressed: () async{
                await Navigator.pushNamed(context, '/sign_up');
              },
                child: Text(
                    'New User? Create Account',
                  style: TextStyle(color: const Color(0xff2e80e4), fontSize: 15.0),
                ),
            )
          ],
        ),
      ),
    );
  }

我喜欢启动进度对话框时,我点击登录按钮,并停止后加载主页。可以使用flutter_spinkit吗?我试过了,但是没有进展,我很新的扑!

tf7tbtn2

tf7tbtn21#

您可以使用状态作为更新状态来管理它,根据您的条件重新呈现您的小部件并显示内容。请找到下面更新的代码,让我们知道,如果有什么是错过了这里

注意:-我在这里假设你的小部件扩展了StatefulWidget而不是StatelessWidget,因为你只添加了build方法

Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
          title: Center(child: Text('Login')),
          backgroundColor: const Color(0xff2e80e4)
      ),
      body: _isLoading ? const Center(child: CircularProgressIndicator(),) : SingleChildScrollView(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 60.0),
              child: Center(
                child: Container(
                  width: 200,
                  height: 150,
                  child: Image.asset('assets/logo.jpg'),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 15),
              child: TextField(
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Username',
                    hintText: 'Please enter your username'
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(left: 15.0, right: 15.0, top: 15.0, bottom: 0),
              child: TextField(
                obscureText: true,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Password',
                    hintText: 'Please enter your password'
                ),
              ),
            ),
            TextButton(
              onPressed: () async{
                await Navigator.pushNamed(context, '/forget_password');
              },
              child: Text(
                'Forget Password',
                style: TextStyle(color: const Color(0xff2e80e4), fontSize: 15.0),
              ),
            ),
            Container(
              height: 50.0,
              width: 250.0,
              decoration: BoxDecoration(
                  color: const Color(0xff2e80e4),
                  borderRadius: BorderRadius.circular(20.0)
              ),
              child: TextButton(
                onPressed: () async{
                  setState(() {
                    _isLoading = true;
                  });
                  //here I need to start the progress dialog
                  http.Response response = await postRequest();
                  Map<String, dynamic> parsedData = jsonDecode(response.body);
                  LoginResponse loginResponse = LoginResponse.fromJson(parsedData);
                  if(loginResponse.verificationStatus)
                  {
                    setState(() {
                      _isLoading = false;
                    });
                    await Navigator.pushNamed(context, '/home');
                  }
                  //Here I need to stop the progress dialog
                },
                child: Text(
                  'Login',
                  style: TextStyle(color: Colors.white, fontSize: 25.0),
                ),
              ),
            ),

            SizedBox(height: 100.0),
            TextButton(
              onPressed: () async{
                await Navigator.pushNamed(context, '/sign_up');
              },
              child: Text(
                'New User? Create Account',
                style: TextStyle(color: const Color(0xff2e80e4), fontSize: 15.0),
              ),
            )
          ],
        ),
      ),
    );
  }

正如你提到的,你是初学者,我想建议通过状态管理Flutter,因为它会帮助你做很多事情
https://docs.flutter.dev/ui/interactive

ubbxdtey

ubbxdtey2#

在上面的答案中回答您的问题(没有评论):您可以使用Stack Widget将子部件堆叠在另一个顶部。为一个空的小部件返回Sizebox.shrink()。

Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.white,
  appBar: AppBar(
      title: Center(child: Text('Login')),
      backgroundColor: const Color(0xff2e80e4)
  ),
  body: Stack(children: [
      _isLoading ? const Center(child: CircularProgressIndicator(),) : SizedBox.shrink(),
      YourSingleChildScrollView
      ])
  }

相关问题