1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'package:flutter/material.dart'; |
6 | |
7 | /// Flutter code sample for [NavigatorState.restorablePushReplacement]. |
8 | |
9 | void main() => runApp(const RestorablePushReplacementExampleApp()); |
10 | |
11 | class RestorablePushReplacementExampleApp extends StatelessWidget { |
12 | const RestorablePushReplacementExampleApp({super.key}); |
13 | |
14 | @override |
15 | Widget build(BuildContext context) { |
16 | return const RootRestorationScope( |
17 | restorationId: 'app' , |
18 | child: MaterialApp(restorationScopeId: 'app' , home: RestorablePushReplacementExample()), |
19 | ); |
20 | } |
21 | } |
22 | |
23 | class RestorablePushReplacementExample extends StatefulWidget { |
24 | const RestorablePushReplacementExample({this.wasPushed = false, super.key}); |
25 | |
26 | final bool wasPushed; |
27 | |
28 | @override |
29 | State<RestorablePushReplacementExample> createState() => _RestorablePushReplacementExampleState(); |
30 | } |
31 | |
32 | @pragma('vm:entry-point' ) |
33 | class _RestorablePushReplacementExampleState extends State<RestorablePushReplacementExample> { |
34 | @pragma('vm:entry-point' ) |
35 | static Route<void> _myRouteBuilder(BuildContext context, Object? arguments) { |
36 | return MaterialPageRoute<void>( |
37 | builder: (BuildContext context) => const RestorablePushReplacementExample(wasPushed: true), |
38 | ); |
39 | } |
40 | |
41 | @override |
42 | Widget build(BuildContext context) { |
43 | return Scaffold( |
44 | appBar: AppBar(title: const Text('Sample Code' )), |
45 | body: Center( |
46 | child: widget.wasPushed |
47 | ? const Text('This is a new route.' ) |
48 | : const Text('This is the initial route.' ), |
49 | ), |
50 | floatingActionButton: FloatingActionButton( |
51 | onPressed: () => Navigator.restorablePushReplacement(context, _myRouteBuilder), |
52 | tooltip: 'Increment Counter' , |
53 | child: const Icon(Icons.add), |
54 | ), |
55 | ); |
56 | } |
57 | } |
58 | |