diff --git a/pkgs/dartpad_ui/lib/main.dart b/pkgs/dartpad_ui/lib/main.dart index 6cafad01f..e213b14ff 100644 --- a/pkgs/dartpad_ui/lib/main.dart +++ b/pkgs/dartpad_ui/lib/main.dart @@ -1013,8 +1013,18 @@ class ListSamplesWidget extends StatelessWidget { for (final sample in samples) MenuItemButton( leadingIcon: Logo(type: sample.icon), - onPressed: () => - GoRouter.of(context).replaceQueryParam('sample', sample.id), + onPressed: () { + // If the current route is already the current sample, + // reset to it manually. + if (GoRouterState.of(context).uri.queryParameters['sample'] == + sample.id) { + final appServices = + Provider.of(context, listen: false); + appServices.resetToSample(sample); + } else { + GoRouter.of(context).replaceQueryParam('sample', sample.id); + } + }, child: Padding( padding: const EdgeInsets.only(right: 32), child: Text(sample.name), diff --git a/pkgs/dartpad_ui/lib/model.dart b/pkgs/dartpad_ui/lib/model.dart index a14e1badb..2502bc687 100644 --- a/pkgs/dartpad_ui/lib/model.dart +++ b/pkgs/dartpad_ui/lib/model.dart @@ -166,10 +166,19 @@ class AppServices { return versionResponse; } - void resetTo({String? type}) { - type ??= 'dart'; + void resetToSample(Sample sample) { + _reset(source: sample.source, title: sample.name); + } + + void resetTo({String type = 'dart'}) { final source = Samples.getDefault(type: type); + _reset(source: source); + + appModel.editorStatus.showToast('Created new ${titleCase(type)} snippet'); + } + + void _reset({String title = '', required String source}) { // Reset the source. appModel.sourceCodeController.text = source; @@ -181,8 +190,6 @@ class AppServices { // Reset the execution area. executionService?.reset(); - - appModel.editorStatus.showToast('Created new ${titleCase(type)} snippet'); } void _handleCodeChanged() {