Flutter:导航到列表后如何阻止list.builder重建

时间:2019-06-27 15:46:43

标签: flutter

我有一个包含一些项目的清单,按时它会进入详细信息页面。当用户按详细的后退按钮时,他们将导航回到列表页面。但是,页面重建了,我失去了位置

如何阻止它重建并到达同一位置?

如果我的代码是..,请摘录。

Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        children: <Widget>[
          searchBoxWidget(),
          Expanded(
            child: (isLoading)
                ? Center(
                    child: new CircularProgressIndicator(),
                  )
                : buildListView(),
          ),
        ],
      ),
    );
  }


 ListView buildListView() {
    return ListView.builder(
      itemCount: jb.hits.length,
      controller: _scrollController,
      key: new PageStorageKey('jobslist'),
      itemBuilder: (BuildContext context, int index) {
        return InkWell(
          final page = DetailPage(
            lob: jb.hits[index].lob,
            ats: jb.hits[index].ats);

          onTap: () {
            **Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => page),
              ),
            );
          },**
          child: Container(
            // width: MediaQuery.of(context).size.width,
            padding: const EdgeInsets.all(14.0),

            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Flexible(
                      child: Padding(
                        padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
                        child: Text(
                          jb.hits[index].title,
                          style: TextStyle(
                            fontSize: 18.0,
                          ),
                        ),
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward,
                      color: Colors.blue,
                    )
                  ],
                ),
                SizedBox(
                  height: 8.0,
                ),
                Divider(color: Colors.brown),
              ],
            ),
          ),
        );
      },
    );
  }

//逻辑加载记录

void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        ++startPage;
        loadMore(startPage);
      }
    });
    searchService.getJobs(0, recordPerFetch, searchText).then((val) {
      setState(() {
        totalNumRecordfetch = val.hits.length;
        totalrecord = val.totalSize;
        jb = val;
        isLoading = false;
      });
    });
  }

1 个答案:

答案 0 :(得分:0)

AutomaticKeepAliveClientMixin

添加到状态

class HelloWorldPage extends StatefulWidget {
  @override
  _HelloWorldPageState createState() => _HelloWorldPageState();
}

class _HelloWorldPageState extends State<HelloWorldPage>
    with AutomaticKeepAliveClientMixin {

 @override
  Widget build(BuildContext context) {
  }

@override
  bool get wantKeepAlive => true;
}