Skip to content

Instantly share code, notes, and snippets.

@rxlabz
Last active May 22, 2017 21:55
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 rxlabz/21bd3bb06ee5f53d4b5b34b11557e86e to your computer and use it in GitHub Desktop.
Save rxlabz/21bd3bb06ee5f53d4b5b34b11557e86e to your computer and use it in GitHub Desktop.
FRP with flutter ?
import 'dart:async';
import 'dart:ui';
final double devicePixelRatio = window.devicePixelRatio;
final Size logicalSize = window.physicalSize / devicePixelRatio;
final Rect physicalBounds = Offset.zero & (logicalSize * devicePixelRatio);
void main() {
final time$ = new Stream.periodic( new Duration(seconds: 1), (value) => value);
final value$ = time$.map((t)=>"Counter : $t");
value$.map(buildView).listen((view) {
window.render(view);
window.scheduleFrame();
});
}
Scene buildView(String text) => render(draw(buildParagraph(text)));
Scene render(Picture view) {
final sceneBuilder = new SceneBuilder()
..pushClipRect(physicalBounds)
..addPicture(Offset.zero, view)
..pop();
return sceneBuilder.build();
}
Picture draw(Paragraph p) {
final recorder = new PictureRecorder();
final canvas = new Canvas(recorder, physicalBounds);
canvas.scale(devicePixelRatio, devicePixelRatio);
canvas.drawParagraph(p, new Offset(100.0, 100.0));
return recorder.endRecording();
}
Paragraph buildParagraph(String text) {
final paragraphBuilder = new ParagraphBuilder(new ParagraphStyle())
..addText('$text');
final p = paragraphBuilder.build()
..layout(new ParagraphConstraints(width: logicalSize.width));
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment