zoukankan      html  css  js  c++  java
  • InheritedWidget and screen

    self:

    import 'package:flutter/material.dart';
    
    class GrantScreen {
      static double _width, _height;
      static double _physicalWidth, _physicalHeight;
      static double _top, _bottom;
      static double _scaleWidth, _scaleHeight;
      static double _textScale;
      static double _pixelRatio;
    
      GrantScreen(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 = 375.0;
        _height = 667.0;
        _scaleWidth = _physicalWidth / _width;
        _scaleHeight = _physicalHeight / _height;
        _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) => w * _scaleWidth;
      setHeight(double h) => h * _scaleHeight;
    
      setFontSize(double f, [bool allowScale=true]) =>
          allowScale? setWidth(f)/_textScale : f/_textScale;
    
      getGlobalPosition(DragDownDetails details){
        double dx = details.globalPosition.dx / _scaleWidth;
        double dy = details.globalPosition.dy / _scaleHeight;
        return DragDownDetails(globalPosition: Offset(dx, dy));
      }
    }
    

      

    main

    import 'package:flutter/material.dart';
    import 'screen.dart';
    
    main() => runApp(MaterialApp(
      home: MyApp(),
    ));
    
    class MyApp extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        var x = MediaQuery.of(context).size;
        print('original size: $x');
    
        GrantScreen screen = GrantScreen(222, 444, context);
        return RootWidget(
          child: HomePage(),
          screen: screen,
        );
      }
    }
    
    class RootWidget extends InheritedWidget {
      RootWidget({Key key, @required this.child, this.screen}):super(key:key);
      final GrantScreen 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', style: TextStyle(fontSize: 22),),),
          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(
             double.infinity,height: double.infinity,
            color: Colors.grey,
            child: Column(
              children: <Widget>[
                Text('Text', style: TextStyle(fontSize: s.setFontSize(44, true)),),
                Text('Text', style: TextStyle(fontSize: s.setFontSize(44, false)),),
                Text('Text', style: TextStyle(fontSize: 44),),
              ],
            ),
          ),),
        );
      }
    }
    

      

    screen:

    import 'package:flutter/material.dart';
    
    class GrantScreen {
      static double _width, _height;
      static double _physicalWidth, _physicalHeight;
      static double _top, _bottom;
      static double _scaleWidth, _scaleHeight;
      static double _textScale;
      static double _pixelRatio;
    
      GrantScreen(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) => w * _scaleWidth;
      setHeight(double h) => h * _scaleHeight;
    
      setFontSize(double f, [bool allowScale=true]) =>
        allowScale? setWidth(f)/_textScale : f/_textScale;
    
      getGlobalPosition(DragDownDetails details){
        double dx = details.globalPosition.dx / _scaleWidth;
        double dy = details.globalPosition.dy / _scaleHeight;
        return DragDownDetails(globalPosition: Offset(dx, dy));
      }
    }
    

      

  • 相关阅读:
    python3 TypeError: a bytes-like object is required, not 'str'
    Centos 安装Python Scrapy PhantomJS
    Linux alias
    Vim vimrc配置
    Windows下 Python Selenium PhantomJS 抓取网页并截图
    Linux sort
    Linux RSync 搭建
    SSH隧道 访问内网机
    笔记《鸟哥的Linux私房菜》7 Linux档案与目录管理
    Tornado 错误 "Global name 'memoryview' is not defined"
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10961932.html
Copyright © 2011-2022 走看看