InheritedWidget and screen

main

import 'package:flutter/material.dart';
import 'screen.dart';

main() => runApp(MaterialApp(
  home: MyApp(),
));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SelfScreen screen = SelfScreen(750, 1344, context);
    return RootWidget(
      child: HomePage(),
      screen: screen,
    );
  }
}

class RootWidget extends InheritedWidget {
  RootWidget({Key key, @required this.child, this.screen}):super(key:key);
  final SelfScreen screen;
  final Widget child;

  static RootWidget of(BuildContext context){
    return context.inheritFromWidgetOfExactType(RootWidget);
  }

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    return true;
  }
}

class HomePage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    var s = RootWidget.of(context).screen;
    return Scaffold(
      appBar: AppBar(title: Text('ABC'),),
      body: GestureDetector(
        onPanDown: (DragDownDetails details){
          print('mmmmmmmmmmmmmmmmmmmmmm');
          print(details);
          print(s.getGlobalPosition(details));
          print('new top: ${s.top}');
          print(MediaQuery.of(context).padding.top);
          print('mmmmmmmmmmmmmmmmmmmmmmmm');
        },
        child: Container(
        width: double.infinity,height: double.infinity,
        color: Colors.grey,
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.red,
              width: s.setWidth(200),
              height: s.setHeight(200),
            )
          ],
        ),
      ),),
    );
  }
}

  

screen:

import 'package:flutter/material.dart';

class SelfScreen {
  static double _width, _height;
  static double _physicalWidth, _physicalHeight;
  static double _top, _bottom;
  static double _scaleWidth, _scaleHeight;
  static double _textScale;
  static double _pixelRatio;

  SelfScreen(double w, double h, BuildContext context){

    _physicalWidth = MediaQuery.of(context).size.width;
    _physicalHeight = MediaQuery.of(context).size.height;
    _top = MediaQuery.of(context).padding.top;
    _bottom = MediaQuery.of(context).padding.bottom;
    _textScale = MediaQuery.of(context).textScaleFactor;
    _width = w;
    _height = h;
    _scaleWidth = _physicalWidth / w;
    _scaleHeight = _physicalHeight / h;
    _pixelRatio = MediaQuery.of(context).devicePixelRatio;
  }

  get top => _top / _scaleHeight;
  get bottom => _bottom / _scaleHeight;
  get scaleWidth => _scaleWidth;
  get scaleHeight => _scaleHeight;
  get width => _width;
  get height => _height;
  get pixelRatio => _pixelRatio;
  get physicalWidth => _physicalWidth;
  get physicalHeight => _physicalHeight;

  setWidth(double w){
    return w * _scaleWidth;
  }

  setHeight(double h){
    return h * _scaleHeight;
  }

  setFontSize(double f, [bool allowScale=true]){
    return allowScale? f*_scaleWidth/_textScale : f/_textScale;
  }

  getGlobalPosition(DragDownDetails details){
    double x = details.globalPosition.dx;
    double y = details.globalPosition.dy;
    return {'dx':x/_scaleWidth, 'dy':y/_scaleHeight};
  }
}

  

猜你喜欢

转载自www.cnblogs.com/pythonClub/p/10961932.html