|
@@ -1,23 +1,126 @@
|
1
|
1
|
// Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
|
2
|
2
|
// for details. All rights reserved. Use of this source code is governed by a
|
3
|
3
|
// BSD-style license that can be found in the LICENSE file.
|
|
4
|
+import 'dart:async';
|
|
5
|
+import 'dart:io';
|
4
|
6
|
import 'dart:math' as math;
|
5
|
7
|
import 'dart:ui' as ui;
|
6
|
8
|
|
7
|
9
|
import 'package:flutter/rendering.dart';
|
8
|
10
|
import 'package:flutter/widgets.dart';
|
9
|
11
|
import 'package:notus/notus.dart';
|
|
12
|
+import 'package:image_picker/image_picker.dart';
|
10
|
13
|
|
11
|
14
|
import 'editable_box.dart';
|
12
|
15
|
|
13
|
|
-class EditableImage extends LeafRenderObjectWidget {
|
14
|
|
- EditableImage({@required this.node}) : assert(node != null);
|
|
16
|
+abstract class ZefyrImageDelegate<S> {
|
|
17
|
+ /// Creates [ImageProvider] for specified [imageSource].
|
|
18
|
+ ImageProvider createImageProvider(String imageSource);
|
|
19
|
+
|
|
20
|
+ /// Picks an image from specified [source].
|
|
21
|
+ ///
|
|
22
|
+ /// Returns unique string key for the selected image. Returned key is stored
|
|
23
|
+ /// in the document.
|
|
24
|
+ Future<String> pickImage(S source);
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+class ZefyrDefaultImageDelegate implements ZefyrImageDelegate<ImageSource> {
|
|
28
|
+ @override
|
|
29
|
+ ImageProvider createImageProvider(String imageSource) {
|
|
30
|
+ final file = new File.fromUri(Uri.parse(imageSource));
|
|
31
|
+ return new FileImage(file);
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ @override
|
|
35
|
+ Future<String> pickImage(ImageSource source) async {
|
|
36
|
+ final file = await ImagePicker.pickImage(source: source);
|
|
37
|
+ if (file == null) return null;
|
|
38
|
+ return file.uri.toString();
|
|
39
|
+ }
|
|
40
|
+}
|
|
41
|
+
|
|
42
|
+class ImageEmbed extends StatefulWidget {
|
|
43
|
+ const ImageEmbed({Key key, @required this.node, @required this.delegate})
|
|
44
|
+ : super(key: key);
|
|
45
|
+
|
|
46
|
+ final EmbedNode node;
|
|
47
|
+ final ZefyrImageDelegate delegate;
|
|
48
|
+
|
|
49
|
+ @override
|
|
50
|
+ _ImageEmbedState createState() => _ImageEmbedState();
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+class _ImageEmbedState extends State<ImageEmbed> {
|
|
54
|
+ ImageProvider _provider;
|
|
55
|
+ ImageStream _imageStream;
|
|
56
|
+ ImageInfo _imageInfo;
|
|
57
|
+
|
|
58
|
+ @override
|
|
59
|
+ void initState() {
|
|
60
|
+ super.initState();
|
|
61
|
+ EmbedAttribute attribute = widget.node.style.get(NotusAttribute.embed);
|
|
62
|
+ final source = attribute.value['source'];
|
|
63
|
+ _provider = widget.delegate.createImageProvider(source);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ @override
|
|
67
|
+ void didChangeDependencies() {
|
|
68
|
+ super.didChangeDependencies();
|
|
69
|
+ _getImage();
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ @override
|
|
73
|
+ void didUpdateWidget(ImageEmbed oldWidget) {
|
|
74
|
+ super.didUpdateWidget(oldWidget);
|
|
75
|
+ EmbedAttribute oldStyle = oldWidget.node.style.get(NotusAttribute.embed);
|
|
76
|
+ final oldSource = oldStyle.value['source'];
|
|
77
|
+ EmbedAttribute style = widget.node.style.get(NotusAttribute.embed);
|
|
78
|
+ final source = style.value['source'];
|
|
79
|
+ if (oldSource != source || oldWidget.delegate != widget.delegate) {
|
|
80
|
+ _provider = widget.delegate.createImageProvider(source);
|
|
81
|
+ _getImage();
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ @override
|
|
86
|
+ void dispose() {
|
|
87
|
+ _imageStream.removeListener(_updateImage);
|
|
88
|
+ super.dispose();
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ @override
|
|
92
|
+ Widget build(BuildContext context) {
|
|
93
|
+ return _EditableImage(
|
|
94
|
+ node: widget.node,
|
|
95
|
+ image: _imageInfo?.image,
|
|
96
|
+ );
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ void _getImage() {
|
|
100
|
+ final oldImageStream = _imageStream;
|
|
101
|
+ _imageStream = _provider.resolve(createLocalImageConfiguration(context));
|
|
102
|
+ if (_imageStream.key != oldImageStream?.key) {
|
|
103
|
+ oldImageStream?.removeListener(_updateImage);
|
|
104
|
+ _imageStream.addListener(_updateImage);
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ void _updateImage(ImageInfo imageInfo, bool synchronousCall) {
|
|
109
|
+ setState(() {
|
|
110
|
+ _imageInfo = imageInfo;
|
|
111
|
+ });
|
|
112
|
+ }
|
|
113
|
+}
|
|
114
|
+
|
|
115
|
+class _EditableImage extends LeafRenderObjectWidget {
|
|
116
|
+ _EditableImage({@required this.node, this.image}) : assert(node != null);
|
15
|
117
|
|
16
|
118
|
final EmbedNode node;
|
|
119
|
+ final ui.Image image;
|
17
|
120
|
|
18
|
121
|
@override
|
19
|
122
|
RenderEditableImage createRenderObject(BuildContext context) {
|
20
|
|
- return new RenderEditableImage(node: node);
|
|
123
|
+ return new RenderEditableImage(node: node, image: image);
|
21
|
124
|
}
|
22
|
125
|
|
23
|
126
|
@override
|
|
@@ -35,9 +138,6 @@ class RenderEditableImage extends RenderImage implements RenderEditableBox {
|
35
|
138
|
super(
|
36
|
139
|
image: image,
|
37
|
140
|
);
|
38
|
|
- //
|
39
|
|
- // Public members
|
40
|
|
- //
|
41
|
141
|
|
42
|
142
|
@override
|
43
|
143
|
EmbedNode get node => _node;
|