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/gestures.dart'; |
6 | import 'package:flutter/material.dart'; |
7 | |
8 | /// Flutter code sample for [RefreshIndicator]. |
9 | |
10 | void main() => runApp(const RefreshIndicatorExampleApp()); |
11 | |
12 | class RefreshIndicatorExampleApp extends StatelessWidget { |
13 | const RefreshIndicatorExampleApp({super.key}); |
14 | |
15 | @override |
16 | Widget build(BuildContext context) { |
17 | return MaterialApp( |
18 | scrollBehavior: const MaterialScrollBehavior().copyWith( |
19 | dragDevices: PointerDeviceKind.values.toSet(), |
20 | ), |
21 | home: const RefreshIndicatorExample(), |
22 | ); |
23 | } |
24 | } |
25 | |
26 | class RefreshIndicatorExample extends StatelessWidget { |
27 | const RefreshIndicatorExample({super.key}); |
28 | |
29 | @override |
30 | Widget build(BuildContext context) { |
31 | return Scaffold( |
32 | appBar: AppBar(title: const Text('RefreshIndicator Sample' )), |
33 | body: RefreshIndicator( |
34 | color: Colors.white, |
35 | backgroundColor: Colors.blue, |
36 | onRefresh: () async { |
37 | // Replace this delay with the code to be executed during refresh |
38 | // and return asynchronous code |
39 | return Future<void>.delayed(const Duration(seconds: 3)); |
40 | }, |
41 | // This check is used to customize listening to scroll notifications |
42 | // from the widget's children. |
43 | // |
44 | // By default this is set to `notification.depth == 0`, which ensures |
45 | // the only the scroll notifications from the first scroll view are listened to. |
46 | // |
47 | // Here setting `notification.depth == 1` triggers the refresh indicator |
48 | // when overscrolling the nested scroll view. |
49 | notificationPredicate: (ScrollNotification notification) { |
50 | return notification.depth == 1; |
51 | }, |
52 | child: CustomScrollView( |
53 | slivers: <Widget>[ |
54 | SliverToBoxAdapter( |
55 | child: Container( |
56 | height: 100, |
57 | alignment: Alignment.center, |
58 | color: Colors.pink[100], |
59 | child: Column( |
60 | mainAxisAlignment: MainAxisAlignment.center, |
61 | children: <Widget>[ |
62 | Text('Pull down here' , style: Theme.of(context).textTheme.headlineMedium), |
63 | const Text("RefreshIndicator won't trigger" ), |
64 | ], |
65 | ), |
66 | ), |
67 | ), |
68 | SliverToBoxAdapter( |
69 | child: Container( |
70 | color: Colors.green[100], |
71 | height: 300, |
72 | child: ListView.builder( |
73 | itemCount: 25, |
74 | itemBuilder: (BuildContext context, int index) { |
75 | return const ListTile( |
76 | title: Text('Pull down here' ), |
77 | subtitle: Text('RefreshIndicator will trigger' ), |
78 | ); |
79 | }, |
80 | ), |
81 | ), |
82 | ), |
83 | SliverList.builder( |
84 | itemCount: 20, |
85 | itemBuilder: (BuildContext context, int index) { |
86 | return const ListTile( |
87 | title: Text('Pull down here' ), |
88 | subtitle: Text("Refresh indicator won't trigger" ), |
89 | ); |
90 | }, |
91 | ), |
92 | ], |
93 | ), |
94 | ), |
95 | ); |
96 | } |
97 | } |
98 | |