import 'package:flutter/material.dart'; import 'package:manager_api/api.dart'; import 'package:mymuseum_visitapp/Components/Loading.dart'; import 'package:mymuseum_visitapp/Components/SearchBox.dart'; import 'package:mymuseum_visitapp/Components/SearchNumberBox.dart'; import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart'; import 'package:mymuseum_visitapp/Helpers/translationHelper.dart'; import 'package:mymuseum_visitapp/Models/articleRead.dart'; import 'package:mymuseum_visitapp/Models/visitContext.dart'; import 'package:mymuseum_visitapp/Screens/Article/article_page.dart'; import 'package:mymuseum_visitapp/Screens/Quizz/quizz_page.dart'; import 'package:mymuseum_visitapp/Services/apiService.dart'; import 'package:mymuseum_visitapp/app_context.dart'; import 'package:mymuseum_visitapp/constants.dart'; import 'package:provider/provider.dart'; import 'section_card.dart'; class Body extends StatefulWidget { const Body({Key? key, required this.configurationId}) : super(key: key); final String? configurationId; @override State createState() => _BodyState(); } class _BodyState extends State { List sections = []; List sectionsToDisplay = []; String? searchValue; int? searchNumberValue; @override Widget build(BuildContext context) { final appContext = Provider.of(context); Size size = MediaQuery.of(context).size; return SafeArea( bottom: false, child: Column( children: [ Row( children: [ SearchBox(onChanged: (value) { setState(() { if(value != null && value != "") { searchValue = value; } else { searchValue = null; } }); }), Expanded( child: SearchNumberBox(onChanged: (value) { setState(() { if(value != null && value != "") { searchNumberValue = int.parse(value); } else { searchNumberValue = null; FocusScope.of(context).unfocus(); } }); }), ), ], ), //const SizedBox(height: kDefaultPadding / 2), Expanded( child: Stack( children: [ // Our background Container( margin: const EdgeInsets.only(top: 0), decoration: const BoxDecoration( color: kBackgroundColor, borderRadius: BorderRadius.only( topLeft: Radius.circular(40), topRight: Radius.circular(40), ), ), ), FutureBuilder( future: getSections(appContext), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { /*print("SECTIONTODISPA"); print(sectionsToDisplay);*/ return Padding( padding: const EdgeInsets.only(bottom: 0), child: RefreshIndicator( onRefresh: () async { if(!(appContext.getContext() as VisitAppContext).configuration!.isOffline!) { // Force refresh if online setState(() {}); } }, child: ListView.builder( itemCount: sectionsToDisplay.length, itemBuilder: (context, index) => SectionCard( itemCount: sectionsToDisplay.length, itemIndex: index, sectionDTO: sectionsToDisplay[index], press: () { switch(sectionsToDisplay[index].type) { case SectionType.Article: Navigator.push( context, MaterialPageRoute( builder: (context) => ArticlePage( visitAppContextIn: appContext.getContext(), articleId: sectionsToDisplay[index].id!, ), ), ); break; case SectionType.Quizz: Navigator.push( context, MaterialPageRoute( builder: (context) => QuizzPage( visitAppContextIn: appContext.getContext(), sectionId: sectionsToDisplay[index].id!, ), ), ); break; default: // TODO HANDLE, SHOW NOT SUPPORTED break; } }, ), ), ), ); } else if (snapshot.connectionState == ConnectionState.none) { return Text(TranslationHelper.getFromLocale("noData", appContext.getContext())); } else { return Center( child: Container( height: size.height * 0.15, child: Loading() ) ); } } ) ], ), ), ], ), ); } getSections(AppContext appContext) async { VisitAppContext visitAppContext = (appContext.getContext() as VisitAppContext); if(visitAppContext.configuration!.isOffline!) { // OFFLINE sections = List.from(await DatabaseHelper.instance.getData(DatabaseTableType.sections)); } else { // ONLINE List? sectionsDownloaded = await ApiService.getAllSections(appContext.clientAPI, visitAppContext.configuration!.id!); print(sectionsDownloaded); if(sectionsDownloaded!.isNotEmpty) { sections = sectionsDownloaded.where((s) => s.type == SectionType.Article || s.type == SectionType.Quizz).toList(); // TODO Support more than Article and Quizz section type print(sections); } } sections = sections.where((s) => s.configurationId == widget.configurationId).toList(); sections.sort((a,b) => a.order!.compareTo(b.order!)); sectionsToDisplay = sections; visitAppContext.currentSections = sectionsToDisplay; if(searchValue != '' && searchValue != null) { sectionsToDisplay = sections.where((s) => TranslationHelper.get(s.title, appContext.getContext()).toLowerCase().contains(searchValue.toString().toLowerCase())).toList(); } else { if(searchNumberValue != null) { sectionsToDisplay = sections.where((s) => s.order!+1 == searchNumberValue).toList(); } else { sectionsToDisplay = sections; } } } }