我正在使用一个名为“flutter_inappwebview”的插件。它还提供了几种使用代码滚动页面的方法,我希望滚动发生并导致任何设备上的相同位置,而不管设备像素/屏幕大小如何。
在 Realme 6(1080x2400 像素)和三星 A10(720 x 1520 像素)上测试。使用网站地址“https://google.com”测试
在页面加载开始时,两个设备都显示完全相同的场景。(可能是因为 Google 主页会根据屏幕尺寸进行扩展)。
执行搜索后,我继续使用一些按钮来滚动页面,使用以下方法之一: scrollBy
和 滚动到
我还使用getContentHeight来获取计算的页面内容高度。
以下是我的代码:
ButtonBar(
buttonHeight: 20,
buttonMinWidth: 20,
alignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () {
m._scrollby((Get.width/2).toInt());
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () async {
int? cc = await m.webViewController.getContentHeight();
m.webViewController.scrollTo(x: 0, y: (((Get.width/200)/0.5)*(cc!/20)).toInt(), animated: true);
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () {
m.webViewController.scrollBy(x: 0, y: 500, animated: true);
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () async {
int? cc = await m.webViewController.getContentHeight();
m.webViewController.scrollTo(x: 0, y: (cc!/2).toInt(), animated: true);
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () {
m.webViewController.scrollTo(x: 0, y: (Get.width/2).toInt(), animated: true);
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () async {
int? cc = await m.webViewController.getContentHeight();
m.webViewController.scrollTo(x: 0, y: (cc!/100*20).toInt(), animated: true);
},
),
RaisedButton(
child: Icon(Icons.refresh),
onPressed: () async {
int? cc = await m.webViewController.getContentHeight();
m.webViewController.scrollTo(x: 0, y: (((Get.width/200)/0.5)*(cc!/20)).toInt(), animated: true);
},
),
],
),
我还在使用Get.width
Getx 包提供的 ,它提供了设备的宽度。我将它与 InAppWebView 的小部件的父级容器结合使用,宽度和高度Get.width
为滚动位置。
结果是,在两台设备上按下任何按钮,我的三星设备上的页面滚动次数都比我的 Realme 设备上的多。我永远无法让页面滚动到两者看起来相同的位置,即使 InAppWebView/Container 都提供了相同或相似的大小。
我希望我的问题在我写的时候很清楚。