geense_plugin 1.0.0 copy "geense_plugin: ^1.0.0" to clipboard
geense_plugin: ^1.0.0 copied to clipboard

测试展示互动iOS插件.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:geense_plugin/geense_plugin.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
//****************flutter主动调用原生方法****************//
  String _nativeCallBackValue = '等待原生传值';
  @override
  void initState() {
// TODO: implement initState
    //实现通道的监听,并传入两个带有参数的函数用于监听到数据后 对数据进行处理
    eventChannel.receiveBroadcastStream()
        // ignore: use_function_type_syntax_for_parameters
        .listen((Event) {
      _receiveFromeNative(Event);
      onError:
      _fromNativeError;
    });
    super.initState();
    initPlatformState();
    _communicateFunction(" flutter to ios ");
  }

  // 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 GeensePlugin.platformVersion ?? 'Unknown platform version';
      platformVersion = await GeensePlugin.gennsee ?? "啥也不是";
    } 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;
    });
  }

//异步执行调用原生方法,保持页面不卡住,因为调用原生的方法可能没实现会抛出异常,所以trycatch包住
  Future<void> _communicateFunction(flutterPara) async {
    try {
      //原生方法名为callNativeMethond,flutterPara为flutter调用原生方法传入的参数,await等待方法执行
      final result = await GeensePlugin.callNativeMethond(flutterPara) ?? "";
      //如果原生方法执行回调传值给flutter,那下面的代码才会被执行
      _nativeCallBackValue = result;

      print('object');
      print('object1');
      print('object2');
      // ignore: nullable_type_in_catch_clause
    } on PlatformException catch (e) {
      //抛出异常
      //flutter: PlatformException(001, 进入异常处理, 进入flutter的trycatch方法的catch方法)
      print(e);
    }
  }

//****************原生主动调用flutter****************//
  //存放原生传给flutter的值
  String _nativeToFlutterString = '等待iOS传值';

  //注册监听原生通道
  EventChannel eventChannel = EventChannel('ios_to_flutter');

  //监听到数据后用于处理数据的方法,这个函数是用于处理接收到原生传进来的数据的,可自行定义
  void _receiveFromeNative(Map para) {
    print(para);
    setState(() {
      // _nativeToFlutterString = para.toString();
      _nativeToFlutterString = para["key"];
    });
  }

  //原生返回错误信息
  void _fromNativeError(Object error) {
    print(error);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text(_platformVersion),
              ElevatedButton(
                onPressed: () async {
                  var dic = {};
                  dic.addAll({
                    "domain": "training.ttcdw.cn",
                    "roomNumber": "55664669",
                    "nickName": "Cooper",
                    "watchPassword": "662778",
                    "liveName": "直播"
                  });
                  String? message = await GeensePlugin.seelive(dic);
                  // ignore: avoid_print
                  print(message);
                },
                child: const Text(
                  '参数传递',
                  style: TextStyle(
                    fontSize: 14.0,
                  ),
                ),
              ),
              MaterialButton(
                color: Colors.green,
                child: Text(_nativeToFlutterString),
                onPressed: () {},
              ),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
130
pub points
0%
popularity

Publisher

unverified uploader

测试展示互动iOS插件.

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on geense_plugin