From 120d25f7868907d01066db1c62976fe357d4ca3e Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 28 Jan 2022 12:40:06 +0200 Subject: [PATCH] `RefreshIndicator`: Add an interactive example (#97254) --- .../refresh_indicator.0.dart | 74 +++++++++++++++++++ .../refresh_indicator.0_test.dart | 35 +++++++++ .../lib/src/material/refresh_indicator.dart | 6 ++ 3 files changed, 115 insertions(+) create mode 100644 examples/api/lib/material/refresh_indicator/refresh_indicator.0.dart create mode 100644 examples/api/test/material/refresh_indicator/refresh_indicator.0_test.dart diff --git a/examples/api/lib/material/refresh_indicator/refresh_indicator.0.dart b/examples/api/lib/material/refresh_indicator/refresh_indicator.0.dart new file mode 100644 index 0000000000..9a14e29576 --- /dev/null +++ b/examples/api/lib/material/refresh_indicator/refresh_indicator.0.dart @@ -0,0 +1,74 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Flutter code sample for RefreshIndicator + +import 'package:flutter/material.dart'; + +void main() => runApp(const MyApp()); + +class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + + static const String _title = 'RefreshIndicator Sample'; + + @override + Widget build(BuildContext context) { + return const MaterialApp( + title: _title, + home: RefreshIndicatorExample(title: _title), + ); + } +} + +class RefreshIndicatorExample extends StatefulWidget { + const RefreshIndicatorExample({Key? key, required this.title}) : super(key: key); + + final String title; + + @override + State createState() => _RefreshIndicatorExampleState(); +} + +class _RefreshIndicatorExampleState extends State { + final GlobalKey _refreshIndicatorKey = + GlobalKey(); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(widget.title), + ), + body: RefreshIndicator( + key: _refreshIndicatorKey, + color: Colors.white, + backgroundColor: Colors.blue, + strokeWidth: 4.0, + onRefresh: () async { + // Replace this delay with the code to be executed during refresh + // and return a Future when code finishs execution. + return Future.delayed(const Duration(seconds: 3)); + }, + // Pull from top to show refresh indicator. + child: ListView.builder( + itemCount: 25, + itemBuilder: (BuildContext context, int index) { + return ListTile( + title: Text('Item $index'), + ); + }, + ), + ), + floatingActionButton: FloatingActionButton.extended( + onPressed: () { + // Show refresh indicator programmatically on button tap. + _refreshIndicatorKey.currentState?.show(); + }, + icon: const Icon(Icons.refresh), + label: const Text('Show Indicator'), + ), + ); + } +} diff --git a/examples/api/test/material/refresh_indicator/refresh_indicator.0_test.dart b/examples/api/test/material/refresh_indicator/refresh_indicator.0_test.dart new file mode 100644 index 0000000000..d7e42e7010 --- /dev/null +++ b/examples/api/test/material/refresh_indicator/refresh_indicator.0_test.dart @@ -0,0 +1,35 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/material/refresh_indicator/refresh_indicator.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Trigger RefreshIndicator - Pull from top', (WidgetTester tester) async { + await tester.pumpWidget( + const example.MyApp(), + ); + + await tester.fling(find.text('Item 1'), const Offset(0.0, 300.0), 1000.0); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + await tester.pump(const Duration(seconds: 1)); + expect(tester.getCenter(find.byType(RefreshProgressIndicator)).dy, lessThan(300.0)); + await tester.pumpAndSettle(); // Advance pending time + }); + + testWidgets('Trigger RefreshIndicator - Button', (WidgetTester tester) async { + await tester.pumpWidget( + const example.MyApp(), + ); + + await tester.tap(find.byType(FloatingActionButton)); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + await tester.pump(const Duration(seconds: 1)); + expect(tester.getCenter(find.byType(RefreshProgressIndicator)).dy, lessThan(300.0)); + await tester.pumpAndSettle(); // Advance pending time + }); +} diff --git a/packages/flutter/lib/src/material/refresh_indicator.dart b/packages/flutter/lib/src/material/refresh_indicator.dart index f76e6fe5e0..1fc66271ee 100644 --- a/packages/flutter/lib/src/material/refresh_indicator.dart +++ b/packages/flutter/lib/src/material/refresh_indicator.dart @@ -71,6 +71,12 @@ enum RefreshIndicatorTriggerMode { /// /// The trigger mode is configured by [RefreshIndicator.triggerMode]. /// +/// {@tool dartpad} +/// This example shows how [RefreshIndicator] can be triggered in different ways. +/// +/// ** See code in examples/api/lib/material/refresh_indicator/refresh_indicator.0.dart ** +/// {@end-tool} +/// /// ## Troubleshooting /// /// ### Refresh indicator does not show up