Index
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_colorpicker/flutter_colorpicker.dart'; import 'package:painter/painter.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Painter Example', home: ExamplePage(), ); } } class ExamplePage extends StatefulWidget { @override _ExamplePageState createState() => _ExamplePageState(); } class _ExamplePageState extends State<ExamplePage> { bool _finished = false; PainterController _controller = _newController(); @override void initState() { super.initState(); } static PainterController _newController() { PainterController controller = PainterController(); controller.thickness = 5.0; controller.backgroundColor = Colors.transparent; return controller; } @override Widget build(BuildContext context) { List<Widget> actions; if (_finished) { actions = <Widget>[ IconButton( icon: const Icon(Icons.content_copy), tooltip: 'New Painting', onPressed: () => setState( () { _finished = false; _controller = _newController(); }, ), ), ]; } else { actions = <Widget>[ IconButton( icon: const Icon( Icons.undo, ), tooltip: 'Undo', onPressed: () { if (_controller.isEmpty) { showModalBottomSheet( context: context, builder: (BuildContext context) => const Text('Nothing to undo')); } else { _controller.undo(); } }, ), IconButton( icon: const Icon(Icons.delete), tooltip: 'Clear', onPressed: _controller.clear), IconButton( icon: const Icon(Icons.check), onPressed: () => _show(_controller.finish(), context)), ]; } return Scaffold( appBar: AppBar( title: const Text('Painter Example'), actions: actions, bottom: PreferredSize( preferredSize: Size(MediaQuery.of(context).size.width, 30.0), child: DrawBar(_controller), )), body: Center( child: AspectRatio( aspectRatio: 1.0, child: Stack( children: [ Image.asset("images/komugi.jpeg"), Painter(_controller), ], ), ), ), ); } void _show(PictureDetails picture, BuildContext context) { setState(() { _finished = true; }); Navigator.of(context).push( MaterialPageRoute( builder: (BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('View your image'), ), body: Container( alignment: Alignment.center, child: FutureBuilder<Uint8List>( future: picture.toPNG(), builder: (BuildContext context, AsyncSnapshot<Uint8List> snapshot) { switch (snapshot.connectionState) { case ConnectionState.done: if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { return Image.memory(snapshot.data!); } default: return const FractionallySizedBox( widthFactor: 0.1, alignment: Alignment.center, child: AspectRatio( aspectRatio: 1.0, child: CircularProgressIndicator(), ), ); } }, ), ), ); }, ), ); } } class DrawBar extends StatelessWidget { final PainterController _controller; DrawBar(this._controller); @override Widget build(BuildContext context) { return Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Flexible( child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Slider( value: _controller.thickness, onChanged: (double value) => setState( () { _controller.thickness = value; }, ), min: 1.0, max: 20.0, activeColor: Colors.transparent, ); }, ), ), StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return RotatedBox( quarterTurns: _controller.eraseMode ? 2 : 0, child: IconButton( icon: const Icon(Icons.create), tooltip: '${_controller.eraseMode ? 'Disable' : 'Enable'} eraser', onPressed: () { setState( () { _controller.eraseMode = !_controller.eraseMode; }, ); }, ), ); }, ), ColorPickerButton(_controller, false), ColorPickerButton(_controller, true), ], ); } } class ColorPickerButton extends StatefulWidget { final PainterController _controller; final bool _background; ColorPickerButton(this._controller, this._background); @override _ColorPickerButtonState createState() => _ColorPickerButtonState(); } class _ColorPickerButtonState extends State<ColorPickerButton> { @override Widget build(BuildContext context) { return IconButton( icon: Icon(_iconData, color: _color), tooltip: widget._background ? 'Change background color' : 'Change draw color', onPressed: _pickColor); } void _pickColor() { Color pickerColor = _color; Navigator.of(context) .push( MaterialPageRoute( fullscreenDialog: true, builder: (BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Pick color'), ), body: Container( alignment: Alignment.center, child: 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; } } } |
https://pub.dev/packages/painter
だいぶ更新されていないので、あまり使わない方がいいかもしれませんが、簡単に実装できる。