Skip to content

Commit a4b0d97

Browse files
authored
Added new constructor RefreshIndicator.noSpinner() (#152075)
This PR adds a new constructor to the RefreshIndicator's class, which is `noSpinner`. The purpose of this new constructor is to create a RefreshIndicator that doesn't show a spinner when the user arms it by pulling. The work is based on a partial that is here: flutter/flutter#133507 I addressed the following issues reported in the PR above: - in the example for `noSpinner`, arming the RefreshIndicator now shows a CircularProgressIndicator, instead of just printing text to the console; - added a test for the new example; - added a doc comment on the new constructor; Fixes flutter/flutter#132775
1 parent 5e19438 commit a4b0d97

File tree

4 files changed

+360
-91
lines changed

4 files changed

+360
-91
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
7+
/// Flutter code sample for [RefreshIndicator.noSpinner].
8+
9+
void main() => runApp(const RefreshIndicatorExampleApp());
10+
11+
class RefreshIndicatorExampleApp extends StatelessWidget {
12+
const RefreshIndicatorExampleApp({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 StatefulWidget {
23+
const RefreshIndicatorExample({super.key});
24+
25+
@override
26+
State<RefreshIndicatorExample> createState() => _RefreshIndicatorExampleState();
27+
}
28+
29+
class _RefreshIndicatorExampleState extends State<RefreshIndicatorExample> {
30+
bool _isRefreshing = false;
31+
32+
@override
33+
Widget build(BuildContext context) {
34+
return Scaffold(
35+
appBar: AppBar(
36+
title: const Text('RefreshIndicator.noSpinner Sample'),
37+
),
38+
body: Stack(
39+
children: <Widget>[
40+
RefreshIndicator.noSpinner(
41+
// Callback function used by the app to listen to the
42+
// status of the RefreshIndicator pull-down action.
43+
onStatusChange: (RefreshIndicatorStatus? status) {
44+
if (status == RefreshIndicatorStatus.done) {
45+
setState(() {
46+
_isRefreshing = false;
47+
});
48+
}
49+
},
50+
51+
// Callback that gets called whenever the user pulls down to refresh.
52+
onRefresh: () async {
53+
// This can be also done in onStatusChange when the status is RefreshIndicatorStatus.refresh.
54+
setState(() {
55+
_isRefreshing = true;
56+
});
57+
58+
// Replace this delay with the code to be executed during refresh
59+
// and return asynchronous code.
60+
return Future<void>.delayed(const Duration(seconds: 3));
61+
},
62+
63+
child: CustomScrollView(
64+
slivers: <Widget>[
65+
SliverList.builder(
66+
itemCount: 20,
67+
itemBuilder: (BuildContext context, int index) {
68+
return ListTile(
69+
tileColor: Colors.green[100],
70+
title: const Text('Pull down here'),
71+
subtitle: const Text('A custom refresh indicator will be shown'),
72+
);
73+
}
74+
)
75+
],
76+
),
77+
),
78+
79+
// Shows an overlay with a CircularProgressIndicator when refreshing.
80+
if (_isRefreshing)
81+
ColoredBox(
82+
color: Colors.black45,
83+
child: Align(
84+
child: CircularProgressIndicator(
85+
color: Colors.purple[500],
86+
strokeWidth: 10,
87+
semanticsLabel: 'Circular progress indicator',
88+
),
89+
),
90+
),
91+
]
92+
),
93+
);
94+
}
95+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.2.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Pulling from scroll view triggers a refresh indicator which shows a CircularProgressIndicator', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.RefreshIndicatorExampleApp(),
13+
);
14+
15+
// Pull the first item.
16+
await tester.fling(find.text('Pull down here').first, const Offset(0.0, 300.0), 1000.0);
17+
await tester.pump();
18+
await tester.pump(const Duration(seconds: 1));
19+
await tester.pump(const Duration(seconds: 1));
20+
21+
expect(find.byType(RefreshProgressIndicator), findsNothing);
22+
expect(find.bySemanticsLabel('Circular progress indicator'), findsOneWidget);
23+
24+
await tester.pumpAndSettle(); // Advance pending time.
25+
});
26+
}

0 commit comments

Comments
 (0)