Skip to content

Commit

Permalink
Fix(bluefireteam#464) network image (bluefireteam#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
renancaraujo authored Sep 17, 2021
1 parent 81fd66c commit f9f5574
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ linter:
- always_put_control_body_on_new_line
- always_require_non_null_named_parameters
- annotate_overrides
- avoid_as
- avoid_classes_with_only_static_members
- avoid_empty_else
- avoid_field_initializers_in_const_classes
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.7.0"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
Expand Down
3 changes: 1 addition & 2 deletions lib/src/core/photo_view_core.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/widgets.dart';

import 'package:photo_view/photo_view.dart'
show
PhotoViewScaleState,
Expand All @@ -11,9 +10,9 @@ import 'package:photo_view/photo_view.dart'
import 'package:photo_view/src/controller/photo_view_controller.dart';
import 'package:photo_view/src/controller/photo_view_controller_delegate.dart';
import 'package:photo_view/src/controller/photo_view_scalestate_controller.dart';
import 'package:photo_view/src/utils/photo_view_utils.dart';
import 'package:photo_view/src/core/photo_view_gesture_detector.dart';
import 'package:photo_view/src/core/photo_view_hit_corners.dart';
import 'package:photo_view/src/utils/photo_view_utils.dart';

const _defaultDecoration = const BoxDecoration(
color: const Color.fromRGBO(0, 0, 0, 1.0),
Expand Down
68 changes: 37 additions & 31 deletions lib/src/photo_view_wrappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,35 @@ class ImageWrapper extends StatefulWidget {
class _ImageWrapperState extends State<ImageWrapper> {
ImageStreamListener? _imageStreamListener;
ImageStream? _imageStream;
ImageChunkEvent? _imageChunkEvent;
ImageChunkEvent? _loadingProgress;
ImageInfo? _imageInfo;
bool _loading = true;
Size? _imageSize;
Object? _lastException;
StackTrace? _stackTrace;
StackTrace? _lastStack;

@override
void dispose() {
super.dispose();
_stopImageStream();
}

@override
void didChangeDependencies() {
_resolveImage();
super.didChangeDependencies();
}

@override
void didUpdateWidget(ImageWrapper oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.imageProvider != oldWidget.imageProvider) {
_resolveImage();
}
}

// retrieve image from the provider
void _getImage() {
void _resolveImage() {
final ImageStream newStream = widget.imageProvider.resolve(
const ImageConfiguration(),
);
Expand All @@ -83,8 +103,10 @@ class _ImageWrapperState extends State<ImageWrapper> {

ImageStreamListener _getOrCreateListener() {
void handleImageChunk(ImageChunkEvent event) {
assert(widget.loadingBuilder != null);
setState(() => _imageChunkEvent = event);
setState(() {
_loadingProgress = event;
_lastException = null;
});
}

void handleImageFrame(ImageInfo info, bool synchronousCall) {
Expand All @@ -96,9 +118,9 @@ class _ImageWrapperState extends State<ImageWrapper> {
_loading = false;
_imageInfo = _imageInfo;

_imageChunkEvent = null;
_loadingProgress = null;
_lastException = null;
_stackTrace = null;
_lastStack = null;
};
synchronousCall ? setupCB() : setState(setupCB);
}
Expand All @@ -107,8 +129,12 @@ class _ImageWrapperState extends State<ImageWrapper> {
setState(() {
_loading = false;
_lastException = error;
_stackTrace = stackTrace;
_lastStack = stackTrace;
});
assert(() {
if (widget.errorBuilder == null) throw error;
return true;
}());
}

_imageStreamListener = ImageStreamListener(
Expand All @@ -133,26 +159,6 @@ class _ImageWrapperState extends State<ImageWrapper> {
_imageStream?.removeListener(_imageStreamListener!);
}

@override
void didUpdateWidget(ImageWrapper oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.imageProvider != oldWidget.imageProvider) {
_getImage();
}
}

@override
void didChangeDependencies() {
_getImage();
super.didChangeDependencies();
}

@override
void dispose() {
super.dispose();
_stopImageStream();
}

@override
Widget build(BuildContext context) {
if (_loading) {
Expand Down Expand Up @@ -195,19 +201,19 @@ class _ImageWrapperState extends State<ImageWrapper> {

Widget _buildLoading(BuildContext context) {
if (widget.loadingBuilder != null) {
return widget.loadingBuilder!(context, _imageChunkEvent);
return widget.loadingBuilder!(context, _loadingProgress);
}

return PhotoViewDefaultLoading(
event: _imageChunkEvent,
event: _loadingProgress,
);
}

Widget _buildError(
BuildContext context,
) {
if (widget.errorBuilder != null) {
return widget.errorBuilder!(context, _lastException!, _stackTrace);
return widget.errorBuilder!(context, _lastException!, _lastStack);
}
return PhotoViewDefaultError(
decoration: widget.backgroundDecoration,
Expand Down

0 comments on commit f9f5574

Please sign in to comment.