1. var size = MediaQuery.of(context);
  2. print(size);
  3. print(size.size.width); //480
  4. print(size.padding.top); //24 顶部状态栏高度
  5. print(size.padding.bottom); // 底部导航栏高度
  1. MediaQueryData(
  2. size: Size(480.0, 829.3),
  3. devicePixelRatio: 1.5,
  4. textScaleFactor: 1.0,
  5. platformBrightness: Brightness.light,
  6. padding: EdgeInsets(0.0, 24.0, 0.0, 0.0),
  7. viewPadding: EdgeInsets(0.0, 24.0, 0.0, 0.0),
  8. viewInsets: EdgeInsets.zero,
  9. alwaysUse24HourFormat: false,
  10. accessibleNavigation: false,
  11. highContrast: false,
  12. disableAnimations: false,
  13. invertColors: false,
  14. boldText: false,
  15. navigationMode: traditional
  16. )

静态化

  1. class Application {
  2. static FluroRouter router;
  3. static Map<String, dynamic> screen = {};
  4. static viewPortSize(context) {
  5. final size = MediaQuery.of(context);
  6. screen['width'] = size.size.width;
  7. screen['height'] = size.size.height;
  8. screen['appBarHeight'] = size.padding.top;
  9. screen['bottomNavigatorHeight'] = size.padding.bottom;
  10. }
  11. }

使用

  1. Application.viewPortSize(context);
  2. print(Application.screen); //{width: 480.0, height: 829.3333333333334, appBarHeight: 24.0, bottomNavigatorHeight: 0.0}

移除间距

  1. MediaQuery.removePadding({
  2. Key key,
  3. @required BuildContext context,
  4. bool removeLeft = false,
  5. bool removeTop = false,
  6. bool removeRight = false,
  7. bool removeBottom = false,
  8. @required Widget child,
  9. }) {
  10. return MediaQuery(
  11. key: key,
  12. data: MediaQuery.of(context).removePadding(
  13. removeLeft: removeLeft,
  14. removeTop: removeTop,
  15. removeRight: removeRight,
  16. removeBottom: removeBottom,
  17. ),
  18. child: child,
  19. );
  20. }

应用:移除顶部状态栏间距

image.png

  1. class ApiDemoPage extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MediaQuery.removePadding(
  5. context: context,
  6. removeTop: true, //移除状态栏间距
  7. child: Scaffold(
  8. appBar: AppBar(title: Text('flutter demo')),
  9. body: null,
  10. ),
  11. );
  12. }
  13. }

SafeArea 异形屏幕适配

现在的手机已经不是方方正正的屏幕了,所以我们在写一些UI的时候可能会受到 刘海屏、水滴屏、iPhoneX底部区域等影响。SafeArea组件就是解决这个问题的。

  1. SafeArea(
  2. top: false,
  3. bottom: true,
  4. left: true,
  5. right: false,
  6. ),

示例1:处理刘海屏

image.png

  1. //不加
  2. class FlutterAlign extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return Align(
  6. alignment: Alignment(-1, -1),
  7. child: Container(
  8. child: Text(
  9. "Hello",
  10. ),
  11. ),
  12. );
  13. }
  14. }
  15. //加
  16. class FlutterAlign extends StatelessWidget {
  17. @override
  18. Widget build(BuildContext context) {
  19. return SafeArea(
  20. child: Align(
  21. alignment: Alignment(-1, -1),
  22. child: Container(
  23. child: Text(
  24. "Hello",
  25. ),
  26. ),
  27. ),
  28. );
  29. }
  30. }

示例2:处理iPhone X类似的底部bottom的区域

image.png

  1. class FlutterAlign extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return SafeArea(
  5. child: Align(
  6. alignment: Alignment(-1, 1),
  7. child: Container(
  8. child: Text(
  9. "Hello",
  10. ),
  11. ),
  12. ),
  13. );
  14. }
  15. }