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));
      }
    }
    

      

  • 相关阅读:
    02(c)多元无约束优化问题-牛顿法
    02(b)多元无约束优化问题-最速下降法
    数据仓库建模指南系列教程学习笔记
    ML_Gradient ascent algorithm for learning logistic regression classifier
    ML_聚类之Nearest neighbor search
    ML_Clustering & Retrieval(一)
    复习_git教程
    Predicting sentiment from product reviews情感预测
    ML_classification_intro
    ML_classification
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10961932.html
Copyright © 2011-2022 走看看