From b4465cc70cbdcf03dfa362caca0ba318db4f9e5b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 18 May 2020 17:58:54 +0200 Subject: [PATCH] Allow to create new choices on survey on its creation --- lib/helpers/posts_helper.dart | 1 + lib/models/new_post.dart | 5 ++++- lib/ui/dialogs/new_survey_dialog.dart | 21 ++++++++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index 9b4bfd4..ceb0a10 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -165,6 +165,7 @@ class PostsHelper { case PostKind.SURVEY: request.addString("question", post.survey.question); request.addString("answers", post.survey.answers.join("<>")); + request.addBool("allowNewAnswers", post.survey.allowNewChoicesCreation); break; case PostKind.YOUTUBE: diff --git a/lib/models/new_post.dart b/lib/models/new_post.dart index 0799b29..7a5236a 100644 --- a/lib/models/new_post.dart +++ b/lib/models/new_post.dart @@ -12,12 +12,15 @@ import 'package:meta/meta.dart'; class NewSurvey { final String question; final Set answers; + final bool allowNewChoicesCreation; const NewSurvey({ @required this.question, @required this.answers, + @required this.allowNewChoicesCreation, }) : assert(question != null), - assert(answers.length > 1); + assert(answers.length > 1), + assert(allowNewChoicesCreation != null); } class NewPost { diff --git a/lib/ui/dialogs/new_survey_dialog.dart b/lib/ui/dialogs/new_survey_dialog.dart index 9c746b9..8a6fbae 100644 --- a/lib/ui/dialogs/new_survey_dialog.dart +++ b/lib/ui/dialogs/new_survey_dialog.dart @@ -29,12 +29,15 @@ class __SurveyDialogState extends State<_SurveyDialog> { TextEditingController _questionController; final _choiceController = TextEditingController(); var _choices = Set(); + var _allowNewChoices = false; bool get canConfirm => _questionController.text.length > 2 && _choices.length > 1; - NewSurvey get newSurvey => - NewSurvey(question: _questionController.text, answers: _choices); + NewSurvey get newSurvey => NewSurvey( + question: _questionController.text, + answers: _choices, + allowNewChoicesCreation: _allowNewChoices); @override void initState() { @@ -42,7 +45,10 @@ class __SurveyDialogState extends State<_SurveyDialog> { _questionController = TextEditingController( text: widget.initialSurvey == null ? "" : widget.initialSurvey.question, ); - if (widget.initialSurvey != null) _choices = widget.initialSurvey.answers; + if (widget.initialSurvey != null) { + _choices = widget.initialSurvey.answers; + _allowNewChoices = widget.initialSurvey.allowNewChoicesCreation; + } } @override @@ -105,6 +111,15 @@ class __SurveyDialogState extends State<_SurveyDialog> { : null, )), ), + + // Allow users to create new choices + ListTile( + leading: Switch.adaptive( + value: _allowNewChoices, + onChanged: (v) => setState(() => _allowNewChoices = v), + ), + title: Text(tr("Allow users to create new choices")), + ) ]), ), );