【问题标题】:setState() is not working inside my widget, since my that widget is built on the bottom of the widget tree, then how can I rebuild that widget?setState() 在我的小部件中不起作用,因为我的那个小部件是在小部件树的底部构建的,那么我该如何重建那个小部件?
【发布时间】:2021-09-26 14:12:48
【问题描述】:

我使用 Navigator.push 导航到下一个屏幕,在弹出该屏幕后,我无法重建旧屏幕。如果用户按下登录按钮,我还希望我的代码能够重建

//这是我的有状态Widget

class LoginpageClass extends StatefulWidget {
   @override
   _LoginpageClassState createState() => _LoginpageClassState();
}

class _LoginpageClassState extends State<LoginpageClass> {
  final _auth = FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
  return Scaffold(
      body: SingleChildScrollView(child: buildBody(context)),
  );
  } 
 }

//在上面调用这个小部件

Widget buildBody(BuildContext context) => Container(
  width: double.infinity,
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
      buildTopClippers(),
      heightSpacer(20.00),
      buildLogoField(),
      heightSpacer(25.00),
      buildTextField(
        "Username",
        Icons.person,
        false,
        onChangedEmail,
      ),
      heightSpacer(15.00),
      buildTextField("Password", Icons.lock, true, onChangedPassword),
      heightSpacer(25.00),
      buildSignUpText(context),
      heightSpacer(25.00),
      buildLoginContainer(context),
      heightSpacer(20.00),
      buildOtherLogins(context),
      buildBottomClippers()
    ],
  ),
);

//上面调用buildLoginContainer

  Widget buildLoginContainer(BuildContext context) => Container(
    width: MediaQuery.of(context).size.width,
    height: 65,
    child: Stack(
      children: [
        buildLoginBtn(context),
      ],
    ),
  );

 Widget buildLoginBtn(context) => Positioned(
     right: -100,
     child: FlatButton(
        onPressed: () async {
            print(email);
         try {
            final newUser = await _auth.signInWithEmailAndPassword(
                email: Email, password: Password);
            print(newUser);

            if (newUser != null) {
              Navigator.pushNamed(context, '/');
             }
           } catch (e) {
           print(e);
        
         }
        },
    child: Container(
      width: 350,
      height: 65,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(40.00),
        color: Color.fromRGBO(11, 94, 255, 1),
      ),
      child: buildLoginText(),
    ),
  ),
);

Widget buildLoginText() => Row(
  children: [
    widthSpacer(75.00),
    Text(
      "Login",
      style: TextStyle(color: Colors.white, fontSize: 20.00),
    ),
    widthSpacer(5.5),
    Icon(
      Icons.navigate_next,
      color: Colors.white,
    )
  ],
);

我是开发人员的初学者,因此我不知道如何在这个小部件中使用 setState,而且它也不能通过这个小部件访问。所有小部件都是在单独的 dart 文件中创建的。

我查看了一些关于继承小部件使用的文章。这里怎么用??还是其他方法??

【问题讨论】:

    标签: flutter push setstate navigator


    【解决方案1】:

    在使用push 时,您正在存储状态,这就是为什么当您返回时,您会发现相同的状态。让我们使用pushReplacement,也可以在流行方面使用。

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多