-
Notifications
You must be signed in to change notification settings - Fork 46
Update text chip to display label when no text is entered #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"projects": { | ||
"default": "fluttergenui" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -75,23 +75,24 @@ class _TextInputChip extends StatefulWidget { | |||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
class _TextInputChipState extends State<_TextInputChip> { | ||||||||||||||||||||||||
late String _currentValue; | ||||||||||||||||||||||||
final TextEditingController _textController = TextEditingController(); | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The For example: @override
void dispose() {
_textController.dispose();
super.dispose();
} |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
@override | ||||||||||||||||||||||||
void initState() { | ||||||||||||||||||||||||
super.initState(); | ||||||||||||||||||||||||
_currentValue = widget.initialValue ?? widget.label; | ||||||||||||||||||||||||
_textController.text = widget.initialValue ?? ''; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
81
to
92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The To ensure
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
@override | ||||||||||||||||||||||||
Widget build(BuildContext context) { | ||||||||||||||||||||||||
final currentValue = widget.values[widget.widgetId] as String?; | ||||||||||||||||||||||||
|
final currentValue = widget.values[widget.widgetId] as String?; | |
final rawValue = widget.values[widget.widgetId]; | |
final currentValue = rawValue is String ? rawValue : null; |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The direct cast as String?
on line 88 is unsafe. Since widget.values
is a Map<String, Object?>
, the value for widget.widgetId
could be a non-String type, which would cause a TypeError
at runtime and crash the widget. It's safer to use a type check with is
.
final currentValue = widget.values[widget.widgetId] as String?; | |
final text = currentValue ?? widget.label; | |
final value = widget.values[widget.widgetId]; | |
final currentValue = value is String ? value : null; | |
final text = currentValue ?? widget.label; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new value from the text controller should be trimmed before checking if it's empty. This handles cases where a user might input only whitespace, which would currently result in an empty-looking chip instead of it reverting to the label text.
final newValue = _textController.text; | |
final newValue = _textController.text.trim(); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling setState
here is necessary because widget.values
was mutated directly on the previous line. While this works for this widget, directly mutating a widget
property like values
is an anti-pattern in Flutter and goes against common framework principles.1 It can lead to inconsistent UI state if other widgets also depend on this values
map but are not rebuilt.
A better approach is to use a callback to notify the parent widget of the change, and let the parent update the state. This is known as 'lifting state up' and leads to more predictable state management.
Style Guide References
Footnotes
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current logic prevents the user from clearing the input. If the user erases the text and presses 'Done', newValue.isNotEmpty
is false, and the modal sheet doesn't close, which is a poor user experience.
To fix this, you should handle the case where the new value is empty by removing the corresponding entry from widget.values
. The modal should always be closed when 'Done' is pressed.
if (newValue.isNotEmpty) {
widget.values[widget.widgetId] = newValue;
} else {
widget.values.remove(widget.widgetId);
}
setState(() {});
Navigator.pop(context);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good and correctly implement the desired functionality. However, I noticed that tests for this new behavior are missing. The repository's style guide states that code should be tested.1
Please consider adding a widget test for
_TextInputChip
to verify:label
when no value is provided.initialValue
when provided.label
when the user clears the text.Style Guide References
Footnotes
Code should be tested. (link) ↩