-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.dart
237 lines (217 loc) · 7.12 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:painter/painter.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Painter Example',
home: new ExamplePage(),
);
}
}
class ExamplePage extends StatefulWidget {
@override
_ExamplePageState createState() => new _ExamplePageState();
}
class _ExamplePageState extends State<ExamplePage> {
bool _finished = false;
PainterController _controller = _newController();
@override
void initState() {
super.initState();
}
static PainterController _newController() {
PainterController controller = new PainterController();
controller.thickness = 5.0;
controller.backgroundColor = Colors.green;
return controller;
}
@override
Widget build(BuildContext context) {
List<Widget> actions;
if (_finished) {
actions = <Widget>[
new IconButton(
icon: new Icon(Icons.content_copy),
tooltip: 'New Painting',
onPressed: () => setState(() {
_finished = false;
_controller = _newController();
}),
),
];
} else {
actions = <Widget>[
new IconButton(
icon: new Icon(
Icons.undo,
),
tooltip: 'Undo',
onPressed: () {
if (_controller.isEmpty) {
showModalBottomSheet(
context: context,
builder: (BuildContext context) =>
new Text('Nothing to undo'));
} else {
_controller.undo();
}
}),
new IconButton(
icon: new Icon(Icons.delete),
tooltip: 'Clear',
onPressed: _controller.clear),
new IconButton(
icon: new Icon(Icons.check),
onPressed: () => _show(_controller.finish(), context)),
];
}
return new Scaffold(
appBar: new AppBar(
title: const Text('Painter Example'),
actions: actions,
bottom: new PreferredSize(
child: new DrawBar(_controller),
preferredSize: new Size(MediaQuery.of(context).size.width, 30.0),
)),
body: new Center(
child: new AspectRatio(
aspectRatio: 1.0, child: new Painter(_controller))),
);
}
void _show(PictureDetails picture, BuildContext context) {
setState(() {
_finished = true;
});
Navigator.of(context)
.push(new MaterialPageRoute(builder: (BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('View your image'),
),
body: new Container(
alignment: Alignment.center,
child: new FutureBuilder<Uint8List>(
future: picture.toPNG(),
builder:
(BuildContext context, AsyncSnapshot<Uint8List> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.done:
if (snapshot.hasError) {
return new Text('Error: ${snapshot.error}');
} else {
return Image.memory(snapshot.data!);
}
default:
return new Container(
child: new FractionallySizedBox(
widthFactor: 0.1,
child: new AspectRatio(
aspectRatio: 1.0,
child: new CircularProgressIndicator()),
alignment: Alignment.center,
));
}
},
)),
);
}));
}
}
class DrawBar extends StatelessWidget {
final PainterController _controller;
DrawBar(this._controller);
@override
Widget build(BuildContext context) {
return new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Flexible(child: new StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return new Container(
child: new Slider(
value: _controller.thickness,
onChanged: (double value) => setState(() {
_controller.thickness = value;
}),
min: 1.0,
max: 20.0,
activeColor: Colors.white,
));
})),
new StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return new RotatedBox(
quarterTurns: _controller.eraseMode ? 2 : 0,
child: IconButton(
icon: new Icon(Icons.create),
tooltip: (_controller.eraseMode ? 'Disable' : 'Enable') +
' eraser',
onPressed: () {
setState(() {
_controller.eraseMode = !_controller.eraseMode;
});
}));
}),
new ColorPickerButton(_controller, false),
new ColorPickerButton(_controller, true),
],
);
}
}
class ColorPickerButton extends StatefulWidget {
final PainterController _controller;
final bool _background;
ColorPickerButton(this._controller, this._background);
@override
_ColorPickerButtonState createState() => new _ColorPickerButtonState();
}
class _ColorPickerButtonState extends State<ColorPickerButton> {
@override
Widget build(BuildContext context) {
return new IconButton(
icon: new Icon(_iconData, color: _color),
tooltip: widget._background
? 'Change background color'
: 'Change draw color',
onPressed: _pickColor);
}
void _pickColor() {
Color pickerColor = _color;
Navigator.of(context)
.push(new MaterialPageRoute(
fullscreenDialog: true,
builder: (BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('Pick color'),
),
body: new Container(
alignment: Alignment.center,
child: new ColorPicker(
pickerColor: pickerColor,
onColorChanged: (Color c) => pickerColor = c,
)));
}))
.then((_) {
setState(() {
_color = pickerColor;
});
});
}
Color get _color => widget._background
? widget._controller.backgroundColor
: widget._controller.drawColor;
IconData get _iconData =>
widget._background ? Icons.format_color_fill : Icons.brush;
set _color(Color color) {
if (widget._background) {
widget._controller.backgroundColor = color;
} else {
widget._controller.drawColor = color;
}
}
}