import 'dart:convert'; import 'dart:developer'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:carousel_slider/carousel_slider.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.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/Helpers/ImageCustomProvider.dart'; import 'package:tablet_app/Models/ResponseSubDTO.dart'; import 'package:tablet_app/Models/tabletContext.dart'; import 'package:tablet_app/app_context.dart'; import 'package:tablet_app/constants.dart'; class ShowReponsesWidget extends StatefulWidget { List? questionsSubDTO; ShowReponsesWidget({this.questionsSubDTO}); @override _ShowReponsesWidget createState() => _ShowReponsesWidget(); } class _ShowReponsesWidget extends State { List _questionsSubDTO = []; CarouselController? sliderController; int currentIndex = 1; @override void initState() { super.initState(); sliderController = CarouselController(); _questionsSubDTO = widget.questionsSubDTO!; } @override void dispose() { sliderController = null; currentIndex = 1; super.dispose(); } @override Widget build(BuildContext context) { Size sizeAll = MediaQuery.of(context).size; Size size = Size(sizeAll.width * 0.65, sizeAll.height * 0.55); final appContext = Provider.of(context); TabletAppContext tabletAppContext = appContext.getContext() as TabletAppContext; var primaryColor = tabletAppContext.configuration != null ? tabletAppContext.configuration!.primaryColor != null ? new Color(int.parse(tabletAppContext.configuration!.primaryColor!.split('(0x')[1].split(')')[0], radix: 16)) : kTestSecondColor : kTestSecondColor; return Stack( children: [ if(_questionsSubDTO != null && _questionsSubDTO.length > 0) CarouselSlider( carouselController: sliderController, options: CarouselOptions( onPageChanged: (int index, CarouselPageChangedReason reason) { setState(() { currentIndex = index + 1; }); }, height: size.height, enlargeCenterPage: true, //scrollPhysics: NeverScrollableScrollPhysics(), reverse: false, ), items: _questionsSubDTO.map((i) { return Builder( builder: (BuildContext context) { return Padding( padding: const EdgeInsets.all(8.0), child: Container( width: size.width, height: size.height, margin: EdgeInsets.symmetric(horizontal: 5.0), decoration: BoxDecoration( color: kBackgroundLight, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(tabletAppContext.configuration!.roundedValue?.toDouble() ?? 20.0), image: i.resourceUrl != null ? new DecorationImage( fit: BoxFit.contain, opacity: 0.35, image: ImageCustomProvider.getImageProvider(appContext, i.resourceId!, i.resourceUrl!), ): null, boxShadow: [ BoxShadow( color: kBackgroundSecondGrey, spreadRadius: 0.5, blurRadius: 5, offset: Offset(0, 1.5), // changes position of shadow ), ], ), child: Column( //crossAxisAlignment: CrossAxisAlignment.center, //mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Padding( padding: const EdgeInsets.all(10.0), child: Container( //width: MediaQuery.of(context).size.width *0.65, //color: Colors.pink, height: size.height *0.25, child: Stack( children: [ Center( child: Container( decoration: BoxDecoration( color: kBackgroundLight, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(tabletAppContext.configuration!.roundedValue?.toDouble() ?? 10.0), boxShadow: [ BoxShadow( color: kBackgroundSecondGrey, spreadRadius: 0.3, blurRadius: 4, offset: Offset(0, 2), // changes position of shadow ), ], ), width: size.width *0.8, height: size.height *0.18, child: Center( child: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(1.0), child: HtmlWidget( i.label!.firstWhere((translation) => translation.language == appContext.getContext().language).value != null ? i.label!.firstWhere((translation) => translation.language == appContext.getContext().language).value! : "", textStyle: TextStyle(fontSize: kIsWeb ? kWebDescriptionSize : kDescriptionSize), customStylesBuilder: (element) { return {'text-align': 'center', 'font-family': "Roboto"}; }, ), ), ), ), ) ), ] ), ), ), Expanded( child: Padding( padding: const EdgeInsets.all(1.0), child: Container( height: size.height * 0.5, width: size.width * 0.88, //color: Colors.green, child: Padding( padding: const EdgeInsets.all(5.0), child: GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, mainAxisExtent: 65, mainAxisSpacing: 10, crossAxisSpacing: 5, ), itemCount: i.responsesSubDTO!.length, itemBuilder: (BuildContext ctx, index) { return Padding( padding: const EdgeInsets.all(5.0), child: Container( alignment: Alignment.center, child: HtmlWidget( i.responsesSubDTO![index].label!.firstWhere((translation) => translation.language == appContext.getContext().language).value != null ? i.responsesSubDTO![index].label!.firstWhere((translation) => translation.language == appContext.getContext().language).value! : "", textStyle: TextStyle(fontSize: kIsWeb ? kWebDescriptionSize : kDescriptionSize, color: i.chosen == index ? Colors.white : Colors.black), customStylesBuilder: (element) { return {'text-align': 'center', 'font-family': "Roboto"}; }, ), decoration: BoxDecoration( color: i.responsesSubDTO![index].isGood! ? kGreen : i.chosen == index ? kMainRed : kBackgroundLight, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(tabletAppContext.configuration!.roundedValue?.toDouble() ?? 10.0), boxShadow: [ BoxShadow( color: kBackgroundSecondGrey, spreadRadius: 0.3, blurRadius: 4, offset: Offset(0, 2), // changes position of shadow ), ], ), ), ); }), ), ), ), ), ], ) ), ); }, ); }).toList(), ), if(_questionsSubDTO != null && _questionsSubDTO.length > 1 && currentIndex != _questionsSubDTO.length && _questionsSubDTO[currentIndex-1].chosen != null && widget.questionsSubDTO!.length > 0) Positioned( top: size.height * 0.35, right: 35, child: InkWell( onTap: () { if(_questionsSubDTO[currentIndex-1].chosen != null && widget.questionsSubDTO!.length > 0) { sliderController!.nextPage(duration: new Duration(milliseconds: 500), curve: Curves.fastOutSlowIn); /*Fluttertoast.showToast( msg: "Vous n'avez pas répondu à cette question", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: kMainRed, textColor: Colors.white, fontSize: 35.0 );*/ } }, child: Icon( Icons.chevron_right, size: 90, color: primaryColor, ), ) ), if(_questionsSubDTO != null && _questionsSubDTO.length > 1 && currentIndex != 1) Positioned( top: size.height * 0.35, left: 35, child: InkWell( onTap: () { if(currentIndex > 1) sliderController!.previousPage(duration: new Duration(milliseconds: 500), curve: Curves.fastOutSlowIn); }, child: Icon( Icons.chevron_left, size: 90, color: primaryColor, ), ) ), if(_questionsSubDTO != null && _questionsSubDTO.length > 0) Padding( padding: const EdgeInsets.only(bottom: 0), child: Align( alignment: Alignment.bottomCenter, child: InkWell( child: Text( currentIndex.toString()+'/'+ widget.questionsSubDTO!.length.toString(), style: TextStyle(fontSize: 30, fontWeight: FontWeight.w500), ), ) ), ), if(_questionsSubDTO == null || _questionsSubDTO.length == 0) Center(child: Text("Aucune question à afficher", style: TextStyle(fontSize: kNoneInfoOrIncorrect),)) ] ); } }