import 'dart:math'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; import 'package:manager_api/api.dart'; import 'package:provider/provider.dart'; import 'package:tablet_app/Components/loading.dart'; import 'package:tablet_app/Components/loading_common.dart'; import 'package:tablet_app/Helpers/DatabaseHelper.dart'; import 'package:tablet_app/Helpers/MQTTHelper.dart'; import 'package:tablet_app/Screens/Agenda/agenda_view.dart'; import 'package:tablet_app/Screens/Article/article_view.dart'; import 'package:tablet_app/Screens/Configuration/config_view.dart'; import 'package:tablet_app/Screens/Map/map_context.dart'; import 'package:tablet_app/Screens/Map/map_view.dart'; import 'package:tablet_app/Models/map-marker.dart'; import 'package:tablet_app/Models/tabletContext.dart'; import 'package:tablet_app/Screens/Menu/menu_view.dart'; import 'package:tablet_app/Screens/PDF/pdf_view.dart'; import 'package:tablet_app/Screens/Puzzle/puzzle_view.dart'; import 'package:tablet_app/Screens/Slider/slider_view.dart'; import 'package:tablet_app/Screens/Video/video_view.dart'; import 'package:tablet_app/Screens/Web/web_view.dart'; import 'package:tablet_app/app_context.dart'; import 'package:tablet_app/constants.dart'; import '../Quizz/quizz_view.dart'; import 'language_selection.dart'; class MainViewWidget extends StatefulWidget { MainViewWidget(); @override _MainViewWidget createState() => _MainViewWidget(); } class _MainViewWidget extends State { Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution SectionDTO? sectionSelected; late ConfigurationDTO configurationDTO; int rowCount = 4; @override Widget build(BuildContext context) { final appContext = Provider.of(context); SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); Size size = MediaQuery.of(context).size; configurationDTO = appContext.getContext().configuration; // TODO REMOVE /*if (!MQTTHelper.instance.isInstantiated) MQTTHelper.instance.connect(appContext);*/ if(sectionSelected != null) { var elementToShow; switch (sectionSelected!.type) { case SectionType.map : // MAP elementToShow = ChangeNotifierProvider( create: (_) => MapContext(new MapMarker( latitude: null, longitude: null, title: '', description: '')), child: MapViewWidget(section: sectionSelected!) /*FutureBuilder( future: _url, builder: (BuildContext context, AsyncSnapshot snapshot) => snapshot.hasData ? WebViewWidget(url: snapshot.data,) : CircularProgressIndicator()),*/ ); break; case SectionType.web : // WEB elementToShow = WebView(section: sectionSelected); break; case SectionType.video : // Video elementToShow = VideoView(section: sectionSelected); break; case SectionType.slider : elementToShow = SliderView(section: sectionSelected); break; case SectionType.menu : elementToShow = MenuView(section: sectionSelected!); break; case SectionType.quizz : elementToShow = QuizzView(section: sectionSelected); break; case SectionType.pdf : elementToShow = PDFViewWidget(section: sectionSelected); break; case SectionType.puzzle : elementToShow = PuzzleView(section: sectionSelected); break; case SectionType.agenda : elementToShow = AgendaView(section: sectionSelected); break; /*case SectionType.article : // TODO elementToShow = ArticleView(section: sectionSelected); break;*/ default: elementToShow = Text("Ce type n'est pas supporté"); break; } return Scaffold( body: SingleChildScrollView( child: Container( height: size.height, width: size.width, color: configurationDTO.imageId == null ? configurationDTO.secondaryColor != null ? new Color(int.parse(configurationDTO.secondaryColor!.split('(0x')[1].split(')')[0], radix: 16)): kBackgroundGrey : null, decoration: configurationDTO.imageId != null ? BoxDecoration( image: new DecorationImage( fit: BoxFit.cover, colorFilter: new ColorFilter.mode(Colors.white.withOpacity(0.8), BlendMode.lighten), image: new NetworkImage( configurationDTO.imageSource!, ), ), ) : null, child: Column( children: [ Container( width: size.width, height: size.height * 0.12, child: Row( children: [ Padding( padding: const EdgeInsets.only(left: 15.0, top: 10.0), child: AspectRatio( aspectRatio: 4 / 4, child: Container( /*width: 125, height: 125,*/ decoration: boxDecoration(sectionSelected!, true), ), ), ), SizedBox( width: 25, ), Padding( padding: const EdgeInsets.all(8.0), child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Align( alignment: Alignment.centerLeft, child: HtmlWidget( sectionSelected!.title!.firstWhere((translation) => translation.language == appContext.getContext().language).value!, textStyle: new TextStyle(fontSize: kIsWeb ? kWebSectionTitleDetailSize : kSectionTitleDetailSize, color: Colors.white), ) ) ), if(sectionSelected!.description!.firstWhere((translation) => translation.language == appContext.getContext().language).value != null && sectionSelected!.description!.firstWhere((translation) => translation.language == appContext.getContext().language).value!.trim().isNotEmpty) Expanded( child: Align( alignment: Alignment.centerLeft, child: HtmlWidget( sectionSelected!.description!.firstWhere((translation) => translation.language == appContext.getContext().language).value!, textStyle: new TextStyle(fontSize: kIsWeb? kWebSectionDescriptionDetailSize : kSectionDescriptionDetailSize), ) ) ) ], ), ) ], ), ), Padding( padding: sectionSelected!.type != SectionType.slider ? const EdgeInsets.only(left: 15.0, right: 15.0, top: 15.0) : const EdgeInsets.only(top: 15.0), child: Container( width: size.width, height: size.height * 0.85, decoration: sectionSelected!.type != SectionType.video && sectionSelected!.type != SectionType.web && sectionSelected!.type != SectionType.slider && sectionSelected!.type != SectionType.map ? BoxDecoration( color: kBackgroundLight, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(30.0), boxShadow: [ BoxShadow( color: kBackgroundSecondGrey, spreadRadius: 0.5, blurRadius: 5, offset: Offset(0, 1.5), // changes position of shadow ), ], ) : null, child: Padding( padding: const EdgeInsets.all(0.0), child: elementToShow), ), ) ], ), ), ), floatingActionButtonLocation: FloatingActionButtonLocation.endTop, floatingActionButton: Padding( padding: const EdgeInsets.only(top: kIsWeb ? 16.0 : 16.0), child: Container( height: kIsWeb ? size.height *0.08 : size.height *0.08, width: kIsWeb ? size.width *0.08: size.width *0.08, child: FittedBox( child: FloatingActionButton.extended( backgroundColor: kBackgroundColor, focusColor: kBackgroundColor, splashColor: kBackgroundColor, onPressed: () { setState(() { sectionSelected = null; }); }, icon: Icon(Icons.arrow_back, color: Colors.grey), label: Text("Menu", style: TextStyle(color: Colors.black)) ), ), ), ), ); } else { return Scaffold( body: Container( height: size.height, width: size.width, color: configurationDTO.imageId == null ? configurationDTO.secondaryColor != null ? new Color(int.parse(appContext.getContext().configuration.secondaryColor.split('(0x')[1].split(')')[0], radix: 16)): kBackgroundGrey : null, decoration: configurationDTO.imageId != null ? BoxDecoration( image: new DecorationImage( fit: BoxFit.cover, colorFilter: new ColorFilter.mode(Colors.grey.withOpacity(0.1), BlendMode.color), image: new NetworkImage( configurationDTO.imageSource!, ), ), ) : null, child: Stack( children: [ LanguageSelection(size: size), Center( child: Container( height: kIsWeb ? size.height : size.height * 0.85, width: size.width * 0.9, child: FutureBuilder( future: getSections(appContext), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { if (snapshot.data == null) { // Show select config return Text(""); } else { return Center( child: GridView.builder( shrinkWrap: true, gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: rowCount), itemCount: snapshot.data?.length, itemBuilder: (BuildContext context, int index) { return InkWell( onTap: () { setState(() { sectionSelected = snapshot.data[index]; }); }, child: Container( decoration: boxDecoration(snapshot.data[index], false), padding: const EdgeInsets.all(25), margin: EdgeInsets.symmetric(vertical: 25, horizontal: 25), child: Align( alignment: Alignment.bottomRight, child: FractionallySizedBox( heightFactor: 0.35, child: Container( color: Colors.green, child: Column( children: [ Container( color: Colors.blue, child: LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { return SizedBox( width: double.infinity, child: FittedBox( fit: BoxFit.fitWidth, child: Align( alignment: Alignment.centerRight, child: HtmlWidget( snapshot.data[index].title.firstWhere((translation) => translation.language == appContext.getContext().language).value, customStylesBuilder: (element) { return {'text-align': 'right'}; }, textStyle: TextStyle(fontSize: calculateFontSize(constraints.maxWidth, constraints.maxHeight, kIsWeb ? kWebMenuTitleDetailSize : kMenuTitleDetailSize)), ), ), ), ); }, ), ), Container(color: Colors.orange, child: Text("ds")), /*Container( color: Colors.orange, child: FittedBox( fit: BoxFit.fitWidth, child: Align( alignment: Alignment.centerRight, child: HtmlWidget( snapshot.data[index].title.firstWhere((translation) => translation.language == appContext.getContext().language).value, customStylesBuilder: (element) { return {'text-align': 'right'}; }, textStyle: new TextStyle(fontSize: calculateFontSize(context, kIsWeb ? kWebMenuTitleDetailSize: kMenuTitleDetailSize)), ), ), ), ), FittedBox( fit: BoxFit.fill, child: Align( alignment: Alignment.centerRight, child: HtmlWidget( snapshot.data[index].description.firstWhere((translation) => translation.language == appContext.getContext().language).value, customStylesBuilder: (element) { return {'text-align': 'right'}; }, textStyle: new TextStyle(fontSize: calculateFontSize(context, kIsWeb ? kWebMenuDescriptionDetailSize: kMenuDescriptionDetailSize), fontFamily: ""), ), ), ),*/ ], ), ) ), ), ), ); } ), ); } } else if (snapshot.connectionState == ConnectionState.none) { return Text("No data"); } else { return Center( child: Container( child: LoadingCommon() ) ); } } ), ), ), ]), ) ); } } Future getCurrentConfiguration(dynamic appContext) async { TabletAppContext tabletAppContext = await appContext.getContext(); try { ConfigurationDTO? configurationDTO = await tabletAppContext.clientAPI!.configurationApi!.configurationGetDetail(tabletAppContext.configuration!.id!); tabletAppContext.configuration = configurationDTO; TabletAppContext? localContext = await DatabaseHelper.instance.getData(); if (localContext != null) { // Check if sql DB exist await DatabaseHelper.instance.update(tabletAppContext); } else { await DatabaseHelper.instance.insert(tabletAppContext); } /* appContext.setContext(tabletAppContext); // STORE IT LOCALLY (SQLite) TabletAppContext? localContext = await DatabaseHelper.instance.getData(); if (localContext != null) { // Check if sql DB exist await DatabaseHelper.instance.update(tabletAppContext); } else { await DatabaseHelper.instance.insert(tabletAppContext); }*/ return configurationDTO; } catch (e) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Une erreur est survenue lors de la récupération de la configuration'), ), ); } } Future?> getSections(dynamic appContext) async { TabletAppContext tabletAppContext = await appContext.getContext(); await getCurrentConfiguration(appContext); try { List? sections = await tabletAppContext.clientAPI!.sectionApi!.sectionGetFromConfiguration(tabletAppContext.configuration!.id!); sections!.sort((a, b) => a.order!.compareTo(b.order!)); return sections; } catch (e) { print(e); print("IN CATCH"); Navigator.pushAndRemoveUntil( context, MaterialPageRoute( builder: (context) { return ConfigViewWidget(); }, ), (Route route) => false // For pushAndRemoveUntil ); } } } double calculateFontSize(double parentWidth, double parentHeight, double baseSize) { // La taille de base est basée sur la taille définie dans vos constantes (kWebMenuTitleDetailSize ou kMenuTitleDetailSize). // Vous pouvez ajuster ce facteur en fonction de vos besoins. double scaleFactor = 0.8; // Calculez la taille de la police en fonction de la largeur et de la hauteur maximales disponibles //double calculatedFontSize = parentWidth * scaleFactor; // Vous pouvez également prendre en compte la hauteur ici si nécessaire double calculatedFontSize = min(parentWidth, parentHeight) * scaleFactor; // Utilisez la plus petite valeur entre la taille de base et la taille calculée return calculatedFontSize < baseSize ? calculatedFontSize : baseSize; } boxDecoration(SectionDTO section, bool isSelected) { return BoxDecoration( color: kBackgroundLight, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(30.0), image: section.imageSource != null ? new DecorationImage( fit: !isSelected || kIsWeb ? BoxFit.cover : BoxFit.contain, colorFilter: !isSelected? new ColorFilter.mode(Colors.black.withOpacity(kIsWeb ? 0.3 : 0.5), BlendMode.dstATop) : null, image: new NetworkImage( section.imageSource!, ), ): null, boxShadow: [ BoxShadow( color: kBackgroundSecondGrey, spreadRadius: 0.5, blurRadius: 5, offset: Offset(0, 1.5), // changes position of shadow ), ], ); }