Advertisement
yudiwibisono

flutter_param_route

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