295 lines
12 KiB
Dart
295 lines
12 KiB
Dart
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}) : super(key: key);
|
|
|
|
final ConfigurationDTO configuration;
|
|
|
|
@override
|
|
State<Body> createState() => _BodyState();
|
|
}
|
|
|
|
class _BodyState extends State<Body> {
|
|
late List<SectionDTO> sections;
|
|
late List<SectionDTO> _allSections;
|
|
late List<dynamic> rawSections;
|
|
String? searchValue;
|
|
int? searchNumberValue;
|
|
|
|
final ValueNotifier<List<SectionDTO>> filteredSections = ValueNotifier([]);
|
|
|
|
late Future<List<SectionDTO>> _futureSections;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final appContext = Provider.of<AppContext>(context, listen: false);
|
|
_futureSections = getSections(appContext);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(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: <Widget>[
|
|
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: <Widget>[
|
|
// 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<dynamic> 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<List<SectionDTO>>(
|
|
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: rawSections[index],
|
|
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<List<SectionDTO>> getSections(AppContext appContext) async {
|
|
VisitAppContext visitAppContext = appContext.getContext();
|
|
if(widget.configuration.isOffline!)
|
|
{
|
|
// OFFLINE
|
|
sections = List<SectionDTO>.from(await DatabaseHelper.instance.getData(DatabaseTableType.sections));
|
|
}
|
|
else
|
|
{
|
|
// ONLINE
|
|
List<dynamic>? sectionsDownloaded = await ApiService.getAllSections(visitAppContext.clientAPI, widget.configuration.id!);
|
|
rawSections = jsonDecode(jsonEncode(sectionsDownloaded));
|
|
var rawToSection = jsonDecode(jsonEncode(rawSections)).map((json) => SectionDTO.fromJson(json)).toList();
|
|
List<SectionDTO> sectionList = rawToSection.whereType<SectionDTO>().toList();
|
|
visitAppContext.currentSections = rawSections;
|
|
|
|
//print(sectionsDownloaded);
|
|
if(sectionList.isNotEmpty) {
|
|
sections = sectionList.toList();
|
|
//print(sections);
|
|
}
|
|
}
|
|
|
|
sections = sections.where((s) => s.configurationId == widget.configuration.id!).toList();
|
|
sections.sort((a,b) => a.order!.compareTo(b.order!));
|
|
|
|
_allSections = sections;
|
|
applyFilters(visitAppContext);
|
|
|
|
return _allSections;
|
|
}
|
|
|
|
void applyFilters(VisitAppContext visitAppContext) {
|
|
List<SectionDTO> result = _allSections;
|
|
|
|
if (searchValue != null && searchValue!.isNotEmpty) {
|
|
result = result.where((s) =>
|
|
removeDiacritics(TranslationHelper.get(s.title, visitAppContext).toLowerCase())
|
|
.contains(removeDiacritics(searchValue!.toLowerCase()))
|
|
).toList();
|
|
} else if (searchNumberValue != null) {
|
|
result = result.where((s) => s.order! + 1 == searchNumberValue).toList();
|
|
}
|
|
|
|
filteredSections.value = result;
|
|
}
|
|
|
|
}
|