0

I have a problem with a simple thing but I don't how is it happening in the code. Below is the code:

  Widget build(BuildContext context) {
    String newTaskTitle;
    return Container(
      color: Color(0xff757575),
      child: Container(
        padding: EdgeInsets.all(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(
                fontSize: 30.0,
                color: Colors.lightBlueAccent,
              ),
            ),
            TextField(
              autofocus: true,
              textAlign: TextAlign.center,
              onChanged: (newText) {
                newTaskTitle = newText;
              },
            ),
            FlatButton(
              child: Text(
                'Add',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              color: Colors.lightBlueAccent,
              onPressed: () {
                print(newTaskTitle);
              },
            ),
          ],
        ),
      ),
    );
  }

I try to get the value of the text field input and pass it to another screen but when I type something then press the button, it gives me the null. enter image description here

3
  • 1
    Are you using the widget tree in another tree? Commented Aug 3, 2020 at 10:47
  • I placeed this screen in bottom pop up screen. Commented Aug 3, 2020 at 11:10
  • Please post the full code snippet of where you used this widget tree so I can help with what issue it is. Commented Aug 3, 2020 at 11:30

2 Answers 2

1

You can also achieve this using controllers. just initialize one and specify the controller in the TextField widget. To access the text, just do _controlller.text as you can see below:

  final _controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Color(0xff757575),
        child: Container(
          padding: EdgeInsets.all(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(
                  fontSize: 30.0,
                  color: Colors.lightBlueAccent,
                ),
              ),
              TextField(
                controller: _controller,
                autofocus: true,
                textAlign: TextAlign.center,
              ),
              FlatButton(
                child: Text(
                  'Add',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
                color: Colors.lightBlueAccent,
                onPressed: () {
                  print(_controller.text);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works well. but I didn't get my problem as it was a simple thing in programming.
1

You have defined String newTaskTitle inside the build() method try removing it from inside the build method and try defining it inside the StatefulWidget.

This is happening because TextField on change method calls build() method every time the text is changed

2 Comments

But i don't want the state other than passing the text of the text field, wasn't it making once of the whole screen?
Thank you, bro. you were right, I had to use the stateful widget.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.