From fb202d252cc64f439e83444f5b8a4941a4a13bde Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Tue, 6 Jul 2021 18:37:50 +0200 Subject: [PATCH] First try - get sections --- lib/Components/MainView/main_view.dart | 123 ++++++++++++++----------- lib/main.dart | 10 +- 2 files changed, 77 insertions(+), 56 deletions(-) diff --git a/lib/Components/MainView/main_view.dart b/lib/Components/MainView/main_view.dart index 3d72848..29d5caa 100644 --- a/lib/Components/MainView/main_view.dart +++ b/lib/Components/MainView/main_view.dart @@ -1,5 +1,3 @@ -import 'dart:convert'; - import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; @@ -11,6 +9,7 @@ import 'package:tablet_app/Components/loading.dart'; import 'package:tablet_app/Components/webView.dart'; import 'package:tablet_app/Models/map-marker.dart'; import 'package:tablet_app/Models/section.dart'; +import 'package:tablet_app/Models/tabletContext.dart'; import 'package:tablet_app/app_context.dart'; import 'package:tablet_app/constants.dart'; import 'package:http/http.dart' as http; @@ -25,7 +24,7 @@ class MainViewWidget extends StatefulWidget { class _MainViewWidget extends State { Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution - Section sectionSelected; + SectionDTO sectionSelected; @override Widget build(BuildContext context) { @@ -35,8 +34,8 @@ class _MainViewWidget extends State { if(sectionSelected != null) { var elementToShow; - switch (sectionSelected.category) { - case 0 : // MAP + switch (sectionSelected.type) { + case SectionType.map : // MAP elementToShow = ChangeNotifierProvider( create: (_) => MapContext(new MapMarker( @@ -51,10 +50,10 @@ class _MainViewWidget extends State { : CircularProgressIndicator()),*/ ); break; - case 1 : // WEB + case SectionType.web : // WEB elementToShow = WebViewWidget(url: 'Test'); break; - case 2 : + case SectionType.slider : elementToShow = Padding( padding: const EdgeInsets.all(15.0), child: Text('Hellow in') @@ -96,7 +95,7 @@ class _MainViewWidget extends State { child: Align( alignment: Alignment.centerLeft, child: AutoSizeText( - sectionSelected.title, + sectionSelected.title.firstWhere((translation) => translation.language == appContext.getContext().language).value, style: new TextStyle(fontSize: 25), maxLines: 1, ), @@ -106,7 +105,7 @@ class _MainViewWidget extends State { child: Align( alignment: Alignment.centerLeft, child: AutoSizeText( - sectionSelected.description, + sectionSelected.description.firstWhere((translation) => translation.language == appContext.getContext().language).value, style: new TextStyle(fontSize: 20), maxLines: 2, ), @@ -183,56 +182,61 @@ class _MainViewWidget extends State { height: size.height * 0.9, width: size.width * 0.9, child: FutureBuilder( - future: getSections(), + future: getSections(appContext), builder: (context, AsyncSnapshot snapshot) { print('helloo test'); if (snapshot.connectionState == ConnectionState.done) { - return GridView.builder( - shrinkWrap: true, - gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4), - itemCount: snapshot.data.length, - itemBuilder: (BuildContext context, int index) { - return // User Picture - InkWell( - onTap: () { - setState(() { - sectionSelected = snapshot.data[index]; - }); - }, - child: Container( - decoration: boxDecoration(snapshot.data[index]), - padding: const EdgeInsets.all(25), - margin: EdgeInsets.symmetric(vertical: 25, horizontal: 25), - child: Align( - alignment: Alignment.bottomRight, - child: FractionallySizedBox( - heightFactor: 0.4, - child: Column( - children: [ - Align( + if (snapshot.data == null) { + return Text("NO CONFIGURATION"); + } + else { + return GridView.builder( + shrinkWrap: true, + gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4), + itemCount: snapshot.data?.length, + itemBuilder: (BuildContext context, int index) { + return // User Picture + InkWell( + onTap: () { + setState(() { + sectionSelected = snapshot.data[index]; + }); + }, + child: Container( + decoration: boxDecoration(snapshot.data[index]), + padding: const EdgeInsets.all(25), + margin: EdgeInsets.symmetric(vertical: 25, horizontal: 25), + child: Align( + alignment: Alignment.bottomRight, + child: FractionallySizedBox( + heightFactor: 0.4, + child: Column( + children: [ + Align( alignment: Alignment.centerRight, child: AutoSizeText( - snapshot.data[index].title, - style: new TextStyle(fontSize: 25), - maxLines: 1, + snapshot.data[index].title.firstWhere((translation) => translation.language == appContext.getContext().language).value, + style: new TextStyle(fontSize: 25), + maxLines: 1, ), - ), - Align( + ), + Align( alignment: Alignment.centerRight, child: AutoSizeText( - snapshot.data[index].description, - style: new TextStyle(fontSize: 18, fontFamily: ""), - maxLines: 1, + snapshot.data[index].description.firstWhere((translation) => translation.language == appContext.getContext().language).value, + style: new TextStyle(fontSize: 18, fontFamily: ""), + maxLines: 1, ), - ), - ], - ) + ), + ], + ) + ), ), ), - ), - ); - } - ); + ); + } + ); + } } else if (snapshot.connectionState == ConnectionState.none) { return Text("No data"); } else { @@ -252,9 +256,22 @@ class _MainViewWidget extends State { } } - Future> getSections() async { + Future> getSections(dynamic appContext) async { + TabletAppContext tabletAppContext = await appContext.getContext(); - print('in future'); + // TODO if null ask user to select a config (first use) + if (tabletAppContext.configuration == null) { + print("config is null"); + tabletAppContext.configuration = await tabletAppContext.clientAPI.configurationApi.configurationGetDetail("60b1257a2939c9163c3f0921"); + print("Set context.. if no config"); + appContext.setContext(tabletAppContext); + } + + print(tabletAppContext.toString()); + List sections = await tabletAppContext.clientAPI.sectionApi.sectionGetFromConfiguration(tabletAppContext.configuration.id); + print(sections); + return sections; + /*print('in future'); final response = await http.get('https://jsonplaceholder.typicode.com/posts/1'); if (response.statusCode == 200) { @@ -271,12 +288,12 @@ class _MainViewWidget extends State { } else { // If that call was not successful, throw an error. throw Exception('Failed to load post'); - } + }*/ } } -boxDecoration(Section section) { +boxDecoration(SectionDTO section) { return BoxDecoration( color: kBackgroundLight, shape: BoxShape.rectangle, @@ -285,7 +302,7 @@ boxDecoration(Section section) { fit: BoxFit.cover, colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.dstATop), image: new NetworkImage( - section.image, + section.imageSource, ), ), boxShadow: [ diff --git a/lib/main.dart b/lib/main.dart index decb955..ebc69d9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -36,16 +36,20 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - TabletAppContext tabletAppContext; Client clientAPI = new Client(); + TabletAppContext tabletAppContext; + @override void initState() { if (widget.tabletAppContext == null) { tabletAppContext = new TabletAppContext(); // store user info locally tabletAppContext.clientAPI = clientAPI; - print(tabletAppContext); + tabletAppContext.configuration = new ConfigurationDTO(id: "60b1257a2939c9163c3f0921"); + tabletAppContext.language = "FR"; + + //appContext.setContext(tabletAppContext); /*List sections = await clientAPI.sectionApi.sectionGetFromConfiguration("60b1257a2939c9163c3f0921"); print("number of sections " + sections.length.toString()); @@ -62,7 +66,7 @@ class _MyAppState extends State { @override Widget build(BuildContext context) { return ChangeNotifierProvider( - create: (_) => AppContext(widget.tabletAppContext), + create: (_) => AppContext(tabletAppContext), child: MaterialApp( debugShowCheckedModeBanner: false, title: 'Tablet App Demo',