Fix text field value not preserved after scrolling

This commit is contained in:
Ming Ming 2021-07-25 19:41:05 +08:00
parent 5330fe81ae
commit 942f61d90d

View file

@ -66,7 +66,7 @@ class _AlbumViewerEditAppBarState extends State<AlbumViewerEditAppBar> {
@override @override
initState() { initState() {
super.initState(); super.initState();
_editNameValue = widget.album.name; _controller = TextEditingController(text: widget.album.name);
} }
@override @override
@ -78,28 +78,25 @@ class _AlbumViewerEditAppBarState extends State<AlbumViewerEditAppBar> {
background: background:
_getAppBarCover(context, widget.account, widget.coverPreviewUrl), _getAppBarCover(context, widget.account, widget.coverPreviewUrl),
title: TextFormField( title: TextFormField(
controller: _controller,
decoration: InputDecoration( decoration: InputDecoration(
hintText: L10n.of(context).nameInputHint, hintText: L10n.of(context).nameInputHint,
), ),
validator: (value) { validator: (_) {
if (value?.isNotEmpty == true) { // use _controller.text here because the value might be wrong if
// user scrolled the app bar off screen
if (_controller.text.isNotEmpty == true) {
return null; return null;
} else { } else {
return L10n.of(context).albumNameInputInvalidEmpty; return L10n.of(context).albumNameInputInvalidEmpty;
} }
}, },
onSaved: (value) { onSaved: (_) {
widget.onAlbumNameSaved?.call(value!); widget.onAlbumNameSaved?.call(_controller.text);
},
onChanged: (value) {
// need to save the value otherwise it'll return to the initial
// after scrolling out of the view
_editNameValue = value;
}, },
style: TextStyle( style: TextStyle(
color: AppTheme.getPrimaryTextColor(context), color: AppTheme.getPrimaryTextColor(context),
), ),
initialValue: _editNameValue,
), ),
), ),
leading: IconButton( leading: IconButton(
@ -112,7 +109,7 @@ class _AlbumViewerEditAppBarState extends State<AlbumViewerEditAppBar> {
); );
} }
late String _editNameValue; late TextEditingController _controller;
} }
Widget? _getAppBarCover( Widget? _getAppBarCover(