■ Navigator 클래스의 pushNamed 정적 메소드를 사용해 화면을 이동하는 방법을 보여준다.
▶ person.dart
1 2 3 4 5 6 7 8 |
class Person { String name; int age; Person(this.name, this.age); } |
▶ detail_page.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import 'package:flutter/material.dart'; import 'package:test_app/person.dart'; class DetailPage extends StatelessWidget { @override Widget build(BuildContext context) { final Person person = ModalRoute.of(context).settings.arguments; return Scaffold( appBar: AppBar( title: Text('Test Application / Detail Page'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( '${person.name}, ${person.age}' ), SizedBox( height: 10, ), RaisedButton( child: Text('메인 페이지'), color: Colors.orange, onPressed: () { Navigator.pop(context, 'OK'); }, ) ], ), ), ); } } |
▶ main_page.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import 'package:flutter/material.dart'; import 'package:test_app/person.dart'; import 'package:test_app/detail_page.dart'; class MainPage extends StatefulWidget { @override _MainPageState createState() => _MainPageState(); } class _MainPageState extends State<MainPage> { String _result; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Test Application / Main Page'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ RaisedButton( child: Text('상세 페이지'), color: Colors.orange, onPressed: () async { setState(() { _result = ''; }); final Person person = Person('홍길동', 20); final result = await Navigator.pushNamed( context, '/detail', arguments: person, ); setState(() { _result = result; }); }, ), Text( '결과 : $_result', ), ], ), ), ); } } |
▶ main.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import 'package:flutter/material.dart'; import 'package:test_app/main_page.dart'; import 'package:test_app/detail_page.dart'; void main() { runApp(TestApplication()); } class TestApplication extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Test Application', theme: ThemeData( primarySwatch: Colors.blue, ), home: MainPage(), routes: { '/main': (context) => MainPage(), '/detail': (context) => DetailPage(), }, ); } } |