skip to Main Content

I’m working to host the Flutter Web app on GitHub, following below link:

How to Host your Flutter Web app on GitHub Pages

That is working successfully for me. But now need to Hosting Web Page with WebView

such as :

return WebView(
  initialUrl: 'https://my-proj.io/pagename',
),

instead use _loadHtmlFromAssets

2

Answers


  1. You can use webviewx package to render a webview in android|ios|web.

    Example:

    WebViewX(
    initialContent: '<h2> Hello, world! 
    </h2>',
    initialSourceType: SourceType.HTML,
    onWebViewCreated: (controller) => 
    webviewController = controller,
    ...
    ... other options
    );
    

    You can also interact with the controller.

    webviewController.loadContent(
    'https://flutter.dev',
    SourceType.url,
    );
    webviewController.goBack();
    
    webviewController.goForward();
    ...
    ...
    
    Login or Signup to reply.
  2. If you are looking to load from your assets directory, I’d recommend using the webview_flutter package:

    import 'package:webview_flutter/webview_flutter.dart';
    
    class WebViewExample extends StatefulWidget {
    
      const WebViewExample({
        Key? key,
      }) : super(key: key);
    
      @override
      State<WebViewExample> createState() => _WebViewExampleState();
    }
    
    class _WebViewExampleState extends State<WebViewExample> {
      late WebViewController _controller;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Index')),
          body: WebView(
            initialUrl: 'about:blank',
            onWebViewCreated: (WebViewController webViewController) {
              _controller = webViewController;
              _loadHtmlFromAssets();
            },
          ),
        );
      }
    
      _loadHtmlFromAssets() async => _controller.loadFlutterAsset('assets/data/index.html');
    }
    

    PS: Don’t forget to add your HTML file to your assets in pubspec.yaml (I nested mine under /assets/data in the example:

    flutter:
      assets:
        - assets/data/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search