desktop_webview_linux 0.1.4 copy "desktop_webview_linux: ^0.1.4" to clipboard
desktop_webview_linux: ^0.1.4 copied to clipboard

PlatformLinux

Webview on Linux using webkit2gtk. Linux implementation of desktop_webview_window.

example/lib/main.dart

import 'dart:developer' show log;
import 'dart:io' show Directory, File, Platform;

import 'package:desktop_webview_linux/desktop_webview_linux.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  Future<void> _openWebView() async {
    if (!Platform.isLinux) {
      return;
    }

    const htmlContent = '''
      <!DOCTYPE html>
      <html>
        <head><title>Demo</title></head>
        <body>
          <h2>Click to send message to Dart</h2>
          <button onclick="window.webkit.messageHandlers.onTokenReceived.postMessage('test-token')">
            Send Token
          </button>
        </body>
      </html>
    ''';

    const jsCode = '''
      console.log('JS code injected');
    ''';

    var handled = false;

    try {
      final Directory tempDir = await getTemporaryDirectory();
      final htmlFile = File(p.join(tempDir.path, 'webview_test.html'));
      await htmlFile.writeAsString(htmlContent);

      final Directory documentDir = await getApplicationDocumentsDirectory();
      final String webviewPath = p.join(
        documentDir.path,
        'desktop_webview_window',
      );

      final config = CreateConfiguration(
        title: 'WebView Example',
        windowWidth: 500,
        windowHeight: 400,
        userDataFolderWindows: webviewPath,
      );

      final Webview webview = await WebviewWindow.create(configuration: config);
      log(
        'WebView created, launching...',
        name: 'desktop_webview_linux.example',
      );

      webview
        ..registerJavaScriptMessageHandler('onTokenReceived', (name, body) {
          if (handled) {
            return;
          }
          handled = true;
          log(
            'JS Message received: $body',
            name: 'desktop_webview_linux.example',
          );
          Future.delayed(const Duration(milliseconds: 300), webview.close);
        })
        ..registerJavaScriptMessageHandler('onError', (name, body) {
          if (handled) {
            return;
          }
          handled = true;
          log('JS Error: $body', name: 'desktop_webview_linux.example');
          Future.delayed(const Duration(milliseconds: 300), webview.close);
        })
        ..launch(htmlFile.uri.toString());

      await Future<void>.delayed(const Duration(milliseconds: 1200));
      await webview.evaluateJavaScript(jsCode);
    } on Object catch (e, stack) {
      log(
        'Exception in WebView',
        name: 'desktop_webview_linux.example',
        error: e,
        stackTrace: stack,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Linux WebView (inline)')),
        body: Center(
          child: ElevatedButton(
            onPressed: _openWebView,
            child: const Text('Open WebView'),
          ),
        ),
      ),
    );
  }
}
2
likes
160
points
199
downloads

Documentation

API reference

Publisher

verified publishercarapacik.dev

Weekly Downloads

Webview on Linux using webkit2gtk. Linux implementation of desktop_webview_window.

Repository (GitHub)
View/report issues

Topics

#webview #linux #webkit2gtk #webkitgtk

License

MIT (license)

Dependencies

flutter

More

Packages that depend on desktop_webview_linux

Packages that implement desktop_webview_linux