简体   繁体   English

Flutter WebView 尺寸扩大

[英]Flutter WebView extends in size

在此处输入图像描述

I have this canvas clock.我有这个 canvas 时钟。 It is responsive and can adjust every screen size.它反应灵敏,可以调整每个屏幕尺寸。 It is rendered in browsers normally and there is no problem, but在浏览器中正常渲染没有问题,但是

when I am using Flutter WebView widget, the clock increases in size step-by-step.当我使用 Flutter WebView 小部件时,时钟的大小会逐步增加。 I mean that at first it has normal size, but on every tic it grows, until it reach specific size and after that it stops growing.我的意思是,起初它具有正常大小,但在每次抽动时它都会增长,直到达到特定大小,然后停止增长。 (In browsers it has stable size) (在浏览器中它具有稳定的大小)

在此处输入图像描述

This is it max size.这是它的最大尺寸。 From here it stops growing:D从这里它停止增长:D

CODE:代码:

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);

    @override
    initState() {
      super.initState();
      if (Platform.isAndroid) WebView.platform = AndroidWebView();
    }

    return Scaffold(
        appBar: AppBar(title: Text('hi')),
        body: Container(
          width: 400,
          height: 500,
          child: const WebView(
            javascriptMode: JavascriptMode.unrestricted,
            initialUrl: 'http://canvasclock.zuka2001.repl.co/',
          ),
        ));
  }
}

How can I stop this clock from growing?我怎样才能阻止这个时钟增长?

You can add width and height same as the device您可以添加与设备相同的宽度和高度

body: Container(
          width: mediaQuery.size.width,
          height: mediaQuery.size.height,
          child: const WebView(
            javascriptMode: JavascriptMode.unrestricted,
            initialUrl: 'http://canvasclock.zuka2001.repl.co/',
          ),
        )

Instead of using hardcoded container size use dynamic size using media query.使用媒体查询使用动态大小,而不是使用硬编码的容器大小。

width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,

and you webview will take as per your device size and other things will be managed by you website.您的 webview 将根据您的设备尺寸进行处理,其他内容将由您的网站管理。

I think the problem is that your initState is inside build function.我认为问题在于您的 initState 在内部构建 function。 Because initState is meant to be called outside build Function to represent the first state of widget being built.因为 initState 意味着在构建 Function 之外调用,以表示正在构建的小部件的第一个 state。 but now it's being called while building the widget, so that is causing this weird behavior.但是现在它在构建小部件时被调用,所以这导致了这种奇怪的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM