Я пытаюсь напечатать newTaskTitle , то есть текст, полученный из текстового поля. Это виджет без сохранения состояния. Но когда срабатывает нажатие кнопки, печатается null . Также печать значение правильно печатает набранный текст.
class AddTaskScreen extends StatelessWidget {
String newTaskTitle;
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xFF757575),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 20.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 30.0,
),
),
TextField(
textAlign: TextAlign.center,
onChanged: (value) {
newTaskTitle = value;
},
),
SizedBox(
height: 10.0,
),
FlatButton(
child: Text(
'Add',
style: TextStyle(color: Colors.white),
),
color: Colors.lightBlueAccent,
onPressed: () {
print(newTaskTitle); //This printing null...
},
)
],
),
),
);
}
}
Это результат Android Studio
W/IInputConnectionWrapper(11205): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(11205): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(11205): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(11205): endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(11205): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(11205): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(11205): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(11205): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(11205): endBatchEdit on inactive InputConnection
I/flutter (11205): null```