From 9a5e9b74a0f563e2e210a3ea04d557502595fd0b Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Thu, 6 May 2021 22:27:22 +0200 Subject: [PATCH] Add multi string - translation logic (save) --- lib/Components/multi_input_modal.dart | 36 ++++-- .../multi_string_input_container.dart | 15 ++- lib/Components/translation_tab.dart | 110 ++++++++++++++++++ .../Section/section_detail_screen.dart | 28 +++-- pubspec.lock | 9 +- pubspec.yaml | 2 + 6 files changed, 179 insertions(+), 21 deletions(-) diff --git a/lib/Components/multi_input_modal.dart b/lib/Components/multi_input_modal.dart index cb51378..9e3a28d 100644 --- a/lib/Components/multi_input_modal.dart +++ b/lib/Components/multi_input_modal.dart @@ -1,17 +1,30 @@ import 'package:flutter/material.dart'; import 'package:manager_app/Components/rounded_button.dart'; import 'package:manager_app/Components/string_input_container.dart'; +import 'package:manager_app/Components/translation_tab.dart'; import 'package:manager_app/constants.dart'; import 'package:managerapi/api.dart'; +import 'package:collection/collection.dart'; -showMultiStringInput (List values, ValueChanged> onChanged, BuildContext context) { /*Function onSelect,*/ - List newValues = new List(); - newValues = values; - showDialog( - builder: (BuildContext context) => AlertDialog( - content: SingleChildScrollView( +showMultiStringInput (String text, List values, List newValues, Function onGetResult, int maxLines, BuildContext context) { /*Function onSelect,*/ + showDialog( + builder: (BuildContext context) => AlertDialog( + title: Text(text), + content: SingleChildScrollView( child: Column( - children: showValues(newValues), + children: [ + Container( + width: 500, + height: 200, + child: TranslationTab( + translations: newValues, + maxLines: maxLines + ) + ), + /*Column( + children: showValues(newValues), + ),*/ + ], ) ), actions: [ @@ -26,7 +39,7 @@ showMultiStringInput (List values, ValueChanged values, ValueChanged values, ValueChanged newValues) { diff --git a/lib/Components/multi_string_input_container.dart b/lib/Components/multi_string_input_container.dart index 31e69e3..4cdc897 100644 --- a/lib/Components/multi_string_input_container.dart +++ b/lib/Components/multi_string_input_container.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + import 'package:auto_size_text/auto_size_text.dart'; import 'package:flutter/material.dart'; import 'package:manager_app/Components/multi_input_modal.dart'; @@ -9,13 +11,15 @@ class MultiStringContainer extends StatelessWidget { final Color color; final String label; final List initialValue; - final ValueChanged> onChanged; + final Function onGetResult; + final int maxLines; const MultiStringContainer({ Key key, this.color = kSecond, this.label, this.initialValue, - this.onChanged, + this.onGetResult, + this.maxLines, }) : super(key: key); @override @@ -34,7 +38,12 @@ class MultiStringContainer extends StatelessWidget { width: size.width *0.15, child: InkWell( onTap: () { - showMultiStringInput(initialValue, onChanged, context); + List newValues = new List(); + // Make a copy + initialValue.forEach((value) { + newValues.add(TranslationDTO.fromJson(jsonDecode(jsonEncode(value)))); + }); + showMultiStringInput(label, initialValue, newValues, onGetResult, maxLines, context); }, child: Container( decoration: BoxDecoration( diff --git a/lib/Components/translation_tab.dart b/lib/Components/translation_tab.dart index e69de29..78707c5 100644 --- a/lib/Components/translation_tab.dart +++ b/lib/Components/translation_tab.dart @@ -0,0 +1,110 @@ +import 'package:flutter/material.dart'; +import 'package:manager_app/constants.dart'; +import 'package:managerapi/api.dart'; +class TranslationTab extends StatefulWidget { + final List translations; + final int maxLines; + const TranslationTab({ + Key key, + this.translations, + this.maxLines, + }) : super(key: key); + + @override + _TranslationTabState createState() => _TranslationTabState(); +} + +class _TranslationTabState extends State with SingleTickerProviderStateMixin { + TabController _tabController; + + @override + void initState() { + _tabController = new TabController(length: widget.translations.length, vsync: this); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + body: Container( + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + TabBar( + unselectedLabelColor: Colors.black, + labelColor: kPrimaryColor, + tabs: getTabs(widget.translations), + controller: _tabController, + indicatorSize: TabBarIndicatorSize.tab, + indicatorColor: kPrimaryColor, + ), + Expanded( + child: TabBarView( + children: getContent(widget.translations, widget.maxLines), + controller: _tabController, + ), + ), + ], + ), + ), + ); + } +} + +getContent(List translations, int maxLines) { + List tabsToShow = new List(); + translations.forEach((translation) { + tabsToShow.add( + new Padding( + padding: EdgeInsets.symmetric(horizontal: 8, vertical: 16), + child: TextFormField ( + keyboardType: TextInputType.multiline, + textInputAction: TextInputAction.newline, + minLines: 1, + maxLines: maxLines, + initialValue: translation.value, + onChanged: (String value) { + translation.value = value; + }, + cursorColor: kPrimaryColor, + decoration: InputDecoration( + enabledBorder: UnderlineInputBorder( + borderSide: BorderSide(color: kPrimaryColor), + ), + focusedBorder: UnderlineInputBorder( + borderSide: BorderSide(color: kPrimaryColor), + ), + border: UnderlineInputBorder( + borderSide: BorderSide(color: kPrimaryColor), + ), + ) + + ) + ), + /*new Padding( + padding: const EdgeInsets.all(10.0), + child: Container( + width: 200, + child: RoundedInputField( + color: kSecond, + textColor: kBlack, + initialValue: translation.value, + onChanged: (String value) { + translation.value = value; + }, + ), + ), + )*/ + ); + }); + return tabsToShow; +} + +getTabs(List newValues) { + List tabsToShow = new List(); + newValues.forEach((value) { + tabsToShow.add( + new Tab(text: value.language)); + }); + return tabsToShow; +} \ No newline at end of file diff --git a/lib/Screens/Configurations/Section/section_detail_screen.dart b/lib/Screens/Configurations/Section/section_detail_screen.dart index 1c5041c..1aecd54 100644 --- a/lib/Screens/Configurations/Section/section_detail_screen.dart +++ b/lib/Screens/Configurations/Section/section_detail_screen.dart @@ -155,16 +155,24 @@ class _SectionDetailScreenState extends State { MultiStringContainer( label: "Titre :", initialValue: sectionDTO.title, - onChanged: (value) { - sectionDTO.title = value; + onGetResult: (value) { + if (sectionDTO.title != value) { + sectionDTO.title = value; + save(true, sectionDTO, appContext); + } }, + maxLines: 1, ), MultiStringContainer( label: "Description :", initialValue: sectionDTO.description, - onChanged: (value) { - sectionDTO.description = value; + onGetResult: (value) { + if (sectionDTO.description != value) { + sectionDTO.description = value; + save(true, sectionDTO, appContext); + } }, + maxLines: 2, ) ], ) @@ -223,7 +231,7 @@ class _SectionDetailScreenState extends State { textColor: Colors.white, fontSize: 15, press: () { - save(sectionDTO, appContext); + save(false, sectionDTO, appContext); }, ), ), @@ -253,13 +261,19 @@ class _SectionDetailScreenState extends State { ); } - Future save(SectionDTO sectionDTO, AppContext appContext) async { + Future save(bool isTraduction, SectionDTO sectionDTO, AppContext appContext) async { + print("SAVE"); + print(sectionDTO); SectionDTO section = await appContext.getContext().clientAPI.sectionApi.sectionUpdate(sectionDTO); ManagerAppContext managerAppContext = appContext.getContext(); managerAppContext.selectedSection = section; appContext.setContext(managerAppContext); - showNotification(Colors.green, kWhite, 'La section a été sauvegardée avec succès', context); + if (isTraduction) { + showNotification(Colors.green, kWhite, 'Les traductions de la section ont été sauvegardées avec succès', context); + } else { + showNotification(Colors.green, kWhite, 'La section a été sauvegardée avec succès', context); + } } getSpecificData(SectionDTO sectionDTO, AppContext appContext) { diff --git a/pubspec.lock b/pubspec.lock index 45a58c8..2323672 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -44,12 +44,19 @@ packages: source: hosted version: "1.1.0" collection: - dependency: transitive + dependency: "direct main" description: name: collection url: "https://pub.dartlang.org" source: hosted version: "1.15.0" + convert: + dependency: "direct main" + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" cupertino_icons: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 9b0da39..860ff35 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,8 @@ dependencies: flutter_colorpicker: ^0.4.0 multiselect_formfield: ^0.1.6 material_segmented_control: ^3.1.2 + convert: ^3.0.0 + collection: any managerapi: path: manager_api