Skip to content

Commit 2195c4a

Browse files
committed
[Mobile] Support custom image size: make margin optional and support alignment
1 parent c932c80 commit 2195c4a

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ It is required to provide `filePickImpl` for toolbar image button, e.g. [Sample
9898

9999
## Custom Size Image for Mobile
100100

101-
Define `mobileWidth`, `mobileHeight` and `mobileMargin` as follows:
101+
Define `mobileWidth`, `mobileHeight`, `mobileMargin`, `mobileAlignment` as follows:
102102
```
103103
{
104104
"insert": {
105105
"image": "https://user-images.githubusercontent.com/122956/72955931-ccc07900-3d52-11ea-89b1-d468a6e2aa2b.png"
106106
},
107107
"attributes":{
108-
"style":"mobileWidth: 50; mobileHeight: 50; mobileMargin: 10;"
108+
"style":"mobileWidth: 50; mobileHeight: 50; mobileMargin: 10; mobileAlignment: topLeft"
109109
}
110110
}
111111
```

example/assets/sample_data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"attributes":{
77
"width":"230",
8-
"style":"display: block; margin: auto; mobileWidth: 50; mobileHeight: 50; mobileMargin: 10;"
8+
"style":"display: block; margin: auto; mobileWidth: 50; mobileHeight: 50; mobileMargin: 10; mobileAlignment: topLeft"
99
}
1010
},
1111
{

lib/src/widgets/editor.dart

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,52 @@ Widget _defaultEmbedBuilder(
110110
final style = node.style.attributes['style'];
111111
if (_isMobile() && style != null) {
112112
final _attrs = parseKeyValuePairs(style.value.toString(),
113-
{'mobileWidth', 'mobileHeight', 'mobileMargin'});
113+
{'mobileWidth', 'mobileHeight', 'mobileMargin', 'mobileAlignment'});
114114
if (_attrs.isNotEmpty) {
115-
assert(_attrs.length == 3,
116-
'mobileWidth, mobileHeight, mobileMargin must be specified');
115+
assert(
116+
_attrs['mobileWidth'] != null && _attrs['mobileHeight'] != null,
117+
'mobileWidth and mobileHeight must be specified');
117118
final w = double.parse(_attrs['mobileWidth']!);
118119
final h = double.parse(_attrs['mobileHeight']!);
119-
final m = double.parse(_attrs['mobileMargin']!);
120+
final m = _attrs['mobileMargin'] == null
121+
? 0.0
122+
: double.parse(_attrs['mobileMargin']!);
123+
var a = Alignment.center;
124+
if (_attrs['mobileAlignment'] != null) {
125+
final _index = [
126+
'topLeft',
127+
'topCenter',
128+
'topRight',
129+
'centerLeft',
130+
'center',
131+
'centerRight',
132+
'bottomLeft',
133+
'bottomCenter',
134+
'bottomRight'
135+
].indexOf(_attrs['mobileAlignment']!);
136+
if (_index >= 0) {
137+
a = [
138+
Alignment.topLeft,
139+
Alignment.topCenter,
140+
Alignment.topRight,
141+
Alignment.centerLeft,
142+
Alignment.center,
143+
Alignment.centerRight,
144+
Alignment.bottomLeft,
145+
Alignment.bottomCenter,
146+
Alignment.bottomRight
147+
][_index];
148+
}
149+
}
120150
return Padding(
121151
padding: EdgeInsets.all(m),
122152
child: imageUrl.startsWith('http')
123-
? Image.network(imageUrl, width: w, height: h)
153+
? Image.network(imageUrl, width: w, height: h, alignment: a)
124154
: isBase64(imageUrl)
125155
? Image.memory(base64.decode(imageUrl),
126-
width: w, height: h)
127-
: Image.file(io.File(imageUrl), width: w, height: h));
156+
width: w, height: h, alignment: a)
157+
: Image.file(io.File(imageUrl),
158+
width: w, height: h, alignment: a));
128159
}
129160
}
130161
return imageUrl.startsWith('http')

0 commit comments

Comments
 (0)