native_web 0.0.8 copy "native_web: ^0.0.8" to clipboard
native_web: ^0.0.8 copied to clipboard

a project that provides native interaction with js

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:native_web/common/Global.dart';
import 'package:native_web/native_web.dart';
import 'package:native_web/common/route_util.dart';
import 'package:native_web/route/dialog_update.dart';
import 'package:native_web/route/webviewpage_inapp.dart';

void main() {
  Global.init().then((value) => runApp(MyApp(
        homeState: value,
      )));
}

class MyApp extends StatelessWidget {
  MyApp({Key? key, required this.homeState}) : super(key: key);

  num homeState;

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        textTheme: const TextTheme(
          bodySmall: TextStyle(fontSize: 16),
          bodyLarge: TextStyle(fontSize: 18),
        ),
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      // home: const WebViewPage_InApp(),
      home: _getHome(), // 注册路由表
      // routes: <String, WidgetBuilder>{
      //   "webView": (context) => const WebViewPage_InApp(),
      // },
    );
  }

  Widget _getHome() {
    switch (homeState) {
      case 0:
        return const DialogUpdate();
      case 1:
        return const WebViewPageInApp();
      case 2:
        return const MyHomePage();
    }
    return const MyHomePage();
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyAppState();
}

class _MyAppState extends State<MyHomePage> {
  String _platformVersion = 'Unknown';
  final _nativeWebPlugin = NativeWeb();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _nativeWebPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _incrementCounter() {
    RouteUtil.routeToNext(context, toNext: (decodeData) {
      debugPrint("跳过web进入主页");
    });
    // Navigator.pushNamed(context, "webView");
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // routes: <String, WidgetBuilder>{
      //   "webView": (context) => const WebViewPage_InApp(),
      // },
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}