可以将小部件缓存以重复使用吗?

huangapple go评论138阅读模式
英文:

Is that possible to cache widget to resue?

问题

I am using getbody() method to get app body and the getbody() return a Widget <br>If i change the variable ct count the the getbody() will return different widget and each widget is stroied in Listqueue&lt;MyPage()&gt; <br> if i set variable ct value 1 it return widget from my list at position 1 and if i set variable ct 2 then it will return corresponding widget .

<br>But the problem is in each widget i am doing an api call but when i reuse that widget it is again calling the API call and why this is calling after first time nd how to stop calling api when i am reusing the widget
<br><br>

ListQueue<Page> page1, page2, page3, page4;
_AppFramePageState() {

page1 = ListQueue();
page1.add(Page(widget: AppHomePage(), title: &quot;Home Page&quot;));
page2 = ListQueue();
page2.add(
    Page(widget: TemplesListing(), title: &quot;Temple listing&quot;));
page3 = ListQueue();
page3.add(Page(widget: AppCommunities(), title: &quot;Communities&quot;));
page4 = ListQueue();
page4.add(Page(widget: AppVideos(), title: &quot;Media&quot;));

}

<br>

If user click on cart button from toolbar then i will add more value to page1 list and if user click on back button then i will remove last item from page1 list
<br>

Widget getBody() {
//This code is solved my 50% issue but renaming 50% is there. How to solve this issue?
return IndexedStack(
index: _selectedIndex,
children: <Widget>[
page1.last.widget,
page2.last.widget,
page3.last.widget,
page4.last.widget
],);<br>
//This is the code i used first time
switch (_selectedIndex) {
case 0:
return page1.last.widget;
case 1:
return page2.last.widget;
case 2:
return page3.last.widget;
case 3:
return page4.last.widget;
}
}

<br><br>

class Page{
Widget widget;
String title;
Page({this.title,this.widget});
}

<br><br>
<br><br>Note: All widgets are StatefulWidget

英文:

I am using getbody() method to get app body and the getbody() return a Widget <br>If i change the variable ct count the the getbody() will return different widget and each widget is stroied in Listqueue&lt;MyPage()&gt; <br> if i set variable ct value 1 it return widget from my list at position 1 and if i set variable ct 2 then it will return corresponding widget .

<br>But the problem is in each widget i am doing an api call but when i reuse that widget it is again calling the API call and why this is calling after first time nd how to stop calling api when i am reusing the widget
<br><br>

ListQueue&lt;Page&gt; page1, page2, page3, page4;
  _AppFramePageState() {
   
    page1 = ListQueue();
    page1.add(Page(widget: AppHomePage(), title: &quot;Home Page&quot;));
    page2 = ListQueue();
    page2.add(
        Page(widget: TemplesListing(), title: &quot;Temple listing&quot;));
    page3 = ListQueue();
    page3.add(Page(widget: AppCommunities(), title: &quot;Communities&quot;));
    page4 = ListQueue();
    page4.add(Page(widget: AppVideos(), title: &quot;Media&quot;));
  }

<br>

If user click on cart button from toolbar then i will add more value to page1 list and if user click on back button then i will remove last item from page1 list
<br>

     Widget getBody() {
 //This code is solved my 50% issue but renaming 50% is there. How to solve this issue?
        return IndexedStack(
          index: _selectedIndex,
          children: &lt;Widget&gt;[
          page1.last.widget,
          page2.last.widget,
          page3.last.widget,
          page4.last.widget
        ],);&lt;br&gt;
//This is the code i used first time 
        switch (_selectedIndex) {
          case 0:
            return page1.last.widget;
          case 1:
            return page2.last.widget;
          case 2:
            return page3.last.widget;
          case 3:
            return page4.last.widget;
        }
      }

<br><br>

class Page{
  Widget widget;
  String title;
  Page({this.title,this.widget});
}

<br><br>
<br><br>Note: All widgets are StatefulWidget

答案1

得分: 1

总的来说,无法重用这些小部件。

您可以通过比较小部件的旧状态和新状态来手动重用缓存小部件。这是非常繁琐的,我认为您不应该这样做。

有许多状态管理架构模式,如Provider、BLOC、MOBX等,可以很好地管理您的应用程序。它们用于提高应用程序性能,减少小部件的重新渲染,管理整个应用程序的数据流等。

您还可以通过在可能的情况下使用const关键字来使您的有状态小部件更具冲击力。

比如对于以下小部件,

Column(
    children: <Widget>[
      // 小部件 1
      Center(child: Text(dynamic_value),),
      // 小部件 2
      Container(
        child: const Center(
          child: const Padding(
            padding: const EdgeInsets.all(10),
            child: Text("Hello"),
          ),
        ),
      ),
    ],
)

在上面的示例中,对于小部件 1,无法使用"const关键字",因为它依赖于"dynamic_value"。

而对于小部件 2,可以使用"const关键字",这将在您的构建方法再次被调用时非常有用,因为**"Center"、"Padding"和"Text"**小部件将不会再次被调用,因为它们被声明为常量小部件。

英文:

In general, it's not possible to reuse the widgets.

You can manually reuse the cache widget by comparing old and new state of the widgets. Which is very lengthy and i don't think you should follow it.

There are many State management architecture patterns like Provider, BLOC, MOBX etc. to manage your app in a great way. They are used to improve your app performance and decrease widget re-renders, manage data flow across the whole app etc.

One more thing you can do to make your Stateful widgets more impactive by using the const keyword whenever possible.

like for following widget ,

Column(
        children: &lt;Widget&gt;[
          // Widget 1
          Center(child: Text(dynamic_value),),
          // Widget 2
          Container(
            child: const Center(
              child: const Padding(
                padding: const EdgeInsets.all(10),
                child: Text(&quot;Hello&quot;),
              ),
            ),
          ),
        ],
      )

In above example for Widget 1, you can't use "const keyword" as it depends on "dynamic_value".

While for Widget 2, you can use the "const keyword" which will be useful if your build method gets called again then "Center", "Padding" & "Text" widgets will be not called again as they are declared as constant widgets.

huangapple
  • 本文由 发表于 2020年1月6日 18:39:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定