Skip to content

Commit

Permalink
Replace output SelectableText with a CodeField instance (apache#25640)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyinkin committed Apr 27, 2023
1 parent 07d8023 commit c0867d6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public class MyClass {
await _selectExampleWithSnippet(wt);
await wt.pumpAndSettle();

await wt.enterText(find.codeField(), code);
await wt.enterText(find.snippetCodeField(), code);
await wt.pumpAndSettle();

await _runAndCancelExample(wt, const Duration(milliseconds: 300));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void main() {
await _runAndCancelExample(wt, const Duration(milliseconds: 300));

final source = wt.findPlaygroundController().source ?? '';
await wt.enterText(find.codeField(), '//comment\n' + source);
await wt.enterText(find.snippetCodeField(), '//comment\n' + source);
await wt.pumpAndSettle();

// Cancel changed example.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class MyClass {
}
''';

await wt.enterText(find.codeField(), code);
await wt.enterText(find.snippetCodeField(), code);
await wt.pumpAndSettle();

await wt.tapAndSettle(find.runOrCancelButton());
Expand Down Expand Up @@ -103,7 +103,7 @@ public class MyClass {
const text = 'OK';
const code = 'print("$text", end="")';

await wt.enterText(find.codeField(), code);
await wt.enterText(find.snippetCodeField(), code);
await wt.pumpAndSettle();

await wt.tapAndSettle(find.runOrCancelButton());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import 'package:flutter/material.dart';
import 'package:flutter_code_editor/flutter_code_editor.dart';

import '../../constants/sizes.dart';
import '../../controllers/playground_controller.dart';
Expand All @@ -38,36 +39,30 @@ class ResultTabContent extends StatefulWidget {

class _ResultTabContentState extends State<ResultTabContent> {
final ScrollController _scrollController = ScrollController();
final CodeController _codeController = CodeController();

@override
Widget build(BuildContext context) {
final ext = Theme.of(context).extension<BeamThemeExtension>()!;
void initState() {
super.initState();
widget.playgroundController.codeRunner.addListener(_updateText);
widget.playgroundController.resultFilterController.addListener(
_updateText,
);
_updateText();
}

return UnreadClearer(
controller: widget.playgroundController.codeRunner.unreadController,
unreadKey: UnreadEntryEnum.result,
child: AnimatedBuilder(
animation: widget.playgroundController.codeRunner,
builder: (context, child) => SingleChildScrollView(
controller: _scrollController,
child: Scrollbar(
thumbVisibility: true,
trackVisibility: true,
controller: _scrollController,
child: Padding(
padding: const EdgeInsets.all(BeamSizes.size16),
child: AnimatedBuilder(
animation: widget.playgroundController.resultFilterController,
builder: (context, child) => SelectableText(
_getText(),
style: ext.codeRootStyle,
),
),
),
),
),
),
void _updateText() {
_codeController.fullText = _getText();
}

@override
void dispose() {
_codeController.dispose();
widget.playgroundController.resultFilterController.removeListener(
_updateText,
);
widget.playgroundController.codeRunner.removeListener(_updateText);
super.dispose();
}

String _getText() {
Expand All @@ -82,4 +77,46 @@ class _ResultTabContentState extends State<ResultTabContent> {
return widget.playgroundController.codeRunner.resultLogOutput;
}
}

@override
Widget build(BuildContext context) {
final ext = Theme.of(context).extension<BeamThemeExtension>()!;

return UnreadClearer(
controller: widget.playgroundController.codeRunner.unreadController,
unreadKey: UnreadEntryEnum.result,
child: ColoredBox(
// TODO(alexeyinkin): Migrate to Material 3: https://github.com/apache/beam/issues/24610
color: Theme.of(context).backgroundColor,
child: AnimatedBuilder(
animation: widget.playgroundController.codeRunner,
builder: (context, child) => SingleChildScrollView(
controller: _scrollController,
child: Scrollbar(
thumbVisibility: true,
trackVisibility: true,
controller: _scrollController,
child: Padding(
padding: const EdgeInsets.all(BeamSizes.size16),
child: AnimatedBuilder(
animation: widget.playgroundController.resultFilterController,
builder: (context, child) {
return CodeTheme(
data: ext.codeTheme,
child: CodeField(
readOnly: true,
controller: _codeController,
gutterStyle: GutterStyle.none,
textStyle: ext.codeRootStyle,
),
);
},
),
),
),
),
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import 'package:playground_components/playground_components.dart';
import 'finder.dart';

extension CommonFindersExtension on CommonFinders {
Finder codeField() {
return byType(CodeField);
Finder snippetCodeField() {
return find.descendant(
of: find.byType(SnippetEditor),
matching: byType(CodeField),
);
}

Finder dropdownMenuItemWithText(String text) {
Expand All @@ -40,10 +43,10 @@ extension CommonFindersExtension on CommonFinders {
return widgetWithText(OutputTab, 'Graph');
}

Finder outputSelectableText() {
Finder outputCodeField() {
return find.descendant(
of: find.outputWidget(),
matching: find.byType(SelectableText),
matching: byType(CodeField),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import 'expect.dart';
extension WidgetTesterExtension on WidgetTester {
//workaround for https://github.com/flutter/flutter/issues/120060
Future<void> enterCodeFieldText(String text) async {
final codeField = widget(find.codeField());
final codeField = widget(find.snippetCodeField());
(codeField as CodeField).controller.fullText = text;
codeField.focusNode?.requestFocus();
}
Expand All @@ -48,7 +48,7 @@ extension WidgetTesterExtension on WidgetTester {
}

CodeController findOneCodeController() {
final codeField = find.codeField();
final codeField = find.snippetCodeField();
expect(codeField, findsOneWidget);

return widget<CodeField>(codeField).controller;
Expand All @@ -66,14 +66,12 @@ extension WidgetTesterExtension on WidgetTester {
}

String? findOutputText() {
final selectableText = find.outputSelectableText();
expect(selectableText, findsOneWidget);

return widget<SelectableText>(selectableText).data;
final codeField = widget(find.outputCodeField());
return (codeField as CodeField).controller.text;
}

PlaygroundController findPlaygroundController() {
final context = element(find.codeField());
final context = element(find.snippetCodeField());
return context.read<PlaygroundController>();
}

Expand Down

0 comments on commit c0867d6

Please sign in to comment.