A Flutter plug-in providing a native web view that loads Flutter assets. Useful for embdding HTML content in your application, for example to display help content.
Usage involves the following steps:
-
Add HTML assets to your application including CSS and images as needed.
A
pubspec.yaml
entry might look as follows:flutter: assets: - help/
example: example/pubspec.yaml
-
Add web content to your application assets.
example: example/help
-
Add the
AssetWebview
widget to your application.Create a page as follows:
return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Example Usage of asset_webview'), ), body: SafeArea( child: Column(children: [ Expanded( child: AssetWebview(initialUrl: 'asset://local/help/index.html')) ])), ), );
example: example/lib/main.dart
See the sample application for a complete application.
To link from web content to a named route in the app, do the following:
-
Create a function that can provide a
BuildContext
BuildContext _currentContext() => _navigatorKey.currentContext!; final _navigatorKey = GlobalKey<NavigatorState>();
-
Pass a
NavigationAssetWebviewController
to theAssetWebview
AssetWebview( initialUrl: 'asset://local/help/index.html', controller: NavigationAssetWebviewController(_currentContext) )
-
Provide named routes in the application
MaterialApp( navigatorKey: _navigatorKey, ... routes: {"about": (context) => AboutPage()}, );
-
Place navigation links in the html content:
<a href="navigation://about">About</a>
See the sample application for a complete application.
My assets aren't showing up (not found)
Check the pubspec.yaml
indentation. assets
must be indented one level under flutter
.
Per Flutter docs when opening in IntelliJ
open example/android/build.gradle
publishing: flutter pub publish
Copyright 2021 David Green
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.