Created
July 25, 2020 23:50
-
-
Save Nailik/02aee5644f1a1d89336c6bebc992e464 to your computer and use it in GitHub Desktop.
Send data to a new screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
class Todo { | |
final String title; | |
final String description; | |
Todo(this.title, this.description); | |
} | |
void main() { | |
runApp(MaterialApp( | |
title: 'Passing Data', | |
home: TodosScreen( | |
todos: List.generate( | |
20, | |
(i) => Todo( | |
'Todo $i', | |
'A description of what needs to be done for Todo $i', | |
), | |
), | |
), | |
)); | |
} | |
class TodosScreen extends StatelessWidget { | |
final List<Todo> todos; | |
TodosScreen({Key key, @required this.todos}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Todos'), | |
), | |
body: ListView.builder( | |
itemCount: todos.length, | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: Text(todos[index].title), | |
// When a user taps the ListTile, navigate to the DetailScreen. | |
// Notice that you're not only creating a DetailScreen, you're | |
// also passing the current todo through to it. | |
onTap: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => DetailScreen(todo: todos[index]), | |
), | |
); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
} | |
class DetailScreen extends StatelessWidget { | |
// Declare a field that holds the Todo. | |
final Todo todo; | |
// In the constructor, require a Todo. | |
DetailScreen({Key key, @required this.todo}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
// Use the Todo to create the UI. | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(todo.title), | |
), | |
body: Padding( | |
padding: EdgeInsets.all(16.0), | |
child: Text(todo.description), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment