parent
b6390153b0
commit
341b9b015d
9 changed files with 87 additions and 77 deletions
@ -1,84 +1,28 @@ |
|||||||
import 'dart:collection'; |
|
||||||
|
|
||||||
import 'package:collection/collection.dart'; |
|
||||||
import 'package:quiver_hashcode/hashcode.dart'; |
|
||||||
|
|
||||||
class Embeddable { |
class Embeddable { |
||||||
static const TYPE_KEY = '_type'; |
|
||||||
static const INLINE_KEY = '_inline'; |
|
||||||
final String type; |
final String type; |
||||||
final bool inline; |
final dynamic data; |
||||||
final Map<String, dynamic> _data; |
|
||||||
|
|
||||||
Embeddable(this.type, this.inline, Map<String, dynamic> data) |
Embeddable(this.type, this.data) |
||||||
: assert(type != null), |
: assert(type != null), |
||||||
assert(inline != null), |
assert(data != null); |
||||||
assert(!data.containsKey(TYPE_KEY)), |
|
||||||
assert(!data.containsKey(INLINE_KEY)), |
|
||||||
_data = Map.from(data); |
|
||||||
|
|
||||||
Map<String, dynamic> get data => UnmodifiableMapView(_data); |
|
||||||
|
|
||||||
Map<String, dynamic> toJson() { |
Map<String, dynamic> toJson() { |
||||||
Map<String, dynamic> m = Map<String, dynamic>.from(_data); |
Map<String, String> m = {type: data}; |
||||||
m[TYPE_KEY] = type; |
|
||||||
m[INLINE_KEY] = inline; |
|
||||||
return m; |
return m; |
||||||
} |
} |
||||||
|
|
||||||
static Embeddable fromJson(Map<String, dynamic> json) { |
static Embeddable fromJson(Map<String, dynamic> json) { |
||||||
String type = json[TYPE_KEY] as String; |
Map<String, dynamic> m = Map<String, dynamic>.from(json); |
||||||
bool inline = json[INLINE_KEY] as bool; |
assert(m.length == 1, 'Embeddable map has one key'); |
||||||
Map<String, dynamic> data = Map<String, dynamic>.from(json); |
|
||||||
data.remove(TYPE_KEY); |
|
||||||
data.remove(INLINE_KEY); |
|
||||||
if (inline) { |
|
||||||
return Span(type, data: data); |
|
||||||
} |
|
||||||
return BlockEmbed(type, data: data); |
|
||||||
} |
|
||||||
|
|
||||||
@override |
|
||||||
bool operator ==(dynamic other) { |
|
||||||
if (identical(this, other)) { |
|
||||||
return true; |
|
||||||
} |
|
||||||
if (other is! Embeddable) { |
|
||||||
return false; |
|
||||||
} |
|
||||||
final typedOther = other; |
|
||||||
return typedOther.type == type && |
|
||||||
typedOther.inline == inline && |
|
||||||
DeepCollectionEquality().equals(typedOther._data, _data); |
|
||||||
} |
|
||||||
|
|
||||||
@override |
|
||||||
int get hashCode { |
|
||||||
if (_data.isEmpty) { |
|
||||||
return hash2(type, inline); |
|
||||||
} |
|
||||||
|
|
||||||
final dataHash = hashObjects( |
return BlockEmbed(m.keys.first, m.values.first); |
||||||
_data.entries.map((e) => hash2(e.key, e.value)), |
|
||||||
); |
|
||||||
return hash3(type, inline, dataHash); |
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
class Span extends Embeddable { |
|
||||||
Span( |
|
||||||
String type, { |
|
||||||
Map<String, dynamic> data = const {}, |
|
||||||
}) : super(type, true, data); |
|
||||||
} |
|
||||||
|
|
||||||
class BlockEmbed extends Embeddable { |
class BlockEmbed extends Embeddable { |
||||||
BlockEmbed( |
BlockEmbed(String type, String data) : super(type, data); |
||||||
String type, { |
|
||||||
Map<String, dynamic> data = const {}, |
static final BlockEmbed horizontalRule = BlockEmbed('divider', 'hr'); |
||||||
}) : super(type, false, data); |
|
||||||
|
|
||||||
static final BlockEmbed horizontalRule = BlockEmbed('hr'); |
static BlockEmbed image(String imageUrl) => BlockEmbed('image', imageUrl); |
||||||
static BlockEmbed image(String source) => |
|
||||||
BlockEmbed('image', data: {'source': source}); |
|
||||||
} |
} |
||||||
|
@ -0,0 +1,31 @@ |
|||||||
|
import 'package:flutter/cupertino.dart'; |
||||||
|
import 'package:flutter/material.dart'; |
||||||
|
import 'package:flutter/rendering.dart'; |
||||||
|
import 'package:photo_view/photo_view.dart'; |
||||||
|
|
||||||
|
class ImageTapWrapper extends StatelessWidget { |
||||||
|
const ImageTapWrapper({ |
||||||
|
this.imageProvider, |
||||||
|
}); |
||||||
|
|
||||||
|
final ImageProvider imageProvider; |
||||||
|
|
||||||
|
@override |
||||||
|
Widget build(BuildContext context) { |
||||||
|
return Scaffold( |
||||||
|
body: Container( |
||||||
|
constraints: BoxConstraints.expand( |
||||||
|
height: MediaQuery.of(context).size.height, |
||||||
|
), |
||||||
|
child: GestureDetector( |
||||||
|
onTapDown: (_) { |
||||||
|
Navigator.pop(context); |
||||||
|
}, |
||||||
|
child: PhotoView( |
||||||
|
imageProvider: imageProvider, |
||||||
|
), |
||||||
|
), |
||||||
|
), |
||||||
|
); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue