Kyaw Tun
Kyaw Tun

Reputation: 13131

Flutter: How to restore ListView position?

When routing master ListView and its detail ItemView with Navigator, scroll position of the ListView was lost on back stack.

How to restore ListView scroll position?

Reusing ListView widget also lost position - also do not see reusing Widget in flutter example.

Sample code as request by Rainer,

 Widget build(BuildContext context) {
return new MaterialApp(

  routes: <String, WidgetBuilder>{
    '/': (BuildContext context) => new MyListView(),
  },
  onGenerateRoute: (RouteSettings s) {
    final List<String> paths = name.split('/');
    ...
    return new new ItemView(paths[1])
  }
}

In MyListView, when ItemRow.onTab => Navigator.pushNamed(c, '/item/1').

In ItemView back button call Navigator.pushNamed('/');

Both Widget are stateful

Upvotes: 4

Views: 4182

Answers (1)

Collin Jackson
Collin Jackson

Reputation: 116728

This should work. Here is some sample code.

screenshot

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyHomePage(),
  ));
}

class MyHomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView(
          children: new List.generate(100, (int index) {
            return new ListTile(
              title: new Text('Item $index'),
              onTap: () {
                Navigator.push(
                  context,
                  new MaterialPageRoute(
                    builder: (BuildContext context) {
                      return new Scaffold(
                        body: new Center(
                          child: new RaisedButton(
                            child: new Text('Pop!'),
                            onPressed: () => Navigator.pop(context),
                          ),
                        ),
                      );
                    },
                  ),
                );
              },
            );
          },
        ),
      ),
    );
  }
}

Make sure you're using Navigator.pop() instead of Navigator.pushNamed('/'); to go back. If it doesn't work, please post more of your code or file an issue.

Upvotes: 1

Related Questions