From ec4ca238de1f34c914dbd1ced88a43db01cc7c64 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 12 Mar 2021 16:37:21 +0100 Subject: [PATCH] Improve update message dialog --- lib/ui/screens/conversation_screen.dart | 16 +++-- lib/utils/ui_utils.dart | 96 +++++++++++++++++-------- 2 files changed, 78 insertions(+), 34 deletions(-) diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index 9d16351..9f1c9fd 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/helpers/events_helper.dart'; +import 'package:comunic/helpers/server_config_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/users_list.dart'; @@ -436,11 +437,16 @@ class _ConversationScreenState extends SafeState { /// Request message content update Future _updateMessage(ConversationMessage message) async { final newContent = await askUserString( - context: context, - title: tr("Update message"), - message: tr("Please enter new message content:"), - defaultValue: message.message.content, - hint: tr("New message")); + context: context, + title: tr("Update message"), + message: tr("Please enter new message content:"), + defaultValue: message.message.content, + hint: tr("New message"), + minLength: + ServerConfigurationHelper.config.conversationsPolicy.minMessageLen, + maxLength: + ServerConfigurationHelper.config.conversationsPolicy.maxMessageLen, + ); if (newContent == null) return; diff --git a/lib/utils/ui_utils.dart b/lib/utils/ui_utils.dart index 8bab2d7..5466a91 100644 --- a/lib/utils/ui_utils.dart +++ b/lib/utils/ui_utils.dart @@ -93,6 +93,7 @@ Future askUserString({ @required String defaultValue, @required String hint, int maxLength = 200, + int minLength = 1, }) async { assert(context != null); assert(title != null); @@ -105,35 +106,13 @@ Future askUserString({ final confirm = await showDialog( context: context, - builder: (c) => AlertDialog( - title: Text(title), - content: AutoSizeDialogContentWidget( - child: Column( - children: [ - Text(message), - TextField( - controller: controller, - maxLines: null, - maxLength: maxLength, - keyboardType: TextInputType.text, - decoration: InputDecoration( - labelText: hint, - alignLabelWithHint: true, - ), - ) - ], - ), - ), - actions: [ - FlatButton( - child: Text(tr("Cancel").toUpperCase()), - onPressed: () => Navigator.pop(c, false), - ), - FlatButton( - child: Text(tr("OK")), - onPressed: () => Navigator.pop(c, true), - ), - ], + builder: (c) => _InputTextDialog( + title: title, + message: message, + controller: controller, + maxLength: maxLength, + minLength: minLength, + hint: hint, )); if (confirm == null || !confirm) return null; @@ -141,6 +120,65 @@ Future askUserString({ return controller.text; } +class _InputTextDialog extends StatefulWidget { + final String title; + final String message; + final TextEditingController controller; + final int maxLength; + final int minLength; + final String hint; + + const _InputTextDialog({ + Key key, + @required this.title, + @required this.message, + @required this.controller, + @required this.maxLength, + @required this.minLength, + @required this.hint, + }) : super(key: key); + + @override + __InputTextDialogState createState() => __InputTextDialogState(); +} + +class __InputTextDialogState extends State<_InputTextDialog> { + @override + Widget build(BuildContext c) => AlertDialog( + title: Text(widget.title), + content: AutoSizeDialogContentWidget( + child: Column( + children: [ + Text(widget.message), + TextField( + controller: widget.controller, + maxLines: null, + maxLength: widget.maxLength, + keyboardType: TextInputType.text, + onChanged: (s) => setState(() {}), + decoration: InputDecoration( + labelText: widget.hint, + alignLabelWithHint: true, + ), + ) + ], + ), + ), + actions: [ + FlatButton( + child: Text(tr("Cancel").toUpperCase()), + onPressed: () => Navigator.pop(c, false), + ), + FlatButton( + child: Text(tr("OK")), + onPressed: widget.controller.text.length >= widget.minLength + ? () => Navigator.pop(c, true) + : null, + ), + ], + ); +} + /// Show an alert dialog to get user confirmation for something /// /// Return value of this function is never null