Flutter中WebView的问题

vyswwuz2  于 2023-02-05  发布在  Flutter
关注(0)|答案(1)|浏览(256)

嘿,我试着在我的flutter应用程序中使用webview,但它所做的只是返回一个空白的白页。
这是我的代码:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

 Completer<WebViewController> _controller = Completer<WebViewController>();

@override
  Widget build(BuildContext context) {
   return Scaffold(
       backgroundColor: Colors.transparent,
        body:
           WebView(
                  initialUrl: 'https://www.instagram.com/p/B_dvb9mAbxR/?utm_source=ig_web_copy_link',
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (WebViewController webViewController) {
                  _controller.complete(webViewController);
                  },

                  ),

   );

  }
}

拜托,我需要帮助

zysjyyx4

zysjyyx41#

这似乎是一个老的实现。下面是一个在Android和iOS上使用webview_flutter插件的当前实现的工作示例:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(
    const MaterialApp(
      home: WebViewApp(),
    ),
  );
}

class WebViewApp extends StatefulWidget {
  const WebViewApp({super.key});

  @override
  State<WebViewApp> createState() => _WebViewAppState();
}

class _WebViewAppState extends State<WebViewApp> {
  late final WebViewController controller;

  @override
  void initState() {
    super.initState();
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..loadRequest(
        Uri.parse('https://www.instagram.com'),
      );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter WebView'),
      ),
      body: WebViewWidget(
        controller: controller,
      ),
    );
  }
}

Android输出:

iOS输出:

相关问题