-
Notifications
You must be signed in to change notification settings - Fork 67
/
programmatically_controlled_indicator_screen.dart
100 lines (91 loc) · 2.95 KB
/
programmatically_controlled_indicator_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import 'dart:math';
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
import 'package:example/indicators/warp_indicator.dart';
import 'package:example/widgets/example_app_bar.dart';
import 'package:example/widgets/example_list.dart';
import 'package:flutter/material.dart';
class ProgrammaticallyControlled extends StatefulWidget {
const ProgrammaticallyControlled({super.key});
@override
State<ProgrammaticallyControlled> createState() =>
_ProgrammaticallyControlledState();
}
class _ProgrammaticallyControlledState
extends State<ProgrammaticallyControlled> {
final key = GlobalKey<CustomRefreshIndicatorState>();
final controller = IndicatorController(refreshEnabled: false);
bool _startedManually = false;
int _itemsCount = 4;
Future<void> onRefresh() async {
await Future.delayed(const Duration(seconds: 2));
if (!mounted) return;
setState(() {
_itemsCount = Random().nextInt(4) + 1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: ExampleAppBar(
title: "Programmatically-controlled",
actions: [
AnimatedBuilder(
animation: controller,
builder: (context, _) {
return Row(
children: [
IconButton(
onPressed: controller.isIdle
? () {
key.currentState!.show(
draggingCurve: Curves.easeOutBack,
);
_startedManually = true;
}
: null,
icon: const Icon(Icons.play_arrow),
),
IconButton(
onPressed: controller.isIdle
? () => key.currentState!.refresh(
draggingCurve: Curves.easeOutBack,
)
: null,
icon: const Icon(Icons.refresh),
),
IconButton(
onPressed: controller.isLoading && _startedManually
? () {
key.currentState!.hide();
_startedManually = false;
}
: null,
icon: const Icon(Icons.stop),
),
],
);
},
),
],
),
body: WarpIndicator(
controller: controller,
indicatorKey: key,
onRefresh: onRefresh,
child: ExampleList(
leading: const ListHelpBox(
child: Text(
'You can use the buttons located in the app bar to control the state of the indicator.',
),
),
itemCount: _itemsCount,
),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}