Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display an HTML asset file?

Tags:

flutter

dart

I have an .html file in my assets directory.

How can I display / render it in Flutter?

like image 938
DogeLion Avatar asked Aug 01 '17 03:08

DogeLion


People also ask

What is an HTML asset?

The HTML Frame Asset enables the display of HTML content in an Intuiface experience as a lighter alternative to using the Web Browser Asset. The HTML content can be entered directly in Composer or retrieved through binding from any external source.

What is assets in HTML and CSS?

Assets are static files which will be deployed in the same directory as the generated documentation. Assets are usually used to add CSS, JavaScript or Images to the final documentation but they are not limited to those kind of files: any file type can be added as an asset to a template.

How do I read a file from assets folder in flutter?

How to Read Text File from Assets Folder: import 'package:flutter/services. dart'; String textasset = "assets/textfiles/file.


1 Answers

The package from the Flutter Team was released yesterday:

webview_flutter

How to load local asset:

Add the file to the project and update your pubspec:

//...
assets:
    //...
    - assets/yourFile.html
    //...

In your widget:

child: FutureBuilder<String>(
  future: LocalLoader().loadLocal(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      //                                return Text("${snapshot.data}");
      return WebView(
        initialUrl: new Uri.dataFromString(snapshot.data,
                mimeType: 'text/html')
            .toString(),
        // maybe you Uri.dataFromString(snapshot.data, mimeType: 'text/html', encoding: Encoding.getByName("UTF-8")).toString()
        javascriptMode: JavascriptMode.unrestricted,
      );
    } else if (snapshot.hasError) {
      return Text("${snapshot.error}");
    }

    return CircularProgressIndicator();
  },
),

Loading the text from the file:

class LocalLoader {
  Future<String> loadLocal() async {
    return await rootBundle.loadString('assets/yourFile.html');
  }
}
like image 131
Durdu Avatar answered Sep 18 '22 17:09

Durdu