Skip to content

Commit

Permalink
Implement /flutter for flutter.new as firebase redirect (#3093)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough authored Nov 20, 2024
1 parent 2ce90bf commit 4d7507e
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 150 deletions.
10 changes: 10 additions & 0 deletions pkgs/dartpad_ui/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"**/node_modules/**"
],
"redirects": [
{
"source": "/dart",
"destination": "/?sample=dart",
"type": 301
},
{
"source": "/flutter",
"destination": "/?sample=flutter",
"type": 301
},
{
"source": "/embed-dart?(.html)",
"destination": "/?embed=true",
Expand Down
7 changes: 1 addition & 6 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class _DartPadAppState extends State<DartPadApp> {

Widget _homePageBuilder(BuildContext context, GoRouterState state,
{String? gist}) {
final path = state.path;
final gistId = gist ?? state.uri.queryParameters['id'];
final builtinSampleId = state.uri.queryParameters['sample'];
final flutterSampleId = state.uri.queryParameters['sample_id'];
Expand All @@ -128,7 +127,6 @@ class _DartPadAppState extends State<DartPadApp> {
final runOnLoad = state.uri.queryParameters['run'] == 'true';

return DartPadMainPage(
path: path,
initialChannel: channelParam,
embedMode: embedMode,
runOnLoad: runOnLoad,
Expand Down Expand Up @@ -207,7 +205,6 @@ class DartPadMainPage extends StatefulWidget {
final String? gistId;
final String? builtinSampleId;
final String? flutterSampleId;
final String? path;

DartPadMainPage({
required this.initialChannel,
Expand All @@ -217,7 +214,6 @@ class DartPadMainPage extends StatefulWidget {
this.gistId,
this.builtinSampleId,
this.flutterSampleId,
this.path,
}) : super(
key: ValueKey(
'sample:$builtinSampleId gist:$gistId flutter:$flutterSampleId',
Expand Down Expand Up @@ -276,14 +272,13 @@ class _DartPadMainPageState extends State<DartPadMainPage>
);

appServices.populateVersions();
final fallBackSnippetType = widget.path == '/flutter' ? 'flutter' : 'dart';
appServices
.performInitialLoad(
gistId: widget.gistId,
sampleId: widget.builtinSampleId,
flutterSampleId: widget.flutterSampleId,
channel: widget.initialChannel,
fallbackSnippet: Samples.getDefault(type: fallBackSnippetType))
fallbackSnippet: Samples.defaultSnippet())
.then((value) {
// Start listening for inject code messages.
handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);
Expand Down
3 changes: 2 additions & 1 deletion pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class AppServices {

void resetTo({String? type}) {
type ??= 'dart';
final source = Samples.getDefault(type: type);
final source =
Samples.defaultSnippet(forFlutter: type.toLowerCase() == 'flutter');

// Reset the source.
appModel.sourceCodeController.text = source;
Expand Down
112 changes: 52 additions & 60 deletions pkgs/dartpad_ui/lib/samples.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkgs/dartpad_ui/test/samples_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import 'package:test/test.dart';

void main() {
group('samples', () {
test('has dart default', () {
final sample = Samples.getDefault(type: 'dart');
test('has default dart sample', () {
final sample = Samples.getById('dart');
expect(sample, isNotNull);
});

test('has flutter default', () {
final sample = Samples.getDefault(type: 'flutter');
test('has default flutter sample', () {
final sample = Samples.getById('flutter');
expect(sample, isNotNull);
});
});
Expand Down
2 changes: 2 additions & 0 deletions pkgs/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Sample code snippets for DartPad.
| --- | --- | --- | --- |
| Dart | Fibonacci | [fibonacci.dart](lib/fibonacci.dart) | `fibonacci` |
| Dart | Hello world | [hello_world.dart](lib/hello_world.dart) | `hello-world` |
| Defaults | Dart snippet | [default_dart.dart](lib/default_dart.dart) | `dart` |
| Defaults | Flutter snippet | [default_flutter.dart](lib/default_flutter.dart) | `flutter` |
| Ecosystem | Flame game | [brick_breaker.dart](lib/brick_breaker.dart) | `flame-game` |
| Ecosystem | Google AI SDK | [google_ai.dart](lib/google_ai.dart) | `google-ai-sdk` |
| Flutter | Counter | [main.dart](lib/main.dart) | `counter` |
Expand Down
6 changes: 5 additions & 1 deletion pkgs/samples/lib/default_dart.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

void main() {
for (int i = 0; i < 10; i++) {
for (var i = 0; i < 10; i++) {
print('hello ${i + 1}');
}
}
4 changes: 4 additions & 0 deletions pkgs/samples/lib/default_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/material.dart';

void main() {
Expand Down
Loading

0 comments on commit 4d7507e

Please sign in to comment.