yc_plugin_learn 0.0.5 copy "yc_plugin_learn: ^0.0.5" to clipboard
yc_plugin_learn: ^0.0.5 copied to clipboard

outdated

演示项目:flutter项目嵌入原生代码、嵌入原生view,双方交互事件,在原生插件中使用第三方工具,

example/lib/main.dart

import 'dart:io';
import 'dart:typed_data';

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

import 'package:flutter/services.dart';
import 'package:yc_plugin_learn/yc_plugin_learn.dart';
import 'package:yc_plugin_learn_example/TestView.dart';

/**
 * 本插件项目主要演示以下几个方面,主要用于教学使用
 * 1。flutter与原生互相传递事件信息
 * 2。在原生项目中使用第三方包glide加载图片,主要用于演示如何在插件中使用第三方包
 * 3。flutter页面上嵌入原生view,并实现独立的事件互传
 * 4。如何在插件代码里获取当前Context
 * 5。插件开发中Android代码报红的解决
 *
 */

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>
    implements YcPluginCallBack /* 实现插件接口,用于接收原生主动发送的消息 */ {
  String _platformVersion = 'Unknown';
  var _data = null;
  double width = 300;

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

    //初始化
    YcPluginLearn.init(appId: "appId").then((value) {
      print("flutter init ${value}");

      //测试,打开便可看到控制台疯狂的输出来自原生的回调
      //YcPluginLearn.test();
    });

    //设置回调
    YcPluginLearn.ycPluginCallBack = this;
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion =
          await YcPluginLearn.platformVersion ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    if (!mounted) return;
    setState(() {
      _platformVersion = platformVersion;
    });
  }

  //加载图片数据
  initData() async {
    _data = await YcPluginLearn.getImage(
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653472637&t=0a02c0594866f17f15d29a4753161f6e");
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              _data == null
                  ? Container(
                      color: Colors.grey,
                      width: 100,
                      height: 100,
                    )
                  : Image.memory(
                      _data,
                      width: 100,
                      height: 100,
                      fit: BoxFit.cover,
                    ),

              GestureDetector(
                onTap: () {
                  print("点我了");
                  width += 5;
                  setState(() {});
                },
                child: Container(
                  height: 40,
                  child: Text("点我刷新页面宽度"),
                  color: Colors.blue,
                ),
              ),

              ///用来加载原生view
              TestView(
                width: width,
              ),

              Container(
                height: 20,
              ),

              ///另一个相同的组件
              TestView(
                width: width,
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void minuteCallBack(String minute) {
    // TODO: implement minuteCallBack
    print("minuteCallBack minute = ${minute}");
  }

  @override
  void secondCallBack(String second) {
    // TODO: implement secondCallBack
    print("secondCallBack second = ${second}");
  }
}
0
likes
0
points
51
downloads

Publisher

unverified uploader

Weekly Downloads

演示项目:flutter项目嵌入原生代码、嵌入原生view,双方交互事件,在原生插件中使用第三方工具,

Homepage

License

unknown (license)

Dependencies

flutter

More

Packages that depend on yc_plugin_learn

Packages that implement yc_plugin_learn