-
Notifications
You must be signed in to change notification settings - Fork 37
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 4 commits
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 | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -75,23 +75,27 @@ 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 ?? ''; | ||||||||||||||||||||||||||||||||||||
if (widget.values[widget.widgetId] == null && widget.initialValue != null) { | ||||||||||||||||||||||||||||||||||||
widget.values[widget.widgetId] = widget.initialValue; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||||||||||||
Widget build(BuildContext context) { | ||||||||||||||||||||||||||||||||||||
final value = widget.values[widget.widgetId]; | ||||||||||||||||||||||||||||||||||||
final currentValue = value is String ? value : null; | ||||||||||||||||||||||||||||||||||||
final text = currentValue ?? widget.label; | ||||||||||||||||||||||||||||||||||||
return FilterChip( | ||||||||||||||||||||||||||||||||||||
label: Text(_currentValue), | ||||||||||||||||||||||||||||||||||||
label: Text(text), | ||||||||||||||||||||||||||||||||||||
selected: false, | ||||||||||||||||||||||||||||||||||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)), | ||||||||||||||||||||||||||||||||||||
onSelected: (bool selected) { | ||||||||||||||||||||||||||||||||||||
_textController.text = currentValue ?? ''; | ||||||||||||||||||||||||||||||||||||
showModalBottomSheet<void>( | ||||||||||||||||||||||||||||||||||||
context: context, | ||||||||||||||||||||||||||||||||||||
builder: (BuildContext context) { | ||||||||||||||||||||||||||||||||||||
|
@@ -110,11 +114,11 @@ class _TextInputChipState extends State<_TextInputChip> { | |||||||||||||||||||||||||||||||||||
final newValue = _textController.text; | ||||||||||||||||||||||||||||||||||||
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 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.
Suggested change
|
||||||||||||||||||||||||||||||||||||
if (newValue.isNotEmpty) { | ||||||||||||||||||||||||||||||||||||
widget.values[widget.widgetId] = newValue; | ||||||||||||||||||||||||||||||||||||
setState(() { | ||||||||||||||||||||||||||||||||||||
_currentValue = newValue; | ||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||
Navigator.pop(context); | ||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||
widget.values.remove(widget.widgetId); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
setState(() {}); | ||||||||||||||||||||||||||||||||||||
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. For better readability and to follow idiomatic Flutter code style, it's recommended to wrap state-mutating logic inside the
Suggested change
|
||||||||||||||||||||||||||||||||||||
Navigator.pop(context); | ||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||
child: const Text('Done'), | ||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||
|
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) ↩