111 lines
4.2 KiB
Dart
111 lines
4.2 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/app_context.dart';
|
|
import 'package:mymuseum_visitapp/constants.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class LoadingCommon extends StatefulWidget {
|
|
const LoadingCommon({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<LoadingCommon> createState() => _LoadingCommonState();
|
|
}
|
|
|
|
class _LoadingCommonState extends State<LoadingCommon> with TickerProviderStateMixin {
|
|
AnimationController? _controller;
|
|
|
|
@override
|
|
void initState() {
|
|
_controller = AnimationController(
|
|
duration: const Duration(milliseconds: 5000),
|
|
vsync: this,
|
|
)..repeat();
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller!.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
VisitAppContext visitAppContext = appContext.getContext();
|
|
|
|
Size size = MediaQuery.of(context).size;
|
|
_controller!.forward(from: 0.0);
|
|
_controller!.addListener(() {
|
|
if (_controller!.isCompleted) {
|
|
_controller!.reverse();
|
|
}
|
|
if(_controller!.isDismissed){
|
|
_controller!.forward();
|
|
}
|
|
});
|
|
|
|
var primaryColor = visitAppContext.configuration != null ? visitAppContext.configuration!.primaryColor != null ? new Color(int.parse(visitAppContext.configuration!.primaryColor!.split('(0x')[1].split(')')[0], radix: 16)) : kMainColor1 : kMainColor1;
|
|
return Center(
|
|
child: SizedBox(
|
|
height: size.height * 0.1,
|
|
child: RotationTransition(
|
|
turns: Tween(begin: 0.0, end: 3.0).animate(_controller!),
|
|
child: visitAppContext.configuration != null && visitAppContext.configuration!.loaderImageUrl != null ?
|
|
//Image.network(tabletAppContext.configuration!.loaderImageUrl!)
|
|
// TODO Replace by CustomCacheResource..
|
|
FutureBuilder<File?>(
|
|
future: _checkIfLocalResourceExists(visitAppContext, visitAppContext.configuration!.loaderImageId!),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
// Loader ou indicateur de chargement pendant la vérification
|
|
return const CircularProgressIndicator();
|
|
} else if (snapshot.hasError || snapshot.data == null) {
|
|
// Si la ressource locale n'existe pas ou s'il y a une erreur
|
|
print("not a local loader succck");
|
|
return CachedNetworkImage(
|
|
imageUrl: visitAppContext.configuration!.loaderImageUrl!,
|
|
progressIndicatorBuilder: (context, url, downloadProgress) => CircularProgressIndicator(value: downloadProgress.progress, color: primaryColor),
|
|
errorWidget: (context, url, error) => const Icon(Icons.error),
|
|
);
|
|
} else {
|
|
print("Its a local loaaader yeah");
|
|
return Image.file(
|
|
snapshot.data!,
|
|
);
|
|
}
|
|
},
|
|
)
|
|
: Icon(Icons.museum_outlined, color: kMainColor2, size: size.height*0.1),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<File?> _checkIfLocalResourceExists(VisitAppContext visitAppContext, String resourceId) async {
|
|
try {
|
|
Directory? appDocumentsDirectory = Platform.isIOS ? await getApplicationDocumentsDirectory() : await getDownloadsDirectory();
|
|
String localPath = appDocumentsDirectory!.path;
|
|
Directory configurationDirectory = Directory('$localPath/${visitAppContext.configuration!.id}');
|
|
List<FileSystemEntity> fileList = configurationDirectory.listSync();
|
|
|
|
if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resourceId))) {
|
|
File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(resourceId)).path);
|
|
return file;
|
|
}
|
|
} catch(e) {
|
|
print("ERROR _checkIfLocalResourceExists CachedCustomResource");
|
|
print(e);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
|