简体   繁体   English

如何停止 SetState flutter

[英]How stop SetState flutter

I work on flutter project.我从事 flutter 项目。 when i click to modify icon to edit name for example ==> the screen is roaleded automatically.当我单击修改图标以编辑名称时,例如 ==> 屏幕会自动滚动。 How i can stop refresh screen after click on edit button?单击编辑按钮后如何停止刷新屏幕?

this piece of my Form code:这段我的表单代码:

Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: [
        Text('Adresse email :',
            style: TextStyle(
                color: Color(0xFF4053FCF),
                fontSize: 16,
                fontWeight: FontWeight.w600
            ),
        ),
        IconButton(
            icon: Icon(CommunityMaterialIcons.pencil,
            color: Colors.grey,
            ),
            onPressed: () {
                emailNode.requestFocus();
                setState(() {
                    enableemail = true;
                });
            })
    ],
),

  void editUserProfile() async {
    setState(() {});

    // if (_formKey.currentState.validate()) {
    String name = _nameController.text;
    String email = _emailController.text;
    String adress = _adressController.text;

    userApi.editUserProfile(name, email, adress).then((data) {
      print(data);
      if (data != null) {
        //   Navigator.pop(context);

        /*  Navigator.push(
            context, MaterialPageRoute(builder: (context) => Profile()));*/
      }
      //  setState(() {});

      /* Navigator.push(
            context, MaterialPageRoute(builder: (context) => BoxSettings()));*/

      setState(() {
        enableup = false;
        enableadress = false;
        enableemail = false;
      });

      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data)));

      // ScaffoldMessenger.of(context).showSnackBar(snackBar3);
    }).catchError((error) {
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text(error.toString())));
    });
    setState(() {});
  }

and this my screen for more information:这是我的屏幕以获取更多信息:

在此处输入图像描述

How i can press on edit button without reload screen?如何在不重新加载屏幕的情况下按下编辑按钮?

There some workarounds to achieve this (ie update the state of one widget after tapping a completely different widget) like passing the callback function as a parameter etc.有一些解决方法可以实现这一点(即在点击一个完全不同的小部件后更新一个小部件的 state),例如将回调 function 作为参数传递等。

But The best and neat solution here which will solve the above problem and keep your code neat is using Provider pattern.但是,解决上述问题并保持代码整洁的最佳且简洁的解决方案是使用 Provider 模式。

If you are not aware of how a Provider pattern works, you can easily google search for articles regarding it.如果您不了解 Provider 模式的工作原理,您可以轻松地在 Google 上搜索有关它的文章。 Here is one of them:这是其中之一:

https://www.raywenderlich.com/6373413-state-management-with-provider https://www.raywenderlich.com/6373413-state-management-with-provider

Read the above article before moving below.在移动到下面之前阅读上面的文章。

Basically what we do is:基本上我们所做的是:

Create a ChangeNotifier class.创建一个 ChangeNotifier class。

Wrap the parent of both widgets by a ChangeNotifierProvider widget.用 ChangeNotifierProvider 小部件包装两个小部件的父级。

Wrap the widget you want to update with Consumer widget.用 Consumer 小部件包装您要更新的小部件。

Then in your onTap/onPressed function of Edit button you can call a function which will call the notifyListener() function.然后在编辑按钮的 onTap/onPressed function 中,您可以调用 function,它将调用 notifyListener() function。 What this will do is it will notify the above ChangeNotifierProvider widget that some change has neen occured in it's widget tree.这样做会通知上面的 ChangeNotifierProvider 小部件,它的小部件树中发生了一些变化。 Then it will traverse the child whole widget tree below and will update the widget wrapped with Consumer widget.然后它将遍历下面的子整个小部件树,并更新用 Consumer 小部件包装的小部件。

So this way, you wont need to refresh your whole screen and you can easily update one widget by doing some action on a competely different widget.因此,通过这种方式,您不需要刷新整个屏幕,并且可以通过对完全不同的小部件执行一些操作来轻松更新一个小部件。

Wrap the widgets you want to refresh inside stateful builder and make the whole screen a stateless widget and then call stateful builder将要刷新的小部件包装在有状态构建器中,并使整个屏幕成为无状态小部件,然后调用有状态构建器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM