import_js_library 0.0.2 import_js_library: ^0.0.2 copied to clipboard
Import & use javascript libraries in your flutter web projects
Import JS Library #
Import & use javascript libraries in your flutter web projects.
Created to make it simpler to build Flutter Plugins compatible with web.
Example #
to include howler.js & use it in a flutter web project :
- Create your plugin Package
flutter create --template=package audio_plugin_example
Then
- Add the js library in your assets
- Declare it inside your pubspec.yaml
flutter:
assets:
- assets/howler.js
- In your Flutter plugin project, import this js lib
For example, on the registerWith()
pluginName: the name of your plugin, based on pubspecs.yaml, here audio_plugin_example
class AudioPlugin {
static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'audio_plugin',
const StandardMethodCodec(),
registrar.messenger,
);
importJsLibrary(url: "./assets/howler.js", pluginName: "audio_plugin_example");
final AudioPlugin instance = AudioPlugin();
channel.setMethodCallHandler(instance.handleMethodCall);
}
...
- Using package:js, wrap your js methods/classes
@JS()
library howl.js;
import 'package:js/js.dart';
@JS("Howl")
class Howl {
external Howl({List<String> src});
external play();
}
- Use your library !
final audio = Howl(src: ["./assets/astronomia.mp3"]);
audio.play();
for example in the plugin
Howl audio;
Future<dynamic> handleMethodCall(MethodCall call) async {
print(call.method);
switch (call.method) {
case "play":
if(audio != null){
audio.play();
}
break;
case "pause":
if(audio != null){
audio.pause();
}
break;
case "open":
final String path = call.arguments["path"];
audio = Howl(src: [path]);
break;
}
}