Skip to content

Commit 8d82e73

Browse files
committed
refactor
1 parent 4e2c19e commit 8d82e73

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

isoimg/assets/tulips.jpg

754 KB
Loading

isoimg/lib/main.dart

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import 'dart:async';
2+
import 'dart:isolate';
23
import 'dart:typed_data';
34

45
import 'package:flutter/foundation.dart';
56
import 'package:flutter/material.dart';
7+
import 'package:flutter/services.dart';
68

79
import 'package:image/image.dart' as image;
8-
import 'package:http/http.dart' as http;
910
import 'package:loading_indicator/loading_indicator.dart';
1011

1112
void main() {
@@ -22,15 +23,13 @@ class MyApp extends StatelessWidget {
2223
theme: ThemeData(
2324
primarySwatch: Colors.blue,
2425
),
25-
home: const MyHomePage(title: 'Image Processing Demo'),
26+
home: const MyHomePage(),
2627
);
2728
}
2829
}
2930

3031
class MyHomePage extends StatefulWidget {
31-
const MyHomePage({Key? key, required this.title}) : super(key: key);
32-
33-
final String title;
32+
const MyHomePage({Key? key}) : super(key: key);
3433

3534
@override
3635
State<MyHomePage> createState() => _MyHomePageState();
@@ -45,20 +44,36 @@ class _MyHomePageState extends State<MyHomePage> {
4544
@override
4645
void initState() {
4746
super.initState();
48-
http.get(Uri.parse('https://picsum.photos/id/428/2529/1581')).then(
49-
(http.Response response) => setState(() {
50-
_originalImage = response.bodyBytes;
51-
_image = _originalImage;
52-
}),
53-
);
47+
rootBundle.load('assets/tulips.jpg').then((ByteData data) {
48+
setState(() {
49+
_originalImage = data.buffer.asUint8List();
50+
_image = _originalImage;
51+
});
52+
});
53+
}
54+
55+
static Uint8List _applySepiaFilter(Uint8List original) {
56+
final image.Image decoded = image.decodeImage(original.toList())!;
57+
final image.Image sepia = image.sepia(decoded);
58+
final Uint8List encoded = Uint8List.fromList(image.encodeJpg(sepia));
59+
return encoded;
5460
}
5561

5662
void _applySepiaFilterSync() {
5763
setState(() {
5864
_loading = true;
5965
});
60-
// Hack to render the loading indicator before the actual computation
61-
// blocks the main thread.
66+
final Uint8List filtered = _applySepiaFilter(_image!);
67+
setState(() {
68+
_image = filtered;
69+
_loading = false;
70+
});
71+
}
72+
73+
void _applySepiaFilterAsync() {
74+
setState(() {
75+
_loading = true;
76+
});
6277
Future.delayed(const Duration(milliseconds: 500), () {
6378
final Uint8List filtered = _applySepiaFilter(_image!);
6479
setState(() {
@@ -68,19 +83,13 @@ class _MyHomePageState extends State<MyHomePage> {
6883
});
6984
}
7085

71-
static Uint8List _applySepiaFilter(Uint8List original) {
72-
final image.Image decoded = image.decodeImage(original.toList())!;
73-
final image.Image sepia = image.sepia(decoded);
74-
final Uint8List encoded = Uint8List.fromList(image.encodeJpg(sepia));
75-
return encoded;
76-
}
77-
78-
Future<void> _applySepiaFilterAsync() async {
86+
Future<void> _applySepiaFilterInIsolate() async {
7987
setState(() {
8088
_loading = true;
8189
});
82-
final Uint8List encoded =
83-
await compute<Uint8List, Uint8List>(_applySepiaFilter, _image!);
90+
final Uint8List encoded = await compute(_applySepiaFilter, _image!);
91+
// Alternative with Isolate.run (currently crashes).
92+
// final Uint8List encoded = await Isolate.run(() => _applySepiaFilter(_image!));
8493
setState(() {
8594
_image = encoded;
8695
_loading = false;
@@ -97,7 +106,7 @@ class _MyHomePageState extends State<MyHomePage> {
97106
Widget build(BuildContext context) {
98107
return Scaffold(
99108
appBar: AppBar(
100-
title: Text(widget.title),
109+
title: const Text('Image Processing Demo'),
101110
),
102111
body: Stack(
103112
children: [
@@ -146,6 +155,12 @@ class _MyHomePageState extends State<MyHomePage> {
146155
: null,
147156
child: const Text('Sepia (async)'),
148157
),
158+
TextButton(
159+
onPressed: _image == _originalImage && !_loading
160+
? _applySepiaFilterInIsolate
161+
: null,
162+
child: const Text('Sepia (isolate)'),
163+
),
149164
TextButton(
150165
onPressed: _image != _originalImage && !_loading ? _reset : null,
151166
child: const Text('Reset'),

isoimg/pubspec.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ dependencies:
3232
cupertino_icons: ^1.0.2
3333
image: ^3.1.3
3434
loading_indicator: ^3.0.3
35-
http: ^0.13.4
3635

3736
dev_dependencies:
3837
flutter_test:
@@ -56,9 +55,8 @@ flutter:
5655
# the material Icons class.
5756
uses-material-design: true
5857

59-
# To add assets to your application, add an assets section, like this:
60-
# assets:
61-
# - packages/flutter_gallery_assets/places/india_chennai_flower_market.png
58+
assets:
59+
- assets/tulips.jpg
6260

6361
# An image asset can refer to one or more resolution-specific "variants", see
6462
# https://flutter.dev/assets-and-images/#resolution-aware.

0 commit comments

Comments
 (0)