Add multi string - translation logic (save)

This commit is contained in:
Thomas Fransolet 2021-05-06 22:27:22 +02:00
parent 6b0f3145c1
commit 9a5e9b74a0
6 changed files with 179 additions and 21 deletions

View File

@ -1,17 +1,30 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:manager_app/Components/rounded_button.dart'; import 'package:manager_app/Components/rounded_button.dart';
import 'package:manager_app/Components/string_input_container.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:manager_app/constants.dart';
import 'package:managerapi/api.dart'; import 'package:managerapi/api.dart';
import 'package:collection/collection.dart';
showMultiStringInput (List<TranslationDTO> values, ValueChanged<List<TranslationDTO>> onChanged, BuildContext context) { /*Function onSelect,*/ showMultiStringInput (String text, List<TranslationDTO> values, List<TranslationDTO> newValues, Function onGetResult, int maxLines, BuildContext context) { /*Function onSelect,*/
List<TranslationDTO> newValues = new List<TranslationDTO>();
newValues = values;
showDialog( showDialog(
builder: (BuildContext context) => AlertDialog( builder: (BuildContext context) => AlertDialog(
title: Text(text),
content: SingleChildScrollView( content: SingleChildScrollView(
child: Column( child: Column(
children: [
Container(
width: 500,
height: 200,
child: TranslationTab(
translations: newValues,
maxLines: maxLines
)
),
/*Column(
children: showValues(newValues), children: showValues(newValues),
),*/
],
) )
), ),
actions: <Widget>[ actions: <Widget>[
@ -26,7 +39,7 @@ showMultiStringInput (List<TranslationDTO> values, ValueChanged<List<Translation
icon: Icons.undo, icon: Icons.undo,
color: kSecond, color: kSecond,
press: () { press: () {
onChanged(values); onGetResult(values);
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
fontSize: 20, fontSize: 20,
@ -41,7 +54,10 @@ showMultiStringInput (List<TranslationDTO> values, ValueChanged<List<Translation
color: kPrimaryColor, color: kPrimaryColor,
textColor: kWhite, textColor: kWhite,
press: () { press: () {
onChanged(newValues); Function deepEq = const DeepCollectionEquality().equals;
if (!deepEq(values, newValues)) {
onGetResult(newValues);
}
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
fontSize: 20, fontSize: 20,

View File

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:auto_size_text/auto_size_text.dart'; import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:manager_app/Components/multi_input_modal.dart'; import 'package:manager_app/Components/multi_input_modal.dart';
@ -9,13 +11,15 @@ class MultiStringContainer extends StatelessWidget {
final Color color; final Color color;
final String label; final String label;
final List<TranslationDTO> initialValue; final List<TranslationDTO> initialValue;
final ValueChanged<List<TranslationDTO>> onChanged; final Function onGetResult;
final int maxLines;
const MultiStringContainer({ const MultiStringContainer({
Key key, Key key,
this.color = kSecond, this.color = kSecond,
this.label, this.label,
this.initialValue, this.initialValue,
this.onChanged, this.onGetResult,
this.maxLines,
}) : super(key: key); }) : super(key: key);
@override @override
@ -34,7 +38,12 @@ class MultiStringContainer extends StatelessWidget {
width: size.width *0.15, width: size.width *0.15,
child: InkWell( child: InkWell(
onTap: () { onTap: () {
showMultiStringInput(initialValue, onChanged, context); List<TranslationDTO> newValues = new List<TranslationDTO>();
// Make a copy
initialValue.forEach((value) {
newValues.add(TranslationDTO.fromJson(jsonDecode(jsonEncode(value))));
});
showMultiStringInput(label, initialValue, newValues, onGetResult, maxLines, context);
}, },
child: Container( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(

View File

@ -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<TranslationDTO> translations;
final int maxLines;
const TranslationTab({
Key key,
this.translations,
this.maxLines,
}) : super(key: key);
@override
_TranslationTabState createState() => _TranslationTabState();
}
class _TranslationTabState extends State<TranslationTab> 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<TranslationDTO> translations, int maxLines) {
List<Widget> tabsToShow = new List<Widget>();
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<TranslationDTO> newValues) {
List<Tab> tabsToShow = new List<Tab>();
newValues.forEach((value) {
tabsToShow.add(
new Tab(text: value.language));
});
return tabsToShow;
}

View File

@ -155,16 +155,24 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
MultiStringContainer( MultiStringContainer(
label: "Titre :", label: "Titre :",
initialValue: sectionDTO.title, initialValue: sectionDTO.title,
onChanged: (value) { onGetResult: (value) {
if (sectionDTO.title != value) {
sectionDTO.title = value; sectionDTO.title = value;
save(true, sectionDTO, appContext);
}
}, },
maxLines: 1,
), ),
MultiStringContainer( MultiStringContainer(
label: "Description :", label: "Description :",
initialValue: sectionDTO.description, initialValue: sectionDTO.description,
onChanged: (value) { onGetResult: (value) {
if (sectionDTO.description != value) {
sectionDTO.description = value; sectionDTO.description = value;
save(true, sectionDTO, appContext);
}
}, },
maxLines: 2,
) )
], ],
) )
@ -223,7 +231,7 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
textColor: Colors.white, textColor: Colors.white,
fontSize: 15, fontSize: 15,
press: () { press: () {
save(sectionDTO, appContext); save(false, sectionDTO, appContext);
}, },
), ),
), ),
@ -253,14 +261,20 @@ class _SectionDetailScreenState extends State<SectionDetailScreen> {
); );
} }
Future<void> save(SectionDTO sectionDTO, AppContext appContext) async { Future<void> save(bool isTraduction, SectionDTO sectionDTO, AppContext appContext) async {
print("SAVE");
print(sectionDTO);
SectionDTO section = await appContext.getContext().clientAPI.sectionApi.sectionUpdate(sectionDTO); SectionDTO section = await appContext.getContext().clientAPI.sectionApi.sectionUpdate(sectionDTO);
ManagerAppContext managerAppContext = appContext.getContext(); ManagerAppContext managerAppContext = appContext.getContext();
managerAppContext.selectedSection = section; managerAppContext.selectedSection = section;
appContext.setContext(managerAppContext); appContext.setContext(managerAppContext);
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); showNotification(Colors.green, kWhite, 'La section a été sauvegardée avec succès', context);
} }
}
getSpecificData(SectionDTO sectionDTO, AppContext appContext) { getSpecificData(SectionDTO sectionDTO, AppContext appContext) {
switch(sectionDTO.type) { switch(sectionDTO.type) {

View File

@ -44,12 +44,19 @@ packages:
source: hosted source: hosted
version: "1.1.0" version: "1.1.0"
collection: collection:
dependency: transitive dependency: "direct main"
description: description:
name: collection name: collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.15.0" version: "1.15.0"
convert:
dependency: "direct main"
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
cupertino_icons: cupertino_icons:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -30,6 +30,8 @@ dependencies:
flutter_colorpicker: ^0.4.0 flutter_colorpicker: ^0.4.0
multiselect_formfield: ^0.1.6 multiselect_formfield: ^0.1.6
material_segmented_control: ^3.1.2 material_segmented_control: ^3.1.2
convert: ^3.0.0
collection: any
managerapi: managerapi:
path: manager_api path: manager_api