Last active
April 13, 2020 02:01
-
-
Save quangquy87/183227a10aabd6a3850f5a325dde65b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:isolate'; | |
import 'package:flutter/material.dart'; | |
import 'package:image/image.dart' as IMG; | |
import 'package:path/path.dart'; | |
class Thumbnail extends StatefulWidget { | |
final Size size; | |
final File image; | |
const Thumbnail({Key key, this.size, this.image}) : super(key: key); | |
@override | |
_ThumbnailState createState() => _ThumbnailState(); | |
} | |
class _ThumbnailState extends State<Thumbnail> { | |
List<int> imgBytes; | |
Isolate isolate; | |
@override | |
void initState() { | |
_asyncInit(); | |
super.initState(); | |
} | |
static _isolateEntry(dynamic d) async { | |
final ReceivePort receivePort = ReceivePort(); | |
d.send(receivePort.sendPort); | |
final config = await receivePort.first; | |
print(config); | |
final file = File(config['path']); | |
final bytes = await file.readAsBytes(); | |
IMG.Image image = IMG.decodeImage(bytes); | |
IMG.Image thumbnail = IMG.copyResize( | |
image, | |
width: config['size'].width.toInt(), | |
); | |
d.send(IMG.encodeNamedImage(thumbnail, basename(config['path']))); | |
} | |
_asyncInit() async { | |
final ReceivePort receivePort = ReceivePort(); | |
isolate = await Isolate.spawn(_isolateEntry, receivePort.sendPort); | |
receivePort.listen((dynamic data) { | |
if (data is SendPort) { | |
if (mounted) { | |
data.send({ | |
'path': widget.image.path, | |
'size': widget.size, | |
}); | |
} | |
} else { | |
if (mounted) { | |
setState(() { | |
imgBytes = data; | |
}); | |
} | |
} | |
}); | |
} | |
@override | |
void dispose() { | |
if (isolate != null) { | |
isolate.kill(); | |
} | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SizedBox( | |
height: widget.size.height, | |
width: widget.size.width, | |
child: imgBytes != null | |
? Image.memory( | |
imgBytes, | |
fit: BoxFit.cover, | |
) | |
: Container( | |
decoration: BoxDecoration( | |
gradient: LinearGradient( | |
colors: [Colors.grey[100], Colors.grey[300]], | |
begin: Alignment.centerLeft, | |
end: Alignment.centerRight, | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment