Skip to content

Commit 28ef625

Browse files
committed
Add programmatically-controlled screen example
1 parent ebcba9a commit 28ef625

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

example/lib/main.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:example/screens/presentation_screen.dart';
2+
import 'package:example/screens/programmatically_controlled_indicator_screen.dart';
23
import 'package:example/widgets/web_frame.dart';
34
import 'package:flutter/material.dart';
45

@@ -30,6 +31,8 @@ class MyApp extends StatelessWidget {
3031
'/presentation': (context) => PresentationScreen(),
3132
'/check-mark': (context) => CheckMarkIndicatorScreen(),
3233
'/warp': (context) => WarpIndicatorScreen(),
34+
'/programmatically-controlled': (context) =>
35+
ProgrammaticallyControlled(),
3336
},
3437
);
3538
}
@@ -132,6 +135,18 @@ class MainScreen extends StatelessWidget {
132135
arguments: simpleIndicator,
133136
),
134137
),
138+
const SizedBox(height: 15),
139+
ElevatedButton(
140+
child: Container(
141+
height: 50,
142+
alignment: Alignment.center,
143+
child: Text("Programmatically-controlled warp"),
144+
),
145+
onPressed: () => Navigator.pushNamed(
146+
context,
147+
'/programmatically-controlled',
148+
),
149+
),
135150
],
136151
),
137152
),
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import 'dart:math';
2+
3+
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
4+
import 'package:example/indicators/warp_indicator.dart';
5+
import 'package:example/widgets/example_list.dart';
6+
import 'package:flutter/material.dart';
7+
8+
class ProgrammaticallyControlled extends StatefulWidget {
9+
const ProgrammaticallyControlled({Key? key}) : super(key: key);
10+
11+
@override
12+
State<ProgrammaticallyControlled> createState() =>
13+
_ProgrammaticallyControlledState();
14+
}
15+
16+
class _ProgrammaticallyControlledState
17+
extends State<ProgrammaticallyControlled> {
18+
final key = GlobalKey<CustomRefreshIndicatorState>();
19+
20+
final controller = IndicatorController(refreshEnabled: false);
21+
22+
bool _startedManually = false;
23+
24+
int _itemsCount = 4;
25+
Future<void> onRefresh() async {
26+
await Future.delayed(Duration(seconds: 2));
27+
setState(() {
28+
_itemsCount = Random().nextInt(4) + 1;
29+
});
30+
}
31+
32+
@override
33+
Widget build(BuildContext context) {
34+
return Scaffold(
35+
appBar: AppBar(
36+
title: Text("Programmatically-controlled"),
37+
actions: [
38+
AnimatedBuilder(
39+
animation: controller,
40+
builder: (context, _) {
41+
return Row(
42+
children: [
43+
IconButton(
44+
onPressed: controller.isIdle
45+
? () {
46+
key.currentState!.show(
47+
draggingCurve: Curves.easeOutBack,
48+
);
49+
_startedManually = true;
50+
}
51+
: null,
52+
icon: const Icon(Icons.play_arrow),
53+
),
54+
IconButton(
55+
onPressed: controller.isIdle
56+
? () => key.currentState!.refresh(
57+
draggingCurve: Curves.easeOutBack,
58+
)
59+
: null,
60+
icon: const Icon(Icons.refresh),
61+
),
62+
IconButton(
63+
onPressed: controller.isLoading && _startedManually
64+
? () {
65+
key.currentState!.hide();
66+
_startedManually = false;
67+
}
68+
: null,
69+
icon: const Icon(Icons.stop),
70+
),
71+
],
72+
);
73+
},
74+
),
75+
],
76+
),
77+
body: WarpIndicator(
78+
controller: controller,
79+
indicatorKey: key,
80+
onRefresh: onRefresh,
81+
child: ExampleList(itemCount: _itemsCount),
82+
),
83+
);
84+
}
85+
}

example/lib/widgets/example_list.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import 'package:flutter/material.dart';
33
import 'example_app_bar.dart';
44

55
class ExampleList extends StatelessWidget {
6+
final int itemCount;
7+
68
final Color backgroundColor;
7-
const ExampleList([this.backgroundColor = appBackgroundColor]);
9+
const ExampleList({
10+
this.backgroundColor = appBackgroundColor,
11+
this.itemCount = 4,
12+
});
813
@override
914
Widget build(BuildContext context) {
1015
return DecoratedBox(
@@ -21,7 +26,7 @@ class ExampleList extends StatelessWidget {
2126
parent: ClampingScrollPhysics(),
2227
),
2328
itemBuilder: (BuildContext context, int index) => const Element(),
24-
itemCount: 4,
29+
itemCount: itemCount,
2530
separatorBuilder: (BuildContext context, int index) => const Divider(
2631
height: 0,
2732
color: Color(0xFFe2d6ce),

0 commit comments

Comments
 (0)