removed country flags package and read flags from server and implement svg cahce structure
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../core/services/svg_cache.dart';
|
||||
|
||||
class CachedSvg extends StatefulWidget {
|
||||
final String url;
|
||||
final double? width;
|
||||
final double? height;
|
||||
final BoxFit fit;
|
||||
|
||||
const CachedSvg({
|
||||
required this.url,
|
||||
this.width,
|
||||
this.height,
|
||||
this.fit = BoxFit.contain,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<CachedSvg> createState() => _CachedSvgState();
|
||||
}
|
||||
|
||||
class _CachedSvgState extends State<CachedSvg> {
|
||||
String? _svgData;
|
||||
bool _isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSvg();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant CachedSvg oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.url != widget.url) {
|
||||
_isLoading = true;
|
||||
_svgData = null;
|
||||
_loadSvg();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadSvg() async {
|
||||
try {
|
||||
final cache = SvgCache();
|
||||
String? cachedPath = await cache.getCachedPath(widget.url);
|
||||
cachedPath ??= await cache.cacheSvg(widget.url);
|
||||
if (kIsWeb && cachedPath.startsWith('web_cache_')) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final fileName = cachedPath.replaceFirst('web_cache_', '');
|
||||
_svgData = prefs.getString('svg_cache_$fileName');
|
||||
} else {
|
||||
_svgData = await File(cachedPath).readAsString();
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_isLoading) {
|
||||
return SizedBox(
|
||||
width: widget.width ?? 20,
|
||||
height: widget.height ?? 20,
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
if (_svgData == null) {
|
||||
return const Icon(Icons.error);
|
||||
}
|
||||
|
||||
return SvgPicture.string(
|
||||
_svgData!,
|
||||
width: widget.width,
|
||||
height: widget.height,
|
||||
fit: widget.fit,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:tm_app/core/utils/size_config.dart';
|
||||
import 'package:tm_app/views/shared/cached_svg.dart';
|
||||
|
||||
import '../../core/config/app_config.dart';
|
||||
|
||||
class Flag extends StatelessWidget {
|
||||
const Flag({required this.countryCode, super.key});
|
||||
|
||||
final String countryCode;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return countryCode.isNotEmpty
|
||||
? ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: CachedSvg(
|
||||
url: '${AppConfig.flagUrl}$countryCode',
|
||||
width: 32.0.w(),
|
||||
height: 21.0.h(),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
)
|
||||
: const SizedBox();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user