alex_sit

screens with stateless

Mar 3rd, 2022
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.17 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:navigation_test/app_routes.dart';
  3. import 'package:navigation_test/navigator_app.dart';
  4. import 'dart:developer';
  5.  
  6. // class FirstScreen extends StatefulWidget {
  7. //   const FirstScreen({Key? key}) : super(key: key);
  8. //   //const FirstScreen({Key? key, required this.inputData}) : super(key: key);
  9. //
  10. //   @override
  11. //   FirstScreenState createState() => FirstScreenState();
  12. // }
  13.  
  14.  
  15. class FirstScreen extends StatelessWidget {
  16.   final String? inputData = "default";
  17.  
  18.   const FirstScreen({Key? key}) : super(key: key);
  19.  
  20.   Future<bool> showExitDialog(BuildContext context) async => await showDialog(
  21.     context: context,
  22.     builder: (context) => AlertDialog(
  23.       title: const Text('Question'),
  24.       content: const Text('Do you want to exit?'),
  25.       actions: <Widget>[
  26.         TextButton(
  27.           child: const Text('Yes'),
  28.           onPressed: () => Navigator.of(context).pop(true),
  29.         ),
  30.         TextButton(
  31.           onPressed: () => Navigator.of(context).pop(false),
  32.           child: const Text('No'),
  33.         ),
  34.       ],
  35.     ),
  36.   );
  37.  
  38.   @override
  39.   Widget build(BuildContext context) {
  40.     return WillPopScope(
  41.       onWillPop: () {
  42.           log("this is onWillPop method");
  43.           return showExitDialog(context);
  44.           },
  45.       child: Scaffold(
  46.           appBar: AppBar(
  47.               centerTitle: true,
  48.               title: const Text('Module three'),
  49.               leading: IconButton(
  50.                 onPressed: () async {
  51.                   bool exit = await (showExitDialog(context));
  52.                   if (exit) {
  53.                     Navigator.of(context).pop();
  54.                   }
  55.                 },
  56.                 icon: const Icon(Icons.arrow_back),
  57.               )),
  58.           body: Column(
  59.             children: [
  60.               Container(
  61.                 alignment: Alignment.center,
  62.                 child: ElevatedButton(
  63.                   onPressed: () =>
  64.                       Navigator.of(context).pushNamed(AppRoutes.secondScreen),
  65.                   child: const Text('Go next'),
  66.                 ),
  67.               ),
  68.               Text('$inputData'),
  69.             ],
  70.           ),
  71.         ),
  72.       );
  73.  
  74.   }
  75. }
  76.  
  77. class SecondScreen extends StatelessWidget {
  78.   const SecondScreen({Key? key}) : super(key: key);
  79.  
  80.   @override
  81.   Widget build(BuildContext context) {
  82.     return MaterialApp(
  83.       home: Scaffold(
  84.         appBar: AppBar(),
  85.         body: Column(
  86.           children: [
  87.             Container(
  88.               alignment: Alignment.center,
  89.               child: ElevatedButton(
  90.                 onPressed: () => Navigator.of(context)
  91.                     .pushNamed(AppRoutes.home, arguments: 'Return 42'),
  92.                 child: const Text('Return 42'),
  93.               ),
  94.             ),
  95.             Container(
  96.               alignment: Alignment.topCenter,
  97.               child: ElevatedButton(
  98.                 onPressed: () => Navigator.of(context)
  99.                     .pushNamed(AppRoutes.home, arguments: 'Return AbErVaLIG'),
  100.                 child: const Text('Return AbErVaLIG'),
  101.               ),
  102.             ),
  103.           ],
  104.         ),
  105.       ),
  106.     );
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment