다른 위젯으로 Data 전달 (passing-data)
2022. 6. 17. 00:00ㆍFlutter
반응형
종종 새 화면으로 이동하고 싶을 뿐만 아니라
화면에도 데이터를 전달하고 싶을 때가 있다.
예를 들어 탭한 항목에 대한 정보를 전달할 수 있다.
이 예에서는 할 일 목록을 만든다.
할 일을 탭하면 할 일에 대한 정보를 표시하는 새 화면(위젯)으로 이동한다.
할 일 클래스 정의
class Todo {
final String title;
final String description;
const Todo(this.title, this.description);
}
할 일 목록 만들기
final todos = List.generate(
20,
(i) => Todo(
'Todo $i',
'A description of what needs to be done for Todo $i',
),
);
ListView를 사용하여 할 일 목록 표시
ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(todos[index].title),
);
},
),
목록을 표시하는 할 일 화면 만들기
class TodosScreen extends StatelessWidget {
// Requiring the list of todos.
const TodosScreen({super.key, required this.todos});
final List<Todo> todos;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Todos'),
),
//passing in the ListView.builder
body: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(todos[index].title),
);
},
),
);
}
}
할 일에 대한 정보를 표시할 수 있는 세부 정보 화면을 만들기
class DetailScreen extends StatelessWidget {
// In the constructor, require a Todo.
const DetailScreen({super.key, required this.todo});
// Declare a field that holds the Todo.
final Todo todo;
@override
Widget build(BuildContext context) {
// Use the Todo to create the UI.
return Scaffold(
appBar: AppBar(
title: Text(todo.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(todo.description),
),
);
}
}
세부 정보 화면으로 이동하여 데이터를 전달
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]),
),
);
},
);
},
),
RouteSettings 를 통해 위 과정 다시 구현
class DetailScreen extends StatelessWidget {
const DetailScreen({super.key});
@override
Widget build(BuildContext context) {
final todo = ModalRoute.of(context)!.settings.arguments as Todo;
// Use the Todo to create the UI.
return Scaffold(
appBar: AppBar(
title: Text(todo.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(todo.description),
),
);
}
}
TodosScreen 수정
class TodosScreen extends StatelessWidget {
const TodosScreen({super.key, required this.todos});
final List<Todo> todos;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const 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) => const DetailScreen(),
// Pass the arguments as part of the RouteSettings. The
// DetailScreen reads the arguments from these settings.
settings: RouteSettings(
arguments: todos[index],
),
),
);
},
);
},
),
);
}
}
관련영상
반응형
'Flutter' 카테고리의 다른 글
Flutter 상태관리 (Bloc - bloc) (0) | 2022.06.21 |
---|---|
Flutter 상태관리 (Bloc - Cubit) (0) | 2022.06.20 |
이전화면으로 이동시에 Data 전달 (Return Data) (0) | 2022.06.16 |
페이지 이동시 인수 (argument) 전달 (0) | 2022.06.15 |
명명된 경로(named routes) 를 이용한 페이지이동 (0) | 2022.06.14 |