Created with <3 with dartpad.dev.
Created
January 18, 2024 22:47
-
-
Save yjbanov/9cb4d9cb817413536abe9c0ef7e48479 to your computer and use it in GitHub Desktop.
solar-marsh-4778
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.rtl, | |
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('مرحبا بالعالم'); | |
// 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