All Projects → genius158 → FlutterTest

genius158 / FlutterTest

Licence: other
事件拦截,屏幕适配等

Programming Languages

dart
5743 projects
objective c
16641 projects - #2 most used programming language
java
68154 projects - #9 most used programming language

flutter 屏幕适配 demo main

一种一劳永逸的全局适配方式

tip

请不要适用MediaQuery.of(context)大小相关方法,window同理,默认MediaQuery适用的系统原本的大小, 非用使用,需要重写MaterialApp,
大小直接使用context.size,或者从size或者constraints获取

效果

320x480
320x480
1080x1920
1080x1920

原理 (具体代码 main

1.更改配置的 ViewConfiguration 的size和devicePixelRatio (ViewConfiguration 这个类再 RenderView 里赋值,而RenderView是 renderObject树的根,在布局和绘制的过程中,会根据ViewConfiguration的值来做由父向子的布局绘制操作)
2.PointerDataPacket 从引擎冲过来的事件,默认采用的是 系统的devicePixelRatio ,这里就需要适用我们的值 复写 initInstances() 内部 ui.window.onPointerDataPacket=_handlePointerDataPacket 赋值以下

void _handlePointerDataPacket(ui.PointerDataPacket packet) {
    _pendingPointerEvents.addAll(PointerEventConverter.expand(
        packet.data,
        // 适配事件的转换比率,采用我们修改的
        getAdapterRatio()));
    if (!locked) _flushPointerEventQueue();
  }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].