Advertisement
yudiwibisono

flutter_route_return

Apr 22nd, 2022
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.86 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("ini data dari screen 2");
  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 StatefulWidget {
  38.   const MyHome({Key? key}) : super(key: key);
  39.  
  40.   @override
  41.   State<StatefulWidget> createState() {
  42.     return MyHomeState();
  43.   }
  44. }
  45.  
  46. class MyHomeState extends State<MyHome> {
  47.   String _hasil = "";
  48.  
  49.   set hasil(String hasil) {
  50.     _hasil = hasil;
  51.     setState(() {});
  52.   }
  53.  
  54.   @override
  55.   Widget build(BuildContext context) {
  56.     return Scaffold(
  57.       appBar: AppBar(
  58.         title: const Text('Test Route'),
  59.       ),
  60.       body: Center(
  61.           child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
  62.         ElevatedButton(
  63.             child: const Text('Ke screen Kedua'),
  64.             onPressed: () async {
  65.               hasil = await Navigator.of(context)
  66.                   .push(MaterialPageRoute(builder: (context) {
  67.                 return const LayarKedua(pesan: "haloo ini pesan dari screen 1");
  68.               }));
  69.             }),
  70.         Text(" Hasil: $_hasil")
  71.       ])),
  72.     );
  73.   }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement