-
-
Save cmkweber/c17aae8fa48a4853a87f242835cb65cb to your computer and use it in GitHub Desktop.
Example showing supersampling accomplished using ImageFiltered widget
This file contains hidden or 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 'package:flutter/material.dart'; | |
| import 'dart:math'; | |
| import 'package:flutter/scheduler.dart'; | |
| import 'dart:ui' as ui; | |
| void main() { runApp(App()); } | |
| class App extends StatelessWidget | |
| { | |
| @override Widget build(BuildContext context) | |
| { | |
| return MaterialApp( | |
| home: View(), | |
| showPerformanceOverlay: true, | |
| title: 'Supersampling' | |
| ); | |
| } | |
| } | |
| class View extends StatefulWidget { @override ViewState createState() { return ViewState(); } } | |
| class ViewState extends State<View> with TickerProviderStateMixin | |
| { | |
| final Matrix4 _downscale = Matrix4.diagonal3Values(0.5, 0.5, 0.0); | |
| final ViewPainter _painter = ViewPainter(); | |
| Ticker _ticker; | |
| @override void initState() | |
| { | |
| super.initState(); | |
| _ticker = createTicker(_painter.update); | |
| _ticker.start(); | |
| } | |
| @override void dispose() | |
| { | |
| _ticker.stop(); | |
| _ticker.dispose(); | |
| super.dispose(); | |
| } | |
| Widget build(BuildContext context) | |
| { | |
| return ImageFiltered( | |
| imageFilter: ui.ImageFilter.matrix( | |
| _downscale.storage, | |
| filterQuality: FilterQuality.low | |
| ), | |
| child: Transform.scale( | |
| alignment: Alignment.topLeft, | |
| scale: 2.0, | |
| child: CustomPaint( | |
| isComplex: false, | |
| painter: _painter, | |
| willChange: true | |
| ) | |
| ) | |
| ); | |
| } | |
| } | |
| class ViewPainter extends ChangeNotifier implements CustomPainter | |
| { | |
| static const Duration _duration = Duration(seconds: 8); | |
| static const Offset _one = Offset(0.0, -0.5); | |
| static const Offset _two = Offset(-0.5, 0.5); | |
| static const Offset _three = Offset(0.5, 0.5); | |
| final Paint _paint = Paint(); | |
| double _rotation = 0.0; | |
| final ui.Vertices _vertices = ui.Vertices(VertexMode.triangles, <Offset>[_one, _two, _three], colors: List<Color>.filled(3, Colors.black)); | |
| void update(Duration timeStamp) | |
| { | |
| _rotation = ((timeStamp.inMicroseconds.toDouble() % _duration.inMicroseconds.toDouble()) / _duration.inMicroseconds.toDouble()) * 2.0 * pi; | |
| notifyListeners(); | |
| } | |
| void paint(ui.Canvas canvas, ui.Size size) | |
| { | |
| canvas.drawColor(Colors.white, BlendMode.srcOver); | |
| canvas.translate(size.width * 0.5, size.height * 0.5); | |
| canvas.scale(size.shortestSide); | |
| canvas.rotate(_rotation); | |
| canvas.drawVertices(_vertices, BlendMode.srcOver, _paint); | |
| } | |
| @override bool hitTest(Offset position) { return true; } | |
| @override bool shouldRebuildSemantics(CustomPainter previous) { return false; } | |
| @override bool shouldRepaint(CustomPainter previous) { return true; } | |
| @override get semanticsBuilder { return null; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment