Skip to content

Instantly share code, notes, and snippets.

@jaripekkala
Created March 20, 2020 10:30
Show Gist options
  • Save jaripekkala/ce3de2dd5f64fcac3ac70d9ef4573d59 to your computer and use it in GitHub Desktop.
Save jaripekkala/ce3de2dd5f64fcac3ac70d9ef4573d59 to your computer and use it in GitHub Desktop.
FakeDevicePixelRatio
import 'package:flutter/material.dart';
void main() {
runApp(
FakeDevicePixelRatio(
fakeDevicePixelRatio: 1.5,
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('foo')),
body: Center(
child: Text('foo'),
),
),
);
}
}
class FakeDevicePixelRatio extends StatelessWidget {
final num fakeDevicePixelRatio;
final Widget child;
FakeDevicePixelRatio({this.fakeDevicePixelRatio, this.child})
: assert(fakeDevicePixelRatio != null);
@override
Widget build(BuildContext context) {
final ratio =
fakeDevicePixelRatio / WidgetsBinding.instance.window.devicePixelRatio;
return FractionallySizedBox(
widthFactor: 1 / ratio,
heightFactor: 1 / ratio,
child: Transform.scale(
scale: ratio,
child: child,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment