|
| 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 | +} |
0 commit comments