Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 5739bf4

Browse files
authored
RefreshIndicator: Add notificationPredicate example (#103894)
1 parent a12a69a commit 5739bf4

File tree

4 files changed

+124
-8
lines changed

4 files changed

+124
-8
lines changed

examples/api/lib/material/refresh_indicator/refresh_indicator.0.dart

+3-8
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ void main() => runApp(const MyApp());
1111
class MyApp extends StatelessWidget {
1212
const MyApp({super.key});
1313

14-
static const String _title = 'RefreshIndicator Sample';
15-
1614
@override
1715
Widget build(BuildContext context) {
1816
return const MaterialApp(
19-
title: _title,
20-
home: RefreshIndicatorExample(title: _title),
17+
home: RefreshIndicatorExample(),
2118
);
2219
}
2320
}
2421

2522
class RefreshIndicatorExample extends StatefulWidget {
26-
const RefreshIndicatorExample({super.key, required this.title});
27-
28-
final String title;
23+
const RefreshIndicatorExample({super.key});
2924

3025
@override
3126
State<RefreshIndicatorExample> createState() => _RefreshIndicatorExampleState();
@@ -39,7 +34,7 @@ class _RefreshIndicatorExampleState extends State<RefreshIndicatorExample> {
3934
Widget build(BuildContext context) {
4035
return Scaffold(
4136
appBar: AppBar(
42-
title: Text(widget.title),
37+
title: const Text('RefreshIndicator Sample'),
4338
),
4439
body: RefreshIndicator(
4540
key: _refreshIndicatorKey,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flutter code sample for RefreshIndicator
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const MyApp());
10+
11+
class MyApp extends StatelessWidget {
12+
const MyApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return const MaterialApp(
17+
home: RefreshIndicatorExample(),
18+
);
19+
}
20+
}
21+
22+
class RefreshIndicatorExample extends StatelessWidget {
23+
const RefreshIndicatorExample({super.key});
24+
25+
@override
26+
Widget build(BuildContext context) {
27+
return Scaffold(
28+
appBar: AppBar(
29+
title: const Text('RefreshIndicator Sample'),
30+
),
31+
body: RefreshIndicator(
32+
color: Colors.white,
33+
backgroundColor: Colors.blue,
34+
onRefresh: () async {
35+
// Replace this delay with the code to be executed during refresh
36+
// and return asynchronous code
37+
return Future<void>.delayed(const Duration(seconds: 3));
38+
},
39+
// This check is used to customize listening to scroll notifications
40+
// from the widget's children.
41+
//
42+
// By default this is set to `notification.depth == 0`, which ensures
43+
// the only the scroll notifications from the first child are listened to.
44+
//
45+
// Here setting `notification.depth == 1` triggers the refresh indicator
46+
// when overscrolling the nested scroll view.
47+
notificationPredicate: (ScrollNotification notification) {
48+
return notification.depth == 1;
49+
},
50+
child: SingleChildScrollView(
51+
child: Column(
52+
children: <Widget>[
53+
Container(
54+
height: 100,
55+
alignment: Alignment.center,
56+
color: Colors.pink[100],
57+
child: Column(
58+
mainAxisAlignment: MainAxisAlignment.center,
59+
children: <Widget>[
60+
Text(
61+
'Pull down here',
62+
style: Theme.of(context).textTheme.headlineMedium,
63+
),
64+
const Text("RefreshIndicator won't trigger"),
65+
],
66+
),
67+
),
68+
Container(
69+
color: Colors.green[100],
70+
child: ListView.builder(
71+
shrinkWrap: true,
72+
itemCount: 25,
73+
itemBuilder: (BuildContext context, int index) {
74+
return const ListTile(
75+
title: Text('Pull down here'),
76+
subtitle: Text('RefreshIndicator will trigger'),
77+
);
78+
},
79+
),
80+
),
81+
],
82+
),
83+
),
84+
),
85+
);
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/refresh_indicator/refresh_indicator.1.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Pulling from nested scroll view triggers refresh indicator', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.MyApp(),
13+
);
14+
15+
// Pull from the upper scroll view.
16+
await tester.fling(find.text('Pull down here').first, const Offset(0.0, 300.0), 1000.0);
17+
await tester.pump();
18+
expect(find.byType(RefreshProgressIndicator), findsNothing);
19+
await tester.pumpAndSettle(); // Advance pending time
20+
21+
// Pull from the nested scroll view.
22+
await tester.fling(find.text('Pull down here').at(3), const Offset(0.0, 300.0), 1000.0);
23+
await tester.pump();
24+
expect(find.byType(RefreshProgressIndicator), findsOneWidget);
25+
await tester.pumpAndSettle(); // Advance pending time
26+
});
27+
}

packages/flutter/lib/src/material/refresh_indicator.dart

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ enum RefreshIndicatorTriggerMode {
7878
/// ** See code in examples/api/lib/material/refresh_indicator/refresh_indicator.0.dart **
7979
/// {@end-tool}
8080
///
81+
/// {@tool dartpad}
82+
/// This example shows how to trigger [RefreshIndicator] in a nested scroll view using
83+
/// the [notificationPredicate] property.
84+
///
85+
/// ** See code in examples/api/lib/material/refresh_indicator/refresh_indicator.1.dart **
86+
/// {@end-tool}
87+
///
8188
/// ## Troubleshooting
8289
///
8390
/// ### Refresh indicator does not show up

0 commit comments

Comments
 (0)