import 'dart:convert';
import 'package:diacritic/diacritic.dart';
import 'package:flutter/material.dart';
import 'package:manager_api_new/api.dart';
import 'package:mymuseum_visitapp/Components/SlideFromRouteRight.dart';
import 'package:mymuseum_visitapp/Components/loading_common.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/visitContext.dart';
import 'package:mymuseum_visitapp/Screens/section_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.configuration, this.openSectionId}) : super(key: key);
final ConfigurationDTO configuration;
final String? openSectionId;
@override
State
createState() => _BodyState();
}
class _BodyState extends State {
late List sections;
late List _allSections;
/// Payload brut de chaque section, indexé par id. Il l'était par position dans
/// la liste de l'API : hors ligne cette liste n'existait pas (`late` jamais
/// assigné, donc `LateInitializationError` avalée dans le callback — le tap ne
/// faisait rien), et en ligne une recherche décalait les index, ouvrant une
/// autre section que celle touchée.
final Map _rawSectionById = {};
String? searchValue;
int? searchNumberValue;
final ValueNotifier> filteredSections = ValueNotifier([]);
late Future> _futureSections;
@override
void initState() {
super.initState();
final appContext = Provider.of(context, listen: false);
_futureSections = getSections(appContext);
// La section demandée n'est ouverte qu'une fois les sections chargées : son
// payload brut n'existe pas avant, et c'est lui que `SectionPage` affiche.
if (widget.openSectionId != null) {
_futureSections.then((_) => _openRequestedSection(appContext));
}
}
void _openRequestedSection(AppContext appContext) {
final raw = _rawSectionById[widget.openSectionId];
if (raw == null || !mounted) return;
Navigator.push(
context,
SlideFromRightRoute(
page: SectionPage(
configuration: widget.configuration,
rawSection: raw,
visitAppContextIn: appContext.getContext(),
sectionId: widget.openSectionId!,
),
),
);
}
@override
Widget build(BuildContext context) {
final appContext = Provider.of(context);
VisitAppContext visitAppContext = appContext.getContext();
Size size = MediaQuery.of(context).size;
Color? primaryColor = widget.configuration.primaryColor != null ? Color(int.parse(widget.configuration.primaryColor!.split('(0x')[1].split(')')[0], radix: 16)) : null;
return SafeArea(
bottom: false,
top: false,
child: Stack(
children: [
Hero(
tag: widget.configuration.id!,
child: Container(
height: size.height * 0.28,
decoration: BoxDecoration(
boxShadow: const [
BoxShadow(
color: kMainGrey,
spreadRadius: 0.5,
blurRadius: 5,
offset: Offset(0, 1), // changes position of shadow
),
],
gradient: widget.configuration.imageSource == null ? const LinearGradient(
begin: Alignment.centerRight,
end: Alignment.centerLeft,
colors: [
/*Color(0xFFDD79C2),
Color(0xFFB65FBE),
Color(0xFF9146BA),
Color(0xFF7633B8),
Color(0xFF6528B6),
Color(0xFF6025B6)*/
kMainColor0, //Color(0xFFf6b3c4)
kMainColor1,
kMainColor2,
],
) : null,
image: widget.configuration.imageSource != null ? DecorationImage(
fit: BoxFit.cover,
opacity: 0.65,
image: NetworkImage(
widget.configuration.imageSource!,
),
): null,
),
),
),
Column(
children: [
SizedBox(
height: size.height * 0.11,
width: size.width,
child: Stack(
fit: StackFit.expand,
children: [
Positioned(
top: 35,
left: 10,
child: SizedBox(
width: 50,
height: 50,
child: InkWell(
onTap: () {
//setState(() {
/**/
Navigator.of(context).pop();
visitAppContext.configuration = null;
visitAppContext.isScanningBeacons = false;
/*Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(
builder: (context) => const HomePage3(),
),(route) => false);*/
//});
},
child: Container(
decoration: BoxDecoration(
color: primaryColor,
shape: BoxShape.circle,
),
child: const Icon(Icons.arrow_back, size: 23, color: Colors.white)
),
)
),
),
],
),
),
Row(
children: [
SearchBox(onChanged: (value) {
searchValue = value?.trim();
applyFilters(visitAppContext);
}),
Expanded(
child: SearchNumberBox(onChanged: (value) {
if (value != null && value.isNotEmpty) {
searchNumberValue = int.tryParse(value);
} else {
searchNumberValue = null;
}
FocusScope.of(context).unfocus();
applyFilters(visitAppContext);
}
),
),
],
),
//const SizedBox(height: kDefaultPadding / 2),
Expanded(
child: Stack(
children: [
// Our background
Container(
margin: const EdgeInsets.only(top: 0),
decoration: const BoxDecoration(
boxShadow: [
BoxShadow(
color: kMainGrey,
spreadRadius: 0.5,
blurRadius: 2,
offset: Offset(0, 1), // changes position of shadow
),
],
color: kBackgroundColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
),
FutureBuilder(
future: _futureSections,
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
/*print("SECTIONTODISPA");
print(sectionsToDisplay);*/
return Padding(
padding: const EdgeInsets.only(bottom: 0),
child: RefreshIndicator(
color: kMainColor,
onRefresh: () async {
if(!widget.configuration.isOffline!) {
setState(() {
_futureSections = getSections(appContext);
});
} },
child: ValueListenableBuilder>(
valueListenable: filteredSections,
builder: (context, value, child) {
return ListView.builder(
itemCount: value.length,
itemBuilder: (context, index) => SectionCard(
configuration: widget.configuration,
itemCount: value.length,
itemIndex: index,
sectionDTO: value[index],
press: () {
Navigator.push(
context,
SlideFromRightRoute(page: SectionPage(
configuration: widget.configuration,
rawSection: _rawSectionById[value[index].id],
visitAppContextIn: appContext.getContext(),
sectionId: value[index].id!,
)),
);
},
),
);
}
)
),
);
} else if (snapshot.connectionState == ConnectionState.none) {
return Text(TranslationHelper.getFromLocale("noData", appContext.getContext()));
} else {
return Center(
child: SizedBox(
height: size.height * 0.15,
child: const LoadingCommon()
)
);
}
}
)
],
),
),
],
),
],
),
);
}
Future> getSections(AppContext appContext) async {
VisitAppContext visitAppContext = appContext.getContext();
sections = [];
_rawSectionById.clear();
if(widget.configuration.isOffline == true)
{
// OFFLINE
// Les lignes brutes, pas `getData` : celui-ci passe par `getSectionFromDB`,
// qui laisse la colonne `data` de côté — or c'est là que vit le payload typé
// dont `section_page` a besoin pour afficher quoi que ce soit.
List