Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Created January 18, 2024 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjbanov/64dba0304f516278f96a2b5103f13d59 to your computer and use it in GitHub Desktop.
Save yjbanov/64dba0304f516278f96a2b5103f13d59 to your computer and use it in GitHub Desktop.
solar-marsh-4778
import 'dart:async';
import 'dart:ui' as ui;
Future<void> main() async {
// This completer is only used to make the test wait until the frame is done.
var frameCompleter = Completer<void>();
// The concept of "dispatcher" is likely irrelevant to the WebText proposal.
// It's just part of Flutter's structure of the rendering API.
var dispatcher = ui.PlatformDispatcher.instance;
// The onDrawFrame callback is Flutter's requestAnimationFrame, on top of
// which it is implemented. All semantics of the requestAnimationFrame are
// preserved. In particular, note that the callback is synchronous. Any
// asynchrony would cause a miss in the frame.
dispatcher.onDrawFrame = () {
// The paragraph-level style that sets paragraph-level properties, such
// as maximum number of lines, ellipsis, etc, but also a subset of text
// styling properties that will apply to all in the paragraph by default.
//
// Full API: https://api.flutter.dev/flutter/dart-ui/ParagraphStyle/ParagraphStyle.html
var paragraphStyle = ui.ParagraphStyle(
fontFamily: 'Roboto',
textDirection: ui.TextDirection.ltr,
maxLines: 10,
);
// Flutter uses a builder pattern for creating paragraphs of text.
//
// Full API: https://api.flutter.dev/flutter/dart-ui/ParagraphBuilder-class.html
var paragraphBuilder = ui.ParagraphBuilder(paragraphStyle);
paragraphBuilder.addText('Hello, World!');
// Create the paragraph object that can be laid out, rendered, and
// inspected (for various metrics).
//
// Full API: https://api.flutter.dev/flutter/dart-ui/Paragraph-class.html
var paragraph = paragraphBuilder.build();
paragraph.layout(const ui.ParagraphConstraints(
width: 200,
));
// Flutter's rendering API
var recorder = ui.PictureRecorder();
var canvas = ui.Canvas(recorder);
canvas.drawColor(const ui.Color(0xFFFFFFFF), ui.BlendMode.dstOver);
canvas.drawParagraph(paragraph, const ui.Offset(10, 10));
var picture = recorder.endRecording();
var sb = ui.SceneBuilder();
sb.pushOffset(0, 0);
sb.addPicture(ui.Offset.zero, picture);
var scene = sb.build();
dispatcher.implicitView!.render(scene);
frameCompleter.complete();
};
ui.PlatformDispatcher.instance.scheduleFrame();
await frameCompleter.future;
print('>>> done');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment