Add screen
- Flutter course
- Beginner
- Lesson 72/99
- 4 min
Write the TextFormField screen to add a new task.
Navigator.push and pop
To add a new task, a separate screen is opened. Navigator.push opens a new page and Navigator.pop returns. If you give a value to pop, that value returns to the calling page and is added to the list as a new task.
Navigator.push(
context,
MaterialPageRoute(builder: (_) => AddScreen()),
);The add screen
The add screen contains a TextFormField and an ElevatedButton. When the button is pressed, you take the text from the controller and return with Navigator.pop(context, text). This text is added to the list on the main page.
Practice Write the add function
Write a function add that takes text (String) as a parameter and prints the concatenation of 'Eklendi: ' and text. In main call add with 'Yeni gorev'.
Starter code
import 'package:flutter/material.dart';
// ekle fonksiyonu yaz: parametre olarak text (String) alsin
// 'Eklendi: ' + text seklinde print ile yazdirsin
void main() {
// ekle'yi 'Yeni gorev' ile cagir
}Sample solution
import 'package:flutter/material.dart';
void ekle(String text) {
print('Eklendi: ' + text);
}
void main() {
ekle('Yeni gorev');
}Expected output
Eklendi: Yeni gorev
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens if a value is given to Navigator.pop?
- The keyboard closes
- The app crashes
- A new page opens
- Returns to the calling page Correct answer
Explanation: If you give a second value to Navigator.pop, that value returns to the calling page as a result; the app does not crash, no new page opens, and the keyboard does not close.
2 Navigator.pop call
Navigator.
____(context, 'Yeni gorev');
Navigator.
pop(context, 'Yeni gorev');
Explanation: To close the page and return a value, Navigator.pop is used; push opens a new page, and back and return are not valid Navigator methods.
3 Add screen parts
| Term | Meaning |
|---|---|
Navigator.push |
Opens a new page |
Navigator.pop |
Closes the page |
TextFormField |
Takes the task text |
ElevatedButton |
Triggers the add |