|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:dcc_toolkit/dcc_toolkit.dart'; |
| 4 | +import 'package:dcc_toolkit/showcase/showcase_scope.dart'; |
| 5 | +import 'package:dcc_toolkit/showcase/showcase_state.dart'; |
| 6 | +import 'package:material_ui/material_ui.dart'; |
| 7 | + |
| 8 | +/// Wait a moment, so the visitor sees the result of the last step first. |
| 9 | +const _delay = Duration(milliseconds: 700); |
| 10 | + |
| 11 | +/// Offers to start over once the demo is finished and the visitor is back on the page that contains this widget, |
| 12 | +/// usually the home of the app. |
| 13 | +/// |
| 14 | +/// Keeping looking around closes the dialog. The demo then starts over by itself at the next |
| 15 | +/// [ShowcaseState.begin]. |
| 16 | +class const FinishPrompt({required final Widget child, super.key}) extends StatefulWidget { |
| 17 | + /// Creates a [FinishPrompt]. |
| 18 | + this; |
| 19 | + |
| 20 | + @override |
| 21 | + State<FinishPrompt> createState() => _FinishPromptState(); |
| 22 | +} |
| 23 | + |
| 24 | +class _FinishPromptState extends State<FinishPrompt> { |
| 25 | + bool _isScheduled = false; |
| 26 | + |
| 27 | + Future<void> _showAfterDelay(ShowcaseState state) async { |
| 28 | + await Future<void>.delayed(_delay); |
| 29 | + _isScheduled = false; |
| 30 | + if (!mounted) return; |
| 31 | + |
| 32 | + final strings = ShowcaseScope.stringsOf(context); |
| 33 | + await showNativeDialog<void>( |
| 34 | + context, |
| 35 | + title: strings.finishTitle, |
| 36 | + content: strings.finishMessage, |
| 37 | + actions: [ |
| 38 | + DialogAction(text: strings.keepLooking, onTap: () {}), |
| 39 | + DialogAction(text: strings.startOver, onTap: state.reset), |
| 40 | + ], |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + @override |
| 45 | + Widget build(BuildContext context) { |
| 46 | + final state = ShowcaseScope.of<ShowcaseState>(context); |
| 47 | + final isCurrent = ModalRoute.of(context)?.isCurrent ?? false; |
| 48 | + |
| 49 | + if (isCurrent && !_isScheduled && state.takeFinishPrompt()) { |
| 50 | + _isScheduled = true; |
| 51 | + unawaited(_showAfterDelay(state)); |
| 52 | + } |
| 53 | + |
| 54 | + return widget.child; |
| 55 | + } |
| 56 | +} |
0 commit comments