Skip to content

Instantly share code, notes, and snippets.

@boldijar
Created March 9, 2018 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boldijar/619c2ab4da5315edb7f9dd77e850c684 to your computer and use it in GitHub Desktop.
Save boldijar/619c2ab4da5315edb7f9dd77e850c684 to your computer and use it in GitHub Desktop.
List view in dart that will keep it's scrolling position
import 'package:flutter/material.dart';
class DoubleHolder {
double value = 0.0;
}
class StatefulListView extends StatefulWidget {
final DoubleHolder offset = new DoubleHolder();
StatefulListView(this._itemCount,
this._indexedWidgetBuilder,
{
Key key
})
: super(key: key);
double getOffsetMethod() {
return offset.value;
}
void setOffsetMethod(double val) {
offset.value = val;
}
final int _itemCount;
final IndexedWidgetBuilder _indexedWidgetBuilder;
@override
_StatefulListViewState createState() =>
new _StatefulListViewState(_itemCount, _indexedWidgetBuilder);
}
class _StatefulListViewState extends State<StatefulListView> {
ScrollController scrollController;
final int _itemCount;
final IndexedWidgetBuilder _itemBuilder;
_StatefulListViewState(this._itemCount, this._itemBuilder);
@override
void initState() {
super.initState();
scrollController = new ScrollController(
initialScrollOffset: widget.getOffsetMethod()
);
}
@override
Widget build(BuildContext context) {
return new NotificationListener(
child: new ListView.builder(
controller: scrollController,
itemCount: _itemCount,
itemBuilder: _itemBuilder
),
onNotification: (notification) {
if (notification is ScrollNotification) {
widget.setOffsetMethod(notification.metrics.pixels);
}
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment