|
| 1 | +import 'dart:io'; |
| 2 | + |
1 | 3 | import 'package:flutter/material.dart'; |
2 | 4 | import 'package:flutter/services.dart'; |
3 | 5 | import 'package:jovial_svg/jovial_svg.dart'; |
| 6 | +import 'package:jovial_svg/src/compact.dart'; |
4 | 7 |
|
5 | 8 | /// A sized scalable image widget. |
6 | 9 | class SizedScalableImageWidget extends StatelessWidget { |
@@ -39,11 +42,14 @@ class SizedScalableImageWidget extends StatelessWidget { |
39 | 42 |
|
40 | 43 | @override |
41 | 44 | Widget build(BuildContext context) { |
| 45 | + File file = File(asset); |
42 | 46 | Widget child = ScalableImageWidget.fromSISource( |
43 | | - si: ScalableImageSource.fromSI( |
44 | | - rootBundle, |
45 | | - asset, |
46 | | - ), |
| 47 | + si: file.existsSync() |
| 48 | + ? _SIFileSource(file, null) |
| 49 | + : ScalableImageSource.fromSI( |
| 50 | + rootBundle, |
| 51 | + asset, |
| 52 | + ), |
47 | 53 | fit: fit, |
48 | 54 | alignment: alignment, |
49 | 55 | ); |
@@ -74,3 +80,38 @@ class SizedScalableImageWidget extends StatelessWidget { |
74 | 80 | return child; |
75 | 81 | } |
76 | 82 | } |
| 83 | + |
| 84 | +class _SIFileSource extends ScalableImageSource { |
| 85 | + final File file; |
| 86 | + final Color? currentColor; |
| 87 | + |
| 88 | + _SIFileSource(this.file, this.currentColor); |
| 89 | + |
| 90 | + @override |
| 91 | + Future<ScalableImage> get si => createSI(); |
| 92 | + |
| 93 | + @override |
| 94 | + Future<ScalableImage> createSI({bool compact = false}) async { |
| 95 | + ScalableImageCompact scalableImageCompact = ScalableImageCompact.fromBytes(file.readAsBytesSync(), currentColor: currentColor); |
| 96 | + if (compact) { |
| 97 | + return scalableImageCompact; |
| 98 | + } else { |
| 99 | + return scalableImageCompact.toDag(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @override |
| 104 | + bool operator ==(final Object other) { |
| 105 | + if (other is _SIFileSource) { |
| 106 | + return file == other.file && currentColor == other.currentColor; |
| 107 | + } else { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @override |
| 113 | + int get hashCode => 0xf67cd716 ^ Object.hash(file, currentColor); |
| 114 | + |
| 115 | + @override |
| 116 | + String toString() => '__SIFileSource($file $currentColor)'; |
| 117 | +} |
0 commit comments