From 2195c4ae3eed8a3f7688633710fd1f3c27efb29b Mon Sep 17 00:00:00 2001 From: Xin Yao Date: Wed, 4 Aug 2021 11:34:35 -0700 Subject: [PATCH] [Mobile] Support custom image size: make margin optional and support alignment --- README.md | 4 +-- example/assets/sample_data.json | 2 +- lib/src/widgets/editor.dart | 45 ++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 98480679..4d5a2756 100644 --- a/README.md +++ b/README.md @@ -98,14 +98,14 @@ It is required to provide `filePickImpl` for toolbar image button, e.g. [Sample ## Custom Size Image for Mobile -Define `mobileWidth`, `mobileHeight` and `mobileMargin` as follows: +Define `mobileWidth`, `mobileHeight`, `mobileMargin`, `mobileAlignment` as follows: ``` { "insert": { "image": "https://user-images.githubusercontent.com/122956/72955931-ccc07900-3d52-11ea-89b1-d468a6e2aa2b.png" }, "attributes":{ - "style":"mobileWidth: 50; mobileHeight: 50; mobileMargin: 10;" + "style":"mobileWidth: 50; mobileHeight: 50; mobileMargin: 10; mobileAlignment: topLeft" } } ``` diff --git a/example/assets/sample_data.json b/example/assets/sample_data.json index c89aa0a9..bf7b4881 100644 --- a/example/assets/sample_data.json +++ b/example/assets/sample_data.json @@ -5,7 +5,7 @@ }, "attributes":{ "width":"230", - "style":"display: block; margin: auto; mobileWidth: 50; mobileHeight: 50; mobileMargin: 10;" + "style":"display: block; margin: auto; mobileWidth: 50; mobileHeight: 50; mobileMargin: 10; mobileAlignment: topLeft" } }, { diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart index 5c3da465..5f3e80f1 100644 --- a/lib/src/widgets/editor.dart +++ b/lib/src/widgets/editor.dart @@ -110,21 +110,52 @@ Widget _defaultEmbedBuilder( final style = node.style.attributes['style']; if (_isMobile() && style != null) { final _attrs = parseKeyValuePairs(style.value.toString(), - {'mobileWidth', 'mobileHeight', 'mobileMargin'}); + {'mobileWidth', 'mobileHeight', 'mobileMargin', 'mobileAlignment'}); if (_attrs.isNotEmpty) { - assert(_attrs.length == 3, - 'mobileWidth, mobileHeight, mobileMargin must be specified'); + assert( + _attrs['mobileWidth'] != null && _attrs['mobileHeight'] != null, + 'mobileWidth and mobileHeight must be specified'); final w = double.parse(_attrs['mobileWidth']!); final h = double.parse(_attrs['mobileHeight']!); - final m = double.parse(_attrs['mobileMargin']!); + final m = _attrs['mobileMargin'] == null + ? 0.0 + : double.parse(_attrs['mobileMargin']!); + var a = Alignment.center; + if (_attrs['mobileAlignment'] != null) { + final _index = [ + 'topLeft', + 'topCenter', + 'topRight', + 'centerLeft', + 'center', + 'centerRight', + 'bottomLeft', + 'bottomCenter', + 'bottomRight' + ].indexOf(_attrs['mobileAlignment']!); + if (_index >= 0) { + a = [ + Alignment.topLeft, + Alignment.topCenter, + Alignment.topRight, + Alignment.centerLeft, + Alignment.center, + Alignment.centerRight, + Alignment.bottomLeft, + Alignment.bottomCenter, + Alignment.bottomRight + ][_index]; + } + } return Padding( padding: EdgeInsets.all(m), child: imageUrl.startsWith('http') - ? Image.network(imageUrl, width: w, height: h) + ? Image.network(imageUrl, width: w, height: h, alignment: a) : isBase64(imageUrl) ? Image.memory(base64.decode(imageUrl), - width: w, height: h) - : Image.file(io.File(imageUrl), width: w, height: h)); + width: w, height: h, alignment: a) + : Image.file(io.File(imageUrl), + width: w, height: h, alignment: a)); } } return imageUrl.startsWith('http')