【问题标题】:How to avoid keyboard popping out while navigating to next screen?导航到下一个屏幕时如何避免键盘弹出?
【发布时间】:2022-04-16 16:07:53
【问题描述】:

当我从 LoginScreen() 导航到 HomeScreen() 时,键盘会无缘无故地突然弹出,然后在导航进行时立即关闭。

调用屏幕pushReplacement的函数:

    () async {
                      if (_formKey.currentState.validate()) {
                        final FirebaseAuth _auth = FirebaseAuth.instance;
                        try {
                          await _auth.signInWithEmailAndPassword(
                              email: email, password: password);
                        } catch (e) {
                          Scaffold.of(context).showSnackBar(SnackBar(
                            content: Text('Email or Password are incorrect'),
                          ));
                        }

                        _user = await _auth.currentUser();

                        if (_user == null) {}
                        if (_user.isEmailVerified == true) {
                          Navigator.of(context)
                              .pushReplacementNamed(HomeScreen.routeName);
                        } else
                          Scaffold.of(context).showSnackBar(SnackBar(
                            content: Text('Validate your email pls!'),
                          ));
                      }
                    },

和 HomeScreen():

    import 'package:final_login/screens/loginscreen.dart';
    import 'package:final_login/services/auth.dart';
    import 'package:flutter/material.dart';

    class HomeScreen extends StatefulWidget {
      static const routeName = '/home-screen';
      @override
      _HomeScreenState createState() => _HomeScreenState();
    }

    class _HomeScreenState extends State<HomeScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Center(
            child: Column(
              children: <Widget>[
                RaisedButton(
                  child: Text('Log out'),
                  onPressed: (){
                    AuthService().signOut();
                    Navigator.of(context).pushReplacementNamed(LoginScreen.routeName);
                  },
                ),

              ],
            ),
          ),
        );
      }
    }

通过将FocusScope.of(context).requestFocus(FocusNode());放在函数调用之前解决。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    更新:

    以下答案不再是颤振更新后最好的方法,这是现在更好的方法:

    FocusScope.of(context).unfocus();

    旧答案

    试试这个,它会在推送新页面之前以编程方式移除键盘:

    if (_user.isEmailVerified == true) {
      FocusScope.of(context).requestFocus(FocusNode());
      Navigator.of(context).pushReplacementNamed(HomeScreen.routeName);
    }
    

    【讨论】:

    • 注意:FocusNode() 必须处理。湖记忆。
    【解决方案2】:

    上层答案对我不起作用。只需在导航之前尝试此代码即可。

    FocusScope.of(context).unfocus();

    RaisedButton(
        onPressed: () {
        FocusScope.of(context).unfocus();
        Navigator.of(context).pushReplacement(
        MaterialPageRoute(
        builder: (context) => HomeScreen(),
                                        ),
                                      );
                                    },
                                  ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 2010-11-09
      • 2021-11-16
      • 2019-12-15
      • 2021-03-22
      相关资源
      最近更新 更多