2020-10-28

On printing the value of 'newTaskTitle' inside TextField, it works fine. But when I try to print it from the FlatButton it shows 'null'

Why is the String variable newTaskTitle not updating across the entire class when I update it inside the text field? when printing it from the flat button it gets backs to its default null value. Anything i change on it inside the onChange brackets doesn't reflect across the entire code.

class AddTaskScreen extends StatelessWidget {
  Color themeColor = TasksScreen().themeColor;

  Function addTaskCallback;
  AddTaskScreen(this.addTaskCallback);

  @override
  Widget build(BuildContext context) {

    **String newTaskTitle;**

    return Container(
      color: Color(0xFF757575),
      child: Container(
        padding: EdgeInsets.all(40),
        decoration: BoxDecoration(
            color: themeColor,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(30),
              topRight: Radius.circular(30),
            )),
        child: Column(
          children: [
            Text(
              'ADD TASK',
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 30,
                  fontWeight: FontWeight.bold),
            ),
            TextField(
              textAlign: TextAlign.center,
              onChanged: (value) {
                newTaskTitle = value;
                print('print from textfield=$newTaskTitle');
              },
            ),
            SizedBox(
              height: 20,
            ),
            FlatButton(
              onPressed: () {
                print('from flatbutton = $newTaskTitle');
              },
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 10, horizontal: 30),
                color: Colors.white,
                child: Text(
                  'Add',
                  style: TextStyle(
                    color: themeColor,
                    fontWeight: FontWeight.bold,
                    fontSize: 20,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}


from Recent Questions - Stack Overflow https://ift.tt/31NaWIb
https://ift.tt/eA8V8J

No comments:

Post a Comment