当推入新页面而弹出旧页面时,将重新创建上一页的Flutter Bloc

时间:2019-10-04 15:11:13

标签: flutter flutter-layout bloc

我有一个Flutter应用程序,我正在使用大量的块来更新和检索列表以及开发整个列表等。因此,例如,下面我显示了一个页面,例如,我有一个Clist页面,其中有一个块并运行循环调用返回的CListIndividualCard(详细信息:state.cList [index]);实际上是一张卡。下面首先是Clist的代码,然后是CListIndividualCard。现在的问题是我点击了手势,将我带到了此页面

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => BListPage()
  )
);

,以及在此新页面BListPage被推送后,我在页面bloc clistApiBLoC中注意到的内容。如何避免在后台重新创建?

CList页面代码。

class _CListPageState extends State<CListPage> with SingleTickerProviderStateMixin {
  Widget _form;
  CListApiBloc clistApiBLoC;
  CListRepository _repo;
  ScrollController _scrollController = new ScrollController();
  @override
  void initState() {
    _repo = new CListRepository();
    clistApiBLoC = new CListApiBloc(_repo);
    super.initState();
  }
  @override
  void dispose() {    
    super.dispose();
  }
  Widget build(BuildContext context) {

    if(_form==null){
      _form = _createForm(context);
    } 
    return _form;
  }
  Widget _createForm(BuildContext context) {
    final _scaffoldKey = new GlobalKey<ScaffoldState>();
      return BlocProviderTree(
        blocProviders: [          
          BlocProvider<CListApiBloc>(
            bloc: clistApiBLoC,
          ),
    ],
    cList: Scaffold(      
      key: _scaffoldKey,
      resizeToAvoidBottomInset: true,
      resizeToAvoidBottomPadding: false,
      backgroundColor: pagebackgroundColor,
      appBar: appBar(),
      drawer: Theme(
          data: Theme.of(context).copyWith(canvasColor: Colors.white),
          cList: new ReusableWidgets().getDrawer('C List',context)
      ),
        body: SafeArea(
                cList: Column(
                   cListren: <Widget>[
                    SizedBox(
                      height: 20.0,
                    ),
                    BlocBuilder(
                      bloc: clistApiBLoC,
                      builder: (BuildContext context, CListApiState state) {
                            if(state is CListInitialState){    
                              if(state.cList.length>0){
                                return Flexible(
                                  child: Container(
                                    margin: EdgeInsets.all(0.0),
                                    child: ListView.builder(
                                      controller: _scrollController,
                                      padding: EdgeInsets.only(top: 0.0, right: 0.0),
                                      physics: AlwaysScrollableScrollPhysics(),
                                      shrinkWrap: true,
                                      itemCount: state.cList?.length??0,
                                      itemBuilder: (BuildContext ctxt, int index) {
                                         return CListIndividualCard(details: state.cList[index]);
                                      },
                                    ),
                                  ),
                                );
                              }
                              else{
                                  return Container(
                                    cList: Center(
                                      cList: Text("No C Record Found"),
                                    ),
                                  );
                              }
                            }
                            if (state is CListApiStateError) {
                              return Flexible(
                                cList: Center(
                                  cList: Text(state.errorMessage,style: errorStyle,),
                                ),
                              );
                            }
                            if (state is LoadingState) {
                              return Flexible(
                                cList: Center(
                                  cList:CircularProgressIndicator(),
                                ),
                              );
                            }
                            return Container(height: 0, width: 0,);
                        },
                      ),                    
                  ],
                )
        )
    )
  );
}

这是居民卡代码。

class _CListIndividualCardState extends State<CListIndividualCard> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[       
        RoundedCardDataNoColor(
          child: Row( 
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                             Column(
                               crossAxisAlignment: CrossAxisAlignment.start,
                               mainAxisSize: MainAxisSize.max,
                               children: <Widget>[
                                      Row(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: <Widget>[
                                        GestureDetector(
                                          onTap: () {
                                          Navigator.pop(context);
                                          Navigator.push(
                                              context,
                                                MaterialPageRoute(
                                                    builder: (context) => BListPage(                                                      
                                                      cID: widget.details.cID,
                                                      sID: widget.details.sID,                                                      
                                                      lID: widget.details.lID,
                                                ),
                                              )
                                          );
                                          },
                                          child: Icon(Icons.shopping_cart,size: 18,color: Colors.blue,),
                                        ),
                                        SizedBox(width: 5,),
                                        Text(
                                          "Purchase",
                                          style: purchaseStyle,
                                        ),            
                                      ],

                                    ),          



                                ],
                             ),
                             SizedBox(width: 20,),
                          ],
                        ),
                ],
              )

            ]
          )
        )
      ]
    );
  }

0 个答案:

没有答案