I am making a TodoList and I am storing the value of a TextField in a variable using the onChanged parameter.
When I tap outside to dismiss the Software Keyboard on the iOS simulator it stores the value correctly.
If I use the enter /return button on the software Keyboard It stores null.
Why is this happening?
What can I do to avoid this behavior?
I am using the provider package as a state management solution.
class AddThingScreen extends StatelessWidget {
String title;
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xff757575),
child: Container(
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20),
topLeft: Radius.circular(20),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
'Add thing',
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 36.0,
),
),
TextField(
controller: textEditingController,
autofocus: true,
autocorrect: true,
textAlign: TextAlign.center,
decoration: InputDecoration(
focusColor: Colors.lightBlueAccent,
),
onChanged: (newThingTitle) {
title = newThingTitle;
},
),
FlatButton.icon(
color: Colors.lightBlueAccent,
icon: Icon(
Icons.add,
color: Colors.white,
),
label: Text(
'Add it',
style: TextStyle(
color: Colors.white,
),
textAlign: TextAlign.center,
),
onPressed: () {
Provider.of<ThingData>(context).addThing(title);
Navigator.pop(context);
},
)
],
),
),
);
}
}
As you can see I call a method I am storing in my ThingData class which adds a new thing to the list and then notifyListeners.
class ThingData extends ChangeNotifier {
List<Thing> _things = [
Thing(name: 'Buy Cheese', isDone: false),
Thing(name: 'Buy Flatbread', isDone: true),
Thing(name: 'Buy Hot Sauce', isDone: false),
];
int get thingCount {
return _things.length;
}
UnmodifiableListView<Thing> get thingsList {
return UnmodifiableListView<Thing>(_things);
}
void addThing(String newThingTitle) {
final thing = Thing(name: newThingTitle);
_things.add(thing);
notifyListeners();
}
}