Skip to content

Commit bec7d0f

Browse files
committed
added max length
1 parent 7184638 commit bec7d0f

File tree

5 files changed

+32
-63
lines changed

5 files changed

+32
-63
lines changed

example/lib/pages/text_input_dialog_page.dart

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class TextInputDialogPage extends ConsumerWidget {
5757
textFields: const [
5858
DialogTextField(
5959
hintText: 'hintText',
60+
maxLenght: 24,
6061
),
6162
],
6263
title: 'Hello',
@@ -73,14 +74,11 @@ class TextInputDialogPage extends ConsumerWidget {
7374
textFields: [
7475
DialogTextField(
7576
hintText: 'hintText',
76-
validator: (value) =>
77-
value!.isEmpty ? 'Input more than one character' : null,
77+
validator: (value) => value!.isEmpty ? 'Input more than one character' : null,
7878
),
7979
DialogTextField(
8080
hintText: 'hintText',
81-
validator: (value) => value!.length < 2
82-
? 'Input more than two characters'
83-
: null,
81+
validator: (value) => value!.length < 2 ? 'Input more than two characters' : null,
8482
),
8583
],
8684
title: 'Hello',
@@ -97,14 +95,11 @@ class TextInputDialogPage extends ConsumerWidget {
9795
textFields: [
9896
DialogTextField(
9997
hintText: 'hintText',
100-
validator: (value) =>
101-
value!.isEmpty ? 'Input more than one character' : null,
98+
validator: (value) => value!.isEmpty ? 'Input more than one character' : null,
10299
),
103100
DialogTextField(
104101
hintText: 'hintText',
105-
validator: (value) => value!.length < 2
106-
? 'Input more than two characters'
107-
: null,
102+
validator: (value) => value!.length < 2 ? 'Input more than two characters' : null,
108103
),
109104
],
110105
title: 'Hello',
@@ -166,12 +161,8 @@ class TextInputDialogPage extends ConsumerWidget {
166161
hintText: 'Start with "F"',
167162
retryTitle: 'Incorrect',
168163
retryMessage: 'Retry?',
169-
retryOkLabel: ref
170-
.watch(adaptiveStyleProvider)
171-
.effectiveStyle(theme)
172-
.isMaterial(theme)
173-
? 'RETRY'
174-
: 'Retry',
164+
retryOkLabel:
165+
ref.watch(adaptiveStyleProvider).effectiveStyle(theme).isMaterial(theme) ? 'RETRY' : 'Retry',
175166
);
176167
logger.info('ok: $ok');
177168
if (!ok) {

lib/src/text_input_dialog/ios_text_input_dialog.dart

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ class IOSTextInputDialog extends StatefulWidget {
3434
}
3535

3636
class _IOSTextInputDialogState extends State<IOSTextInputDialog> {
37-
late final List<TextEditingController> _textControllers = widget.textFields
38-
.map((tf) => TextEditingController(text: tf.initialText))
39-
.toList();
37+
late final List<TextEditingController> _textControllers =
38+
widget.textFields.map((tf) => TextEditingController(text: tf.initialText)).toList();
4039
String? _validationMessage;
4140
bool _autovalidate = false;
4241

@@ -131,6 +130,7 @@ class _IOSTextInputDialogState extends State<IOSTextInputDialog> {
131130
placeholder: field.hintText,
132131
obscureText: field.obscureText,
133132
keyboardType: field.keyboardType,
133+
maxLength: field.maxLenght,
134134
minLines: field.minLines,
135135
maxLines: field.maxLines,
136136
autocorrect: field.autocorrect,
@@ -141,9 +141,7 @@ class _IOSTextInputDialogState extends State<IOSTextInputDialog> {
141141
isBottomRounded: i == _textControllers.length - 1,
142142
),
143143
textInputAction: isLast ? null : TextInputAction.next,
144-
onSubmitted: isLast && widget.autoSubmit
145-
? (_) => submitIfValid()
146-
: null,
144+
onSubmitted: isLast && widget.autoSubmit ? (_) => submitIfValid() : null,
147145
);
148146
},
149147
),
@@ -170,20 +168,15 @@ class _IOSTextInputDialogState extends State<IOSTextInputDialog> {
170168
onPressed: cancel,
171169
isDefaultAction: true,
172170
child: Text(
173-
widget.cancelLabel ??
174-
MaterialLocalizations.of(context)
175-
.cancelButtonLabel
176-
.capitalizedForce,
171+
widget.cancelLabel ?? MaterialLocalizations.of(context).cancelButtonLabel.capitalizedForce,
177172
),
178173
),
179174
CupertinoDialogAction(
180175
onPressed: submitIfValid,
181176
child: Text(
182177
widget.okLabel ?? MaterialLocalizations.of(context).okButtonLabel,
183178
style: TextStyle(
184-
color: widget.isDestructiveAction
185-
? CupertinoColors.systemRed.resolveFrom(context)
186-
: null,
179+
color: widget.isDestructiveAction ? CupertinoColors.systemRed.resolveFrom(context) : null,
187180
),
188181
),
189182
),

lib/src/text_input_dialog/macos_text_input_dialog.dart

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ class MacOSTextInputDialog extends StatefulWidget {
3737
}
3838

3939
class _MacOSTextInputDialogState extends State<MacOSTextInputDialog> {
40-
late final List<TextEditingController> _textControllers = widget.textFields
41-
.map((tf) => TextEditingController(text: tf.initialText))
42-
.toList();
40+
late final List<TextEditingController> _textControllers =
41+
widget.textFields.map((tf) => TextEditingController(text: tf.initialText)).toList();
4342
String? _validationMessage;
4443
bool _autovalidate = false;
4544

@@ -141,16 +140,12 @@ class _MacOSTextInputDialogState extends State<MacOSTextInputDialog> {
141140
keyboardType: field.keyboardType,
142141
minLines: field.minLines,
143142
maxLines: field.maxLines,
143+
maxLength: field.maxLenght,
144144
autocorrect: field.autocorrect,
145-
prefix:
146-
prefixText == null ? null : Text(prefixText),
147-
suffix:
148-
suffixText == null ? null : Text(suffixText),
149-
textInputAction:
150-
isLast ? null : TextInputAction.next,
151-
onSubmitted: isLast && widget.autoSubmit
152-
? (_) => submitIfValid()
153-
: null,
145+
prefix: prefixText == null ? null : Text(prefixText),
146+
suffix: suffixText == null ? null : Text(suffixText),
147+
textInputAction: isLast ? null : TextInputAction.next,
148+
onSubmitted: isLast && widget.autoSubmit ? (_) => submitIfValid() : null,
154149
),
155150
),
156151
);
@@ -175,10 +170,7 @@ class _MacOSTextInputDialogState extends State<MacOSTextInputDialog> {
175170
isSecondary: true,
176171
onPressed: cancel,
177172
child: Text(
178-
widget.cancelLabel ??
179-
MaterialLocalizations.of(context)
180-
.cancelButtonLabel
181-
.capitalizedForce,
173+
widget.cancelLabel ?? MaterialLocalizations.of(context).cancelButtonLabel.capitalizedForce,
182174
),
183175
),
184176
const SizedBox(width: 14),
@@ -187,12 +179,9 @@ class _MacOSTextInputDialogState extends State<MacOSTextInputDialog> {
187179
onPressed: submitIfValid,
188180
isSecondary: widget.isDestructiveAction,
189181
child: Text(
190-
widget.okLabel ??
191-
MaterialLocalizations.of(context).okButtonLabel,
182+
widget.okLabel ?? MaterialLocalizations.of(context).okButtonLabel,
192183
style: TextStyle(
193-
color: widget.isDestructiveAction
194-
? CupertinoColors.systemRed.resolveFrom(context)
195-
: null,
184+
color: widget.isDestructiveAction ? CupertinoColors.systemRed.resolveFrom(context) : null,
196185
),
197186
),
198187
),

lib/src/text_input_dialog/material_text_input_dialog.dart

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class MaterialTextInputDialog extends StatefulWidget {
2020
this.autoSubmit = false,
2121
});
2222
@override
23-
_MaterialTextInputDialogState createState() =>
24-
_MaterialTextInputDialogState();
23+
_MaterialTextInputDialogState createState() => _MaterialTextInputDialogState();
2524

2625
final List<DialogTextField> textFields;
2726
final String? title;
@@ -38,9 +37,8 @@ class MaterialTextInputDialog extends StatefulWidget {
3837
}
3938

4039
class _MaterialTextInputDialogState extends State<MaterialTextInputDialog> {
41-
late final List<TextEditingController> _textControllers = widget.textFields
42-
.map((tf) => TextEditingController(text: tf.initialText))
43-
.toList();
40+
late final List<TextEditingController> _textControllers =
41+
widget.textFields.map((tf) => TextEditingController(text: tf.initialText)).toList();
4442
final _formKey = GlobalKey<FormState>();
4543
var _autovalidateMode = AutovalidateMode.disabled;
4644

@@ -85,8 +83,7 @@ class _MaterialTextInputDialogState extends State<MaterialTextInputDialog> {
8583
final cancelLabel = widget.cancelLabel;
8684
final okLabel = widget.okLabel;
8785
final okText = Text(
88-
(widget.fullyCapitalized ? okLabel?.toUpperCase() : okLabel) ??
89-
MaterialLocalizations.of(context).okButtonLabel,
86+
(widget.fullyCapitalized ? okLabel?.toUpperCase() : okLabel) ?? MaterialLocalizations.of(context).okButtonLabel,
9087
style: TextStyle(
9188
color: widget.isDestructiveAction ? colorScheme.error : null,
9289
),
@@ -122,6 +119,7 @@ class _MaterialTextInputDialogState extends State<MaterialTextInputDialog> {
122119
keyboardType: textField.keyboardType,
123120
minLines: textField.minLines,
124121
maxLines: textField.maxLines,
122+
maxLength: textField.maxLenght,
125123
autocorrect: textField.autocorrect,
126124
decoration: InputDecoration(
127125
hintText: textField.hintText,
@@ -131,9 +129,7 @@ class _MaterialTextInputDialogState extends State<MaterialTextInputDialog> {
131129
validator: textField.validator,
132130
autovalidateMode: _autovalidateMode,
133131
textInputAction: isLast ? null : TextInputAction.next,
134-
onFieldSubmitted: isLast && widget.autoSubmit
135-
? (_) => submitIfValid()
136-
: null,
132+
onFieldSubmitted: isLast && widget.autoSubmit ? (_) => submitIfValid() : null,
137133
);
138134
})
139135
],
@@ -142,9 +138,7 @@ class _MaterialTextInputDialogState extends State<MaterialTextInputDialog> {
142138
TextButton(
143139
onPressed: cancel,
144140
child: Text(
145-
(widget.fullyCapitalized
146-
? cancelLabel?.toUpperCase()
147-
: cancelLabel) ??
141+
(widget.fullyCapitalized ? cancelLabel?.toUpperCase() : cancelLabel) ??
148142
MaterialLocalizations.of(context).cancelButtonLabel,
149143
),
150144
),

lib/src/text_input_dialog/show_text_input_dialog.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class DialogTextField {
117117
this.suffixText,
118118
this.minLines,
119119
this.maxLines = 1,
120+
this.maxLenght,
120121
this.autocorrect = true,
121122
});
122123
final String? initialText;
@@ -128,5 +129,6 @@ class DialogTextField {
128129
final String? suffixText;
129130
final int? minLines;
130131
final int maxLines;
132+
final int? maxLenght;
131133
final bool autocorrect;
132134
}

0 commit comments

Comments
 (0)