-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into dependabot/pub/packages/smooth_app/flutte…
…r_email_sender-6.0.2
- Loading branch information
Showing
7 changed files
with
146 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/smooth_app/lib/cards/category_cards/svg_safe_network.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'dart:ui' as ui; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_svg/flutter_svg.dart'; | ||
import 'package:flutter_svg/svg.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:smooth_app/cards/category_cards/asset_cache_helper.dart'; | ||
import 'package:smooth_app/cards/category_cards/svg_async_asset.dart'; | ||
import 'package:smooth_app/cards/category_cards/svg_cache.dart'; | ||
import 'package:smooth_app/services/smooth_services.dart'; | ||
|
||
/// Widget with async load of SVG network file. | ||
/// | ||
/// We could use SvgPicture.network, but it sends tons of errors if there in no | ||
/// internet connection. That's why we download the data ourselves. | ||
class SvgSafeNetwork extends StatefulWidget { | ||
const SvgSafeNetwork(this.helper); | ||
|
||
final AssetCacheHelper helper; | ||
|
||
@override | ||
State<SvgSafeNetwork> createState() => _SvgSafeNetworkState(); | ||
} | ||
|
||
class _SvgSafeNetworkState extends State<SvgSafeNetwork> { | ||
late final Future<String> _loading = _load(); | ||
|
||
String get _url => widget.helper.url; | ||
|
||
Future<String> _load() async { | ||
String? cached = _networkCache[_url]; | ||
if (cached != null) { | ||
return cached; | ||
} | ||
final http.Response response = await http.get(Uri.parse(_url)); | ||
_networkCache[_url] = cached = response.body; | ||
return cached; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) => FutureBuilder<String>( | ||
future: _loading, | ||
builder: (BuildContext context, AsyncSnapshot<String> snapshot) { | ||
if (snapshot.connectionState == ConnectionState.done) { | ||
if (snapshot.data != null) { | ||
return SvgPicture.string( | ||
snapshot.data!, | ||
width: widget.helper.width, | ||
height: widget.helper.height, | ||
colorFilter: widget.helper.color == null | ||
? null | ||
: ui.ColorFilter.mode( | ||
widget.helper.color!, | ||
ui.BlendMode.srcIn, | ||
), | ||
fit: BoxFit.contain, | ||
semanticsLabel: SvgCache.getSemanticsLabel(context, _url), | ||
placeholderBuilder: (BuildContext context) => | ||
SvgAsyncAsset(widget.helper), | ||
); | ||
} | ||
} | ||
if (snapshot.error != null) { | ||
final bool serverOrConnectionIssue = snapshot.error.toString() == | ||
"Failed host lookup: 'static.openfoodfacts.org'"; | ||
if (!serverOrConnectionIssue) { | ||
Logs.e( | ||
'Could not download "$_url"', | ||
ex: snapshot.error, | ||
); | ||
} | ||
} | ||
return SvgAsyncAsset(widget.helper); | ||
}, | ||
); | ||
} | ||
|
||
/// Network cache, with url as key and SVG data as value. | ||
Map<String, String> _networkCache = <String, String>{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters