|
| 1 | +// Demonstrates the controller cache: setting a positive `controllerTimeout` |
| 2 | +// keeps the controller (and its state) alive across short navigations away |
| 3 | +// from the screen. Navigating back within the timeout window reuses the |
| 4 | +// same controller — the counter resumes where it was left. |
| 5 | +// |
| 6 | +// Try: tap "increment", tap "leave", tap "back" — the counter is preserved. |
| 7 | + |
| 8 | +import 'package:df_screen/df_screen.dart'; |
| 9 | +import 'package:flutter/material.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + runApp(const MaterialApp(home: _Host())); |
| 13 | +} |
| 14 | + |
| 15 | +class _Host extends StatefulWidget { |
| 16 | + const _Host(); |
| 17 | + @override |
| 18 | + State<_Host> createState() => _HostState(); |
| 19 | +} |
| 20 | + |
| 21 | +class _HostState extends State<_Host> { |
| 22 | + bool showCounter = true; |
| 23 | + |
| 24 | + @override |
| 25 | + Widget build(BuildContext context) { |
| 26 | + return Scaffold( |
| 27 | + appBar: AppBar(title: const Text('Controller cache demo')), |
| 28 | + body: showCounter |
| 29 | + ? const CounterScreen( |
| 30 | + key: ValueKey('counter'), |
| 31 | + controllerTimeout: Duration(minutes: 5), |
| 32 | + ) |
| 33 | + : Center( |
| 34 | + child: ElevatedButton( |
| 35 | + onPressed: () => setState(() => showCounter = true), |
| 36 | + child: const Text('Back to counter'), |
| 37 | + ), |
| 38 | + ), |
| 39 | + bottomNavigationBar: SafeArea( |
| 40 | + child: Padding( |
| 41 | + padding: const EdgeInsets.all(8), |
| 42 | + child: ElevatedButton( |
| 43 | + onPressed: () => setState(() => showCounter = false), |
| 44 | + child: const Text('Leave the screen'), |
| 45 | + ), |
| 46 | + ), |
| 47 | + ), |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +base class CounterScreen extends Screen { |
| 53 | + const CounterScreen({super.key, super.controllerTimeout}); |
| 54 | + |
| 55 | + @override |
| 56 | + State createState() => _CounterScreenState(); |
| 57 | + |
| 58 | + @override |
| 59 | + ScreenController createController(Screen screen, ScreenState state) { |
| 60 | + return CounterController(screen, state); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +base class CounterController extends ScreenController { |
| 65 | + CounterController(super.superScreen, super.superState); |
| 66 | + |
| 67 | + // This survives navigating away + back if controllerTimeout > 0. |
| 68 | + int count = 0; |
| 69 | + |
| 70 | + void increment() { |
| 71 | + count++; |
| 72 | + superState!.rebuildScreen(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +base class _CounterScreenState |
| 77 | + extends ScreenState<CounterScreen, CounterController> { |
| 78 | + @override |
| 79 | + Widget buildWidget(BuildContext context) { |
| 80 | + return Center( |
| 81 | + child: Column( |
| 82 | + mainAxisSize: MainAxisSize.min, |
| 83 | + children: [ |
| 84 | + Text('Count: ${c.count}', |
| 85 | + style: Theme.of(context).textTheme.headlineMedium,), |
| 86 | + const SizedBox(height: 16), |
| 87 | + ElevatedButton(onPressed: c.increment, child: const Text('Increment')), |
| 88 | + ], |
| 89 | + ), |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments