Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset source if same sample chosen #3057

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppServices>(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),
Expand Down
15 changes: 11 additions & 4 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -181,8 +190,6 @@ class AppServices {

// Reset the execution area.
executionService?.reset();

appModel.editorStatus.showToast('Created new ${titleCase(type)} snippet');
}

void _handleCodeChanged() {
Expand Down
Loading