skip to Main Content

I am new to flutter and I think that I need help.
I am writing a native macOS app with flutter, and I need to display html contained in a String variable. I need to display it in a scrollable widget, such as SingleChildScrollView.
The html that I need to print is collected via the gmail API, so it is supposed to display emails from the API, written in html.

First I tried the librairies flutter_html and flutter_widget_from_html but they only work with basic html, when I am trying to display more heavy html, it crashes.

So i am now trying to display html via a webview, but I can’t make them work with a native macOS app.
I tried with webview_flutter, which doesn’t support macOS currently, so I moved to flutter_macos_webview, but it crashes when I try to open the webview. Here is the code.
The only thing that seems to work is the library desktop_webview_window, but it opens another window while I want an embedded widget, and I don’t know how to execute local html contained in a string instead of launching a website

Hope it’s clear, thanks for your help !

Here is the code of flutter_macos_webview that crash when I launch the webview.

import 'package:flutter/cupertino.dart';
import 'package:flutter_macos_webview/flutter_macos_webview.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  Future<void> _onOpenPressed(PresentationStyle presentationStyle) async {
    final webview = FlutterMacOSWebView(
      onOpen: () => print('Opened'),
      onClose: () => print('Closed'),
      onPageStarted: (url) => print('Page started: $url'),
      onPageFinished: (url) => print('Page finished: $url'),
      onWebResourceError: (err) {
        print(
          'Error: ${err.errorCode}, ${err.errorType}, ${err.domain}, ${err.description}',
        );
      },
    );

    await webview.open(
      url: 'https://google.com',
      presentationStyle: presentationStyle,
      size: Size(400.0, 400.0),
      userAgent:
          'Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
    );

    // await Future.delayed(Duration(seconds: 5));
    // await webview.close();
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          CupertinoButton(
            child: Text('Open as modal'),
            onPressed: () => _onOpenPressed(PresentationStyle.modal),
          ),
          SizedBox(height: 16.0),
          CupertinoButton(
            child: Text('Open as sheet'),
            onPressed: () => _onOpenPressed(PresentationStyle.sheet),
          ),
        ],
      ),
    );
  }
}

2

Answers


  1. here is an idea , why not create a temporarily html file then open it in browser like

    import 'dart:html' as html;
    html.window.open(url, name);
    
    Login or Signup to reply.
  2. You can use flutter_widget_from_html_core: ^0.10.3 package to display Html in your app.

    Or you can use webview_flutter_plus 0.3.0+2 to create a webview to display the html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search