Skip to content

Commit

Permalink
Update example, add thumbFormat.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiJingLong committed Mar 22, 2020
1 parent 4772f6e commit 9b465ca
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 57 deletions.
18 changes: 11 additions & 7 deletions example/lib/core/lru_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,33 @@ class LRUMap<K, V> {
class ImageLruCache {
static LRUMap<_ImageCacheEntity, Uint8List> _map = LRUMap(500);

static Uint8List getData(AssetEntity entity, [int size = 64]) {
return _map.get(_ImageCacheEntity(entity, size));
static Uint8List getData(AssetEntity entity,
[int size = 64, ThumbFormat format = ThumbFormat.jpeg]) {
return _map.get(_ImageCacheEntity(entity, size, format));
}

static void setData(AssetEntity entity, int size, Uint8List list) {
_map.put(_ImageCacheEntity(entity, size), list);
static void setData(
AssetEntity entity, int size, ThumbFormat format, Uint8List list) {
_map.put(_ImageCacheEntity(entity, size, format), list);
}
}

class _ImageCacheEntity {
AssetEntity entity;
int size;
ThumbFormat format;

_ImageCacheEntity(this.entity, this.size);
_ImageCacheEntity(this.entity, this.size, this.format);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is _ImageCacheEntity &&
runtimeType == other.runtimeType &&
entity == other.entity &&
size == other.size;
size == other.size &&
format == other.format;

@override
int get hashCode => entity.hashCode ^ size.hashCode;
int get hashCode => entity.hashCode * size.hashCode * format.hashCode;
}
18 changes: 17 additions & 1 deletion example/lib/model/photo_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class PhotoProvider extends ChangeNotifier {
notifyListeners();
}

var _thumbFormat = ThumbFormat.png;

ThumbFormat get thumbFormat => _thumbFormat;

set thumbFormat(thumbFormat) {
_thumbFormat = thumbFormat;
notifyListeners();
}

bool get notifying => _notifying;

String minWidth = "0";
Expand Down Expand Up @@ -158,6 +167,14 @@ class PhotoProvider extends ChangeNotifier {
}
notifyListeners();
}

void changeThumbFormat() {
if (thumbFormat == ThumbFormat.jpeg) {
thumbFormat = ThumbFormat.png;
} else {
thumbFormat = ThumbFormat.jpeg;
}
}
}

class PathProvider extends ChangeNotifier {
Expand Down Expand Up @@ -212,7 +229,6 @@ class PathProvider extends ChangeNotifier {
printListLength("deleted");
this.list.clear();
this.list.addAll(list);

}
}

Expand Down
11 changes: 11 additions & 0 deletions example/lib/page/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class _NewHomePageState extends State<NewHomePage> {
),
_buildHasAllCheck(),
_buildOnlyAllCheck(),
_buildPngCheck(),
_buildNotifyCheck(),
_buildFilterOption(provider),
],
Expand Down Expand Up @@ -138,6 +139,16 @@ class _NewHomePageState extends State<NewHomePage> {
);
}

Widget _buildPngCheck() {
return CheckboxListTile(
value: provider.thumbFormat == ThumbFormat.png,
onChanged: (value) {
provider.changeThumbFormat();
},
title: Text("thumb png"),
);
}

Widget _buildOnlyAllCheck() {
return CheckboxListTile(
value: provider.onlyAll,
Expand Down
64 changes: 18 additions & 46 deletions example/lib/page/image_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:image_scanner_example/widget/loading_widget.dart';
import 'package:oktoast/oktoast.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:provider/provider.dart';
import 'dart:ui' as ui;

class GalleryContentListPage extends StatefulWidget {
final AssetPathEntity path;
Expand All @@ -27,6 +26,8 @@ class GalleryContentListPage extends StatefulWidget {
class _GalleryContentListPageState extends State<GalleryContentListPage> {
AssetPathEntity get path => widget.path;

PhotoProvider get photoProvider => Provider.of<PhotoProvider>(context);

PathProvider get provider =>
Provider.of<PhotoProvider>(context).getOrCreatePathProvider(path);

Expand All @@ -45,6 +46,22 @@ class _GalleryContentListPageState extends State<GalleryContentListPage> {
appBar: AppBar(
title: Text("${path.name}"),
actions: <Widget>[
ChangeNotifierBuilder(
builder: (context, provider) {
final formatType = provider.thumbFormat == ThumbFormat.jpeg
? ThumbFormat.png
: ThumbFormat.jpeg;
return IconButton(
icon: Icon(Icons.swap_horiz),
iconSize: 22,
tooltip: "Use another format.",
onPressed: () {
provider.thumbFormat = formatType;
},
);
},
value: photoProvider,
),
Tooltip(
child: Padding(
padding: const EdgeInsets.all(10.0),
Expand Down Expand Up @@ -243,49 +260,4 @@ class _GalleryContentListPageState extends State<GalleryContentListPage> {
);
});
}

void showThumbImageDialog(AssetEntity entity, int width, int height) async {
final format = ThumbFormat.jpeg;
width = width ~/ 4;
height = height ~/ 4;

print("will show ${width}x$height image");

if (entity.orientation == 90 || entity.orientation == 270) {
int tmp = width;
width = height;
height = tmp;
}

showDialog(
context: context,
builder: (_) {
return FutureBuilder<Uint8List>(
future: entity.thumbDataWithSize(width, height, format: format),
builder: (BuildContext context, snapshot) {
Widget w;
if (snapshot.hasData) {
ui.decodeImageFromList(snapshot.data, (decodingImage) {
print(
"the thumb of image is ${decodingImage.width}x${decodingImage.height}");
});
print("$format image length : ${snapshot.data.length}");
w = Image.memory(snapshot.data);
} else {
w = Center(
child: Container(
color: Colors.white,
padding: const EdgeInsets.all(20),
child: CircularProgressIndicator(),
),
);
}
return GestureDetector(
child: w,
onTap: () => Navigator.pop(context),
);
},
);
});
}
}
21 changes: 18 additions & 3 deletions example/lib/widget/image_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:image_scanner_example/core/lru_map.dart';
import 'package:image_scanner_example/model/photo_provider.dart';
import 'package:image_scanner_example/widget/change_notifier_builder.dart';
import 'package:image_scanner_example/widget/loading_widget.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:provider/provider.dart';

class ImageItemWidget extends StatefulWidget {
final AssetEntity entity;
Expand All @@ -19,6 +22,18 @@ class ImageItemWidget extends StatefulWidget {
class _ImageItemWidgetState extends State<ImageItemWidget> {
@override
Widget build(BuildContext context) {
final provider = Provider.of<PhotoProvider>(context);
return ChangeNotifierBuilder(
builder: (c, p) {
final format = provider.thumbFormat;
print(format);
return buildContent(format);
},
value: provider,
);
}

Widget buildContent(ThumbFormat format) {
if (widget.entity.type == AssetType.audio) {
return Center(
child: Icon(
Expand All @@ -29,15 +44,15 @@ class _ImageItemWidgetState extends State<ImageItemWidget> {
}
final item = widget.entity;
final size = 130;
final u8List = ImageLruCache.getData(item, size);
final u8List = ImageLruCache.getData(item, size, format);

Widget image;

if (u8List != null) {
return _buildImageWidget(item, u8List, size);
} else {
image = FutureBuilder<Uint8List>(
future: item.thumbDataWithSize(size, size),
future: item.thumbDataWithSize(size, size, format: format),
builder: (context, snapshot) {
Widget w;
if (snapshot.hasError) {
Expand All @@ -46,7 +61,7 @@ class _ImageItemWidgetState extends State<ImageItemWidget> {
);
}
if (snapshot.hasData) {
ImageLruCache.setData(item, size, snapshot.data);
ImageLruCache.setData(item, size, format, snapshot.data);
w = _buildImageWidget(item, snapshot.data, size);
} else {
w = Center(
Expand Down

0 comments on commit 9b465ca

Please sign in to comment.