Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak when use default header and footer. #513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/ios/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Icon?
/Flutter/App.framework
/Flutter/Flutter.framework
/Flutter/Generated.xcconfig
/Flutter/flutter_export_environment.sh
/ServiceDefinitions.json

Pods/
4 changes: 2 additions & 2 deletions lib/src/smart_refresher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ class SmartRefresherState extends State<SmartRefresher> {
double viewportExtent = 0;
bool _canDrag = true;

final RefreshIndicator defaultHeader =
RefreshIndicator get defaultHeader =>
defaultTargetPlatform == TargetPlatform.iOS
? ClassicHeader()
: MaterialClassicHeader();

final LoadIndicator defaultFooter = ClassicFooter();
LoadIndicator get defaultFooter => ClassicFooter();

//build slivers from child Widget
List<Widget>? _buildSliversByChild(BuildContext context, Widget? child,
Expand Down
57 changes: 57 additions & 0 deletions test/smart_refresher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,61 @@ void main() {
expect(RefreshConfiguration.of(context2)!.enableScrollWhenTwoLevel, true);
expect(RefreshConfiguration.of(context2)!.enableBallisticRefresh, false);
});

/// Rebuild the widget after 'refresh' or 'loadmore' is completed, check
/// their callbaks can get a correct timestamp(defined in 'builder').
/// If get a wrong timestamp, consider memory leak.
testWidgets("test memory leak", (tester) async {
final controller = RefreshController();
int currentTimestamp = 0;
await tester.pumpWidget(MaterialApp(
home: Directionality(
textDirection: TextDirection.ltr,
child: Builder(
key: Key('builder'),
builder: (context) {
int timestamp = DateTime.now().millisecondsSinceEpoch;
currentTimestamp = timestamp;
return SmartRefresher(
controller: controller,
enablePullUp: true,
onLoading: () {
controller.loadComplete();
expect(timestamp, currentTimestamp);
tester
.firstElement(find.byKey(Key('builder')))
.markNeedsBuild();
},
onRefresh: () {
controller.refreshCompleted();
expect(timestamp, currentTimestamp);
tester
.firstElement(find.byKey(Key('builder')))
.markNeedsBuild();
},
child: ListView(
children: List<Widget>.generate(20, (index) {
return Container(height: 50, child: Text('Item: $index'));
}),
),
);
}),
),
));

// test pull refresh
await tester.drag(find.byType(Scrollable), const Offset(0, 100.0),
touchSlopY: 0.0);
await tester.pumpAndSettle();
await tester.drag(find.byType(Scrollable), const Offset(0, 100.0),
touchSlopY: 0.0);
await tester.pumpAndSettle();

// test load more
controller.position!.jumpTo(controller.position!.maxScrollExtent - 30.0);
await tester.drag(find.byType(Scrollable), const Offset(0, -30.0));
await tester.pumpAndSettle();
await tester.drag(find.byType(Scrollable), const Offset(0, -30.0));
await tester.pumpAndSettle();
});
}