First try - get sections

This commit is contained in:
Thomas Fransolet 2021-07-06 18:37:50 +02:00
parent d0724b0099
commit fb202d252c
2 changed files with 77 additions and 56 deletions

View File

@ -1,5 +1,3 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/services.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/Components/webView.dart';
import 'package:tablet_app/Models/map-marker.dart'; import 'package:tablet_app/Models/map-marker.dart';
import 'package:tablet_app/Models/section.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/app_context.dart';
import 'package:tablet_app/constants.dart'; import 'package:tablet_app/constants.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
@ -25,7 +24,7 @@ class MainViewWidget extends StatefulWidget {
class _MainViewWidget extends State<MainViewWidget> { class _MainViewWidget extends State<MainViewWidget> {
Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution
Section sectionSelected; SectionDTO sectionSelected;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -35,8 +34,8 @@ class _MainViewWidget extends State<MainViewWidget> {
if(sectionSelected != null) { if(sectionSelected != null) {
var elementToShow; var elementToShow;
switch (sectionSelected.category) { switch (sectionSelected.type) {
case 0 : // MAP case SectionType.map : // MAP
elementToShow = ChangeNotifierProvider<MapContext>( elementToShow = ChangeNotifierProvider<MapContext>(
create: (_) => create: (_) =>
MapContext(new MapMarker( MapContext(new MapMarker(
@ -51,10 +50,10 @@ class _MainViewWidget extends State<MainViewWidget> {
: CircularProgressIndicator()),*/ : CircularProgressIndicator()),*/
); );
break; break;
case 1 : // WEB case SectionType.web : // WEB
elementToShow = WebViewWidget(url: 'Test'); elementToShow = WebViewWidget(url: 'Test');
break; break;
case 2 : case SectionType.slider :
elementToShow = Padding( elementToShow = Padding(
padding: const EdgeInsets.all(15.0), padding: const EdgeInsets.all(15.0),
child: Text('Hellow in') child: Text('Hellow in')
@ -96,7 +95,7 @@ class _MainViewWidget extends State<MainViewWidget> {
child: Align( child: Align(
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
child: AutoSizeText( child: AutoSizeText(
sectionSelected.title, sectionSelected.title.firstWhere((translation) => translation.language == appContext.getContext().language).value,
style: new TextStyle(fontSize: 25), style: new TextStyle(fontSize: 25),
maxLines: 1, maxLines: 1,
), ),
@ -106,7 +105,7 @@ class _MainViewWidget extends State<MainViewWidget> {
child: Align( child: Align(
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
child: AutoSizeText( child: AutoSizeText(
sectionSelected.description, sectionSelected.description.firstWhere((translation) => translation.language == appContext.getContext().language).value,
style: new TextStyle(fontSize: 20), style: new TextStyle(fontSize: 20),
maxLines: 2, maxLines: 2,
), ),
@ -183,56 +182,61 @@ class _MainViewWidget extends State<MainViewWidget> {
height: size.height * 0.9, height: size.height * 0.9,
width: size.width * 0.9, width: size.width * 0.9,
child: FutureBuilder( child: FutureBuilder(
future: getSections(), future: getSections(appContext),
builder: (context, AsyncSnapshot<dynamic> snapshot) { builder: (context, AsyncSnapshot<dynamic> snapshot) {
print('helloo test'); print('helloo test');
if (snapshot.connectionState == ConnectionState.done) { if (snapshot.connectionState == ConnectionState.done) {
return GridView.builder( if (snapshot.data == null) {
shrinkWrap: true, return Text("NO CONFIGURATION");
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4), }
itemCount: snapshot.data.length, else {
itemBuilder: (BuildContext context, int index) { return GridView.builder(
return // User Picture shrinkWrap: true,
InkWell( gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
onTap: () { itemCount: snapshot.data?.length,
setState(() { itemBuilder: (BuildContext context, int index) {
sectionSelected = snapshot.data[index]; return // User Picture
}); InkWell(
}, onTap: () {
child: Container( setState(() {
decoration: boxDecoration(snapshot.data[index]), sectionSelected = snapshot.data[index];
padding: const EdgeInsets.all(25), });
margin: EdgeInsets.symmetric(vertical: 25, horizontal: 25), },
child: Align( child: Container(
alignment: Alignment.bottomRight, decoration: boxDecoration(snapshot.data[index]),
child: FractionallySizedBox( padding: const EdgeInsets.all(25),
heightFactor: 0.4, margin: EdgeInsets.symmetric(vertical: 25, horizontal: 25),
child: Column( child: Align(
children: [ alignment: Alignment.bottomRight,
Align( child: FractionallySizedBox(
heightFactor: 0.4,
child: Column(
children: [
Align(
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
child: AutoSizeText( child: AutoSizeText(
snapshot.data[index].title, snapshot.data[index].title.firstWhere((translation) => translation.language == appContext.getContext().language).value,
style: new TextStyle(fontSize: 25), style: new TextStyle(fontSize: 25),
maxLines: 1, maxLines: 1,
), ),
), ),
Align( Align(
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
child: AutoSizeText( child: AutoSizeText(
snapshot.data[index].description, snapshot.data[index].description.firstWhere((translation) => translation.language == appContext.getContext().language).value,
style: new TextStyle(fontSize: 18, fontFamily: ""), style: new TextStyle(fontSize: 18, fontFamily: ""),
maxLines: 1, maxLines: 1,
), ),
), ),
], ],
) )
),
), ),
), ),
), );
); }
} );
); }
} else if (snapshot.connectionState == ConnectionState.none) { } else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data"); return Text("No data");
} else { } else {
@ -252,9 +256,22 @@ class _MainViewWidget extends State<MainViewWidget> {
} }
} }
Future<List<Section>> getSections() async { Future<List<SectionDTO>> 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<SectionDTO> 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'); final response = await http.get('https://jsonplaceholder.typicode.com/posts/1');
if (response.statusCode == 200) { if (response.statusCode == 200) {
@ -271,12 +288,12 @@ class _MainViewWidget extends State<MainViewWidget> {
} else { } else {
// If that call was not successful, throw an error. // If that call was not successful, throw an error.
throw Exception('Failed to load post'); throw Exception('Failed to load post');
} }*/
} }
} }
boxDecoration(Section section) { boxDecoration(SectionDTO section) {
return BoxDecoration( return BoxDecoration(
color: kBackgroundLight, color: kBackgroundLight,
shape: BoxShape.rectangle, shape: BoxShape.rectangle,
@ -285,7 +302,7 @@ boxDecoration(Section section) {
fit: BoxFit.cover, fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.dstATop), colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.dstATop),
image: new NetworkImage( image: new NetworkImage(
section.image, section.imageSource,
), ),
), ),
boxShadow: [ boxShadow: [

View File

@ -36,16 +36,20 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
TabletAppContext tabletAppContext;
Client clientAPI = new Client(); Client clientAPI = new Client();
TabletAppContext tabletAppContext;
@override @override
void initState() { void initState() {
if (widget.tabletAppContext == null) { if (widget.tabletAppContext == null) {
tabletAppContext = new TabletAppContext(); tabletAppContext = new TabletAppContext();
// store user info locally // store user info locally
tabletAppContext.clientAPI = clientAPI; tabletAppContext.clientAPI = clientAPI;
print(tabletAppContext); tabletAppContext.configuration = new ConfigurationDTO(id: "60b1257a2939c9163c3f0921");
tabletAppContext.language = "FR";
//appContext.setContext(tabletAppContext);
/*List<SectionDTO> sections = await clientAPI.sectionApi.sectionGetFromConfiguration("60b1257a2939c9163c3f0921"); /*List<SectionDTO> sections = await clientAPI.sectionApi.sectionGetFromConfiguration("60b1257a2939c9163c3f0921");
print("number of sections " + sections.length.toString()); print("number of sections " + sections.length.toString());
@ -62,7 +66,7 @@ class _MyAppState extends State<MyApp> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ChangeNotifierProvider<AppContext>( return ChangeNotifierProvider<AppContext>(
create: (_) => AppContext(widget.tabletAppContext), create: (_) => AppContext(tabletAppContext),
child: MaterialApp( child: MaterialApp(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
title: 'Tablet App Demo', title: 'Tablet App Demo',