|
1 | 1 | import 'dart:async'; |
2 | 2 | import 'dart:convert'; |
3 | 3 | import 'dart:typed_data'; |
| 4 | +import 'dart:ui' as ui; |
4 | 5 |
|
5 | 6 | import 'package:flutter/material.dart'; |
6 | | -import 'package:flutter_blurhash/flutter_blurhash.dart'; |
7 | 7 | import 'package:flutter_svg/flutter_svg.dart'; |
8 | 8 | import 'package:http/http.dart' as http; |
9 | 9 | import 'package:meta/meta.dart'; |
10 | 10 | import 'package:neon_framework/models.dart'; |
11 | 11 | import 'package:neon_framework/src/bloc/result.dart'; |
| 12 | +import 'package:neon_framework/src/blocs/blur.dart'; |
12 | 13 | import 'package:neon_framework/src/utils/account_client_extension.dart'; |
13 | 14 | import 'package:neon_framework/src/utils/image_utils.dart'; |
| 15 | +import 'package:neon_framework/src/utils/provider.dart'; |
14 | 16 | import 'package:neon_framework/src/utils/request_manager.dart'; |
15 | 17 | import 'package:neon_framework/src/widgets/error.dart'; |
16 | 18 | import 'package:neon_framework/src/widgets/linear_progress_indicator.dart'; |
@@ -108,32 +110,65 @@ class NeonImage extends StatelessWidget { |
108 | 110 | // If the data is not UTF-8 |
109 | 111 | } |
110 | 112 |
|
111 | | - return Image.memory( |
112 | | - data, |
113 | | - height: size?.height, |
114 | | - width: size?.width, |
115 | | - fit: fit ?? BoxFit.contain, |
116 | | - gaplessPlayback: true, |
117 | | - errorBuilder: (context, error, stacktrace) => _buildError(context, error), |
118 | | - ); |
119 | | - } |
120 | | - |
121 | | - if (blurHash != null) { |
122 | | - return BlurHash( |
123 | | - hash: blurHash!, |
124 | | - imageFit: fit ?? BoxFit.cover, |
125 | | - decodingHeight: size?.height.toInt() ?? 32, |
126 | | - decodingWidth: size?.width.toInt() ?? 32, |
| 113 | + return _buildImageWithBlur( |
| 114 | + context, |
| 115 | + child: Image.memory( |
| 116 | + data, |
| 117 | + height: size?.height, |
| 118 | + width: size?.width, |
| 119 | + fit: fit ?? BoxFit.contain, |
| 120 | + gaplessPlayback: true, |
| 121 | + errorBuilder: (context, error, stacktrace) => _buildError(context, error), |
| 122 | + ), |
| 123 | + isLoading: imageResult.isLoading, |
127 | 124 | ); |
128 | 125 | } |
129 | 126 |
|
130 | 127 | if (imageResult.hasError) { |
131 | 128 | return _buildError(context, imageResult.error); |
132 | 129 | } |
133 | 130 |
|
| 131 | + return _buildBlur(context, isLoading: imageResult.isLoading); |
| 132 | + }, |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /// Replacing the blurhash with the actual image leads to UI flickering when scrolling very fast. |
| 137 | + /// To mitigate this, we keep the blurhash underneath the actual image. |
| 138 | + Widget _buildImageWithBlur(BuildContext context, {required Widget child, bool isLoading = true}) => Stack( |
| 139 | + fit: StackFit.passthrough, |
| 140 | + children: [ |
| 141 | + _buildBlur(context, isLoading: isLoading), |
| 142 | + child, |
| 143 | + ], |
| 144 | + ); |
| 145 | + |
| 146 | + Widget _buildBlur(BuildContext context, {bool isLoading = true}) { |
| 147 | + final blurBloc = NeonProvider.of<BlurBloc>(context); |
| 148 | + return FutureBuilder<ui.Image>( |
| 149 | + // Key is important to ensure that we can move it without cost in the widget tree. |
| 150 | + key: ValueKey(blurHash), |
| 151 | + // We are not caching the blurHash result because we do not want to take care of cleanup in here. |
| 152 | + // If pre-caching is required, the encapsulating widget should take care of it and also of the cleanup. |
| 153 | + future: blurHash != null |
| 154 | + ? blurBloc.getBlurHash( |
| 155 | + blurHash!, |
| 156 | + size ?? const Size.square(32), |
| 157 | + cache: false, |
| 158 | + ) |
| 159 | + : null, |
| 160 | + builder: (context, snapshot) { |
| 161 | + if (snapshot.hasData) { |
| 162 | + return RawImage( |
| 163 | + image: snapshot.data, |
| 164 | + ); |
| 165 | + } |
| 166 | + |
134 | 167 | return SizedBox( |
135 | 168 | width: size?.width, |
136 | | - child: const NeonLinearProgressIndicator(), |
| 169 | + child: NeonLinearProgressIndicator( |
| 170 | + visible: isLoading, |
| 171 | + ), |
137 | 172 | ); |
138 | 173 | }, |
139 | 174 | ); |
|
0 commit comments