flutter AutomaticKeepAliveClientMixin不起作用?

參考 https://www.colabug.com/3162835.html
https://stackoverflow.com/questions/53011686/flutter-automatickeepaliveclientmixin-is-not-working-with-bottomnavigationbar62835.html

Flutter切換tab后保留tab狀態(tài) 概述 Flutter中為了節(jié)約內(nèi)存不會(huì)保存widget的狀態(tài),widget都是臨時(shí)變量。當(dāng)我們使用TabBar,TabBarView是我們就會(huì)發(fā)現(xiàn),切換tab,initState又會(huì)被調(diào)用一次。
怎么為了讓tab一直保存在內(nèi)存中,不被銷(xiāo)毀?
添加AutomaticKeepAliveClientMixin,并設(shè)置為true,這樣就能一直保持當(dāng)前不被initState了。

class TicketListViewState extends State<TicketListView>
    with AutomaticKeepAliveClientMixin {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return new SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        onRefresh: _onRefresh,
        controller: refreshController,
        child: ListView.builder(
          itemCount: _result.length,
          itemBuilder: (context, index) {
            return getItem(_result[index]);
          },
        ));
  }
  //不會(huì)被銷(xiāo)毀,占內(nèi)存中
  @override
  bool get wantKeepAlive => true;
}

如果不起作用

  @override
  Widget build(BuildContext context) {
    super.build(context);//必須添加
   .....
        ));

官方解釋

/// A mixin with convenience methods for clients of [AutomaticKeepAlive]. Used
/// with [State] subclasses.
///
/// Subclasses must implement [wantKeepAlive], and their [build] methods must
/// call `super.build` (the return value will always return null, and should be
/// ignored).
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容