-
Notifications
You must be signed in to change notification settings - Fork 57
/
stickers_example.dart
159 lines (148 loc) · 5 KB
/
stickers_example.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
159
// Flutter imports:
import 'package:flutter/material.dart';
// Package imports:
import 'package:pro_image_editor/pro_image_editor.dart';
// Project imports:
import '../utils/example_helper.dart';
class StickersExample extends StatefulWidget {
const StickersExample({super.key});
@override
State<StickersExample> createState() => _StickersExampleState();
}
class _StickersExampleState extends State<StickersExample>
with ExampleHelperState<StickersExample> {
final String _url = 'https://picsum.photos/id/176/2000';
@override
Widget build(BuildContext context) {
return ListTile(
onTap: () async {
LoadingDialog.instance.show(
context,
configs: const ProImageEditorConfigs(),
theme: ThemeData.dark(),
);
await precacheImage(NetworkImage(_url), context);
LoadingDialog.instance.hide();
if (!context.mounted) return;
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => _buildEditor(),
),
);
},
leading: const Icon(Icons.image_outlined),
title: const Text('Custom Stickers'),
trailing: const Icon(Icons.chevron_right),
);
}
Widget _buildEditor() {
return ProImageEditor.network(
_url,
callbacks: ProImageEditorCallbacks(
onImageEditingStarted: onImageEditingStarted,
onImageEditingComplete: onImageEditingComplete,
onCloseEditor: onCloseEditor,
),
configs: ProImageEditorConfigs(
designMode: platformDesignMode,
blurEditorConfigs: const BlurEditorConfigs(enabled: false),
stickerEditorConfigs: StickerEditorConfigs(
enabled: true,
buildStickers: (setLayer, scrollController) {
return ClipRRect(
borderRadius:
const BorderRadius.vertical(top: Radius.circular(20)),
child: GridView.builder(
padding: const EdgeInsets.all(16),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 80,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
),
controller: scrollController,
itemCount: 21,
shrinkWrap: true,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () async {
// Important make sure the image is completely loaded
// cuz the editor will directly take a screenshot
// inside of a background isolated thread.
LoadingDialog.instance.show(
context,
configs: const ProImageEditorConfigs(),
theme: ThemeData.dark(),
);
await precacheImage(
NetworkImage(
'https://picsum.photos/id/${(index + 3) * 3}/2000'),
context);
LoadingDialog.instance.hide();
setLayer(Sticker(index: index));
},
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: Sticker(index: index),
),
);
},
),
);
},
),
),
);
}
}
class Sticker extends StatefulWidget {
final int index;
const Sticker({
super.key,
required this.index,
});
@override
State<Sticker> createState() => StickerState();
}
class StickerState extends State<Sticker> {
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(7),
child: Image.network(
'https://picsum.photos/id/${(widget.index + 3) * 3}/2000',
width: 80,
height: 80,
fit: BoxFit.cover,
loadingBuilder: (context, child, loadingProgress) {
return AnimatedSwitcher(
layoutBuilder: (currentChild, previousChildren) {
return SizedBox(
width: 80,
height: 80,
child: Stack(
fit: StackFit.expand,
alignment: Alignment.center,
children: <Widget>[
...previousChildren,
if (currentChild != null) currentChild,
],
),
);
},
duration: const Duration(milliseconds: 200),
child: loadingProgress == null
? child
: Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
),
),
);
},
),
);
}
}