【问题标题】:Flutter Page View dispose children, how to keep children and blocFlutter Page View dispose children,如何留住children和bloc
【发布时间】:2020-08-18 10:15:05
【问题描述】:

我有下一个代码结构,当我滑动到下一页时,pageview distroy previos page(widget and bloc)
home: SafeArea( child: PageView(controller: _pageController, children: [ Scaffold( body: BlocProvider<Bloc>( bloc: bloc, child: Container()), Scaffold( body: BlocProvider<Bloc>( bloc: bloc, child: Container()) Scaffold( body: BlocProvider<Bloc>( bloc: bloc, child: Container()))) 当 pageview 更改页面时,如何使用小部件保存 bloc 类?

【问题讨论】:

    标签: android flutter dart flutter-layout


    【解决方案1】:

    PageView 的孩子需要一个 StatefullWidget 并且 StatefullWidget 的状态是添加 with AutomaticKeepAliveClientMixin

    例子

    class _SearchPageState extends State<SearchPage>
        with AutomaticKeepAliveClientMixin {
      static const TAG = 'SearchPage';
    
      VoidCallback onTapSort, onTapArea, onTapDatePicker, onTapFilter;
    
      SearchPageFilter searchFilter;
    
      WorkBloc _workBloc;
    
      VoidCallback onReloadPage;
    
      @override
      void initState() {
        super.initState();
    
      @override
      Widget build(BuildContext context) {
        super.build(context);
        return ChangeNotifierProvider.value(
          value: searchFilter,
          child: Scaffold(
            appBar: AppBar(
              title: Text(AppLocalizations.of(context).titleSearchPage),
              bottom: PreferredSize(
                  child: SearchSliverPersistentHeaderDelegate(
                      onTapSort, onTapArea,
                      onTapDatePicker: onTapDatePicker,
                      onTapFilter: onTapFilter),
                  preferredSize: Size.fromHeight(128)),
            ),
            body: Builder(
                builder: (context) {
                  //.....
                }
            ),
          ),
        );
      }
    
      @override
      bool get wantKeepAlive => true;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 1970-01-01
      • 2019-08-07
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      相关资源
      最近更新 更多