Skip to content

Commit

Permalink
feat: add support for loading image from memory
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothvino42 committed Dec 14, 2023
1 parent dacc84c commit 11f7b27
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions lib/widget/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:flutter_svg/svg.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';
import 'package:http/http.dart' as http;
import 'package:yaml/yaml.dart';

class EnsembleImage extends StatefulWidget
with Invokable, HasController<ImageController, ImageState> {
Expand Down Expand Up @@ -78,7 +79,7 @@ class ImageController extends BoxController {
clipContent = true;
}
DateTime? lastModifiedCache;
String source = '';
dynamic source;
BoxFit? fit;
Color? placeholderColor;
EnsembleAction? onTap;
Expand All @@ -95,17 +96,25 @@ class ImageController extends BoxController {
class ImageState extends WidgetState<EnsembleImage> {
@override
Widget buildWidget(BuildContext context) {
String source = widget._controller.source.trim();
// use the placeholder for the initial state before binding kicks in
if (source.isEmpty) {
return const ColoredBoxPlaceholder();
}

Widget image;
if (isSvg()) {
image = buildSvgImage(source, widget._controller.fit);
// Memory Image
if (widget._controller.source is YamlList) {
final List<int> data = Utils.getList(widget._controller.source) ?? [];
final imageBytes = Uint8List.fromList(data);
image = buildMemoryImage(imageBytes);
} else {
image = buildNonSvgImage(source, widget._controller.fit);
String source =
Utils.getString(widget._controller.source.trim(), fallback: '');
// use the placeholder for the initial state before binding kicks in
if (source.isEmpty) {
return const ColoredBoxPlaceholder();
}

if (isSvg()) {
image = buildSvgImage(source, widget._controller.fit);
} else {
image = buildNonSvgImage(source, widget._controller.fit);
}
}

Widget rtn = BoxWrapper(
Expand Down Expand Up @@ -154,6 +163,16 @@ class ImageState extends WidgetState<EnsembleImage> {
return "${widget.controller.source}${str}timeStamp=$lastModifiedDateTime";
}

Widget buildMemoryImage(Uint8List source) {
return Image.memory(
source,
width: widget._controller.width?.toDouble(),
height: widget._controller.height?.toDouble(),
fit: widget._controller.fit,
errorBuilder: (context, error, stacktrace) => errorFallback(),
);
}

Widget buildNonSvgImage(String source, BoxFit? fit) {
if (source.startsWith('https://') || source.startsWith('http://')) {
int? cachedWidth = widget._controller.resizedWidth;
Expand Down Expand Up @@ -190,7 +209,7 @@ class ImageState extends WidgetState<EnsembleImage> {
? FutureBuilder(
future: fetch(widget.controller.source),
initialData: widget._controller.source,
builder: (context, snapshot) {
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
return cacheImage(snapshot.data!);
Expand Down

0 comments on commit 11f7b27

Please sign in to comment.