-
Notifications
You must be signed in to change notification settings - Fork 10
/
custom_gif_refresher.dart
158 lines (135 loc) · 3.87 KB
/
custom_gif_refresher.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import 'dart:convert';
import 'package:flutter/material.dart' hide RefreshIndicator, RefreshIndicatorState;
import 'package:flutter/widgets.dart';
import 'package:flutter_gifimage/flutter_gifimage.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
class CustomGifHeader extends RefreshIndicator {
CustomGifHeader(this.headerBase64, this.topFrameCount)
: super(height: recommendedHeight, refreshStyle: RefreshStyle.Follow);
final String headerBase64;
final int topFrameCount;
static double recommendedHeight = 80;
@override
State<StatefulWidget> createState() {
return CustomGifHeaderState();
}
}
class CustomGifHeaderState extends RefreshIndicatorState<CustomGifHeader> with SingleTickerProviderStateMixin {
GifController _gifController;
@override
void initState() {
_gifController = GifController(
vsync: this,
value: 0,
);
super.initState();
}
@override
void onModeChange(RefreshStatus mode) {
if ((mode == RefreshStatus.canRefresh) ||
(mode == RefreshStatus.refreshing && _gifController.isAnimating == false)) {
_gifController.repeat(
min: 0,
max: widget.topFrameCount * 1.0,
period: Duration(milliseconds: (widget.topFrameCount / 24 * 1000).floor()),
);
}
super.onModeChange(mode);
}
@override
Future<void> endRefresh() {
return super.endRefresh();
}
@override
void resetValue() {
_gifController.reset();
super.resetValue();
}
@override
Widget buildContent(BuildContext context, RefreshStatus mode) {
return GifImage(
image: MemoryImage(
base64.decode(widget.headerBase64),
),
controller: _gifController,
height: 80.0,
);
}
@override
void dispose() {
_gifController.dispose();
super.dispose();
}
}
class CustomGifFooter extends LoadIndicator {
final VoidCallback onClick;
final int bottomFrameCount;
CustomGifFooter(this.footerBase64, this.bottomFrameCount, {this.onClick})
: super(onClick: onClick, loadStyle: LoadStyle.ShowWhenLoading, height: recommendedHeight);
final String footerBase64;
static double recommendedHeight = 80;
@override
State<StatefulWidget> createState() {
return _CustomGifFooterState();
}
}
class _CustomGifFooterState extends LoadIndicatorState<CustomGifFooter> with SingleTickerProviderStateMixin {
GifController _gifController;
bool started;
@override
void initState() {
started = false;
_gifController = GifController(
vsync: this,
value: 0,
);
super.initState();
}
@override
void dispose() {
_gifController.dispose();
super.dispose();
}
@override
void onModeChange(LoadStatus mode) {
if (mode == LoadStatus.idle && started) {
started = false;
_gifController.value = 0;
} else if (mode == LoadStatus.loading && !started) {
started = true;
if (!_gifController.isAnimating) {
_gifController.repeat(
min: 0,
max: widget.bottomFrameCount * 1.0,
period: Duration(milliseconds: (widget.bottomFrameCount / 24 * 1000).floor()));
}
} else if (mode == LoadStatus.canLoading) {
started = true;
if (!_gifController.isAnimating) {
_gifController.repeat(
min: 0,
max: widget.bottomFrameCount * 1.0,
period: Duration(milliseconds: (widget.bottomFrameCount / 24 * 1000).floor()));
}
}
super.onModeChange(mode);
}
@override
Future endLoading() {
if (_gifController.isAnimating) {
started = false;
_gifController.reset();
}
return super.endLoading();
}
@override
Widget buildContent(BuildContext context, LoadStatus mode) {
return !started
? Container()
: GifImage(
image: MemoryImage(base64.decode(widget.footerBase64)),
controller: _gifController,
height: 80.0,
);
}
}