Advertisement
yudiwibisono

flutter_named_route

Apr 19th, 2022
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.42 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. class LayarKedua extends StatelessWidget {
  4.   const LayarKedua({Key? key}) : super(key: key);
  5.  
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return Scaffold(
  9.         appBar: AppBar(
  10.           title: const Text('Screen 2'),
  11.         ),
  12.         body: Center(
  13.             child: ElevatedButton(
  14.           child: const Text("Ini screen kedua, tap icon back di app bar"),
  15.           onPressed: () {
  16.             Navigator.of(context).pop();
  17.           },
  18.         )));
  19.   }
  20. }
  21.  
  22. void main() {
  23.   runApp(const MyApp());
  24. }
  25.  
  26. class MyApp extends StatelessWidget {
  27.   const MyApp({Key? key}) : super(key: key);
  28.  
  29.   @override
  30.   Widget build(BuildContext context) {
  31.     return MaterialApp(
  32.       title: 'Test Route',
  33.       routes: {
  34.         '/': (context) => const MyHome(),
  35.         '/layar2': (context) => const LayarKedua()
  36.       },
  37.     );
  38.   }
  39. }
  40.  
  41. //perlu  dipisah karena Navigator perlu punya parent Material App
  42. class MyHome extends StatelessWidget {
  43.   const MyHome({Key? key}) : super(key: key);
  44.  
  45.   @override
  46.   Widget build(BuildContext context) {
  47.     return Scaffold(
  48.       appBar: AppBar(
  49.         title: const Text('Test Route'),
  50.       ),
  51.       body: Center(
  52.           child: ElevatedButton(
  53.               child: const Text('Ke screen Kedua'),
  54.               onPressed: () {
  55.                 Navigator.of(context).pushNamed("/layar2");
  56.               })),
  57.     );
  58.   }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement