flutter 螢幕適配 demo main
一種一勞永逸的全域性適配方式
效果
320x480

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();
}
複製程式碼