liyufengrex / flutter_subscreen_plugin

flutter插件:支持 Android 设备双屏显示,主副屏皆使用 flutter 绘制,通过 channel 双引擎实现主副屏通信交互。
BSD 3-Clause "New" or "Revised" License
124 stars 24 forks source link

关于副屏SubScreenPlugin.sendMsgToMainScreen 第一次无效的咨询 #16

Open the6nnoo opened 5 months ago

the6nnoo commented 5 months ago

当我在启动后直接 SubScreenPlugin.sendMsgToMainScreen时,主屏没有收到响应。 但是主屏SubScreenPlugin.sendMsgToViceScreen之后 一切就恢复正常了,请问作者这是什么情况呢?

下面是我的示例demo:

import 'dart:math';
import 'dart:ui';

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

void main() {
  var defaultRouteName = PlatformDispatcher.instance.defaultRouteName;
  if ("subMain" == defaultRouteName) {
    viceScreenMain();
  } else {
    defaultMain();
  }
}

//主屏ui
void defaultMain() {
  runApp(const MainApp());
}

//副屏ui
void viceScreenMain() {
  runApp(const SubApp());
}

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

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  Color _randomColor = Colors.white;

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

    SubScreenPlugin.mainStream.listen((e) {
      _randomColor =
          Colors.primaries[Random().nextInt(Colors.primaries.length)];
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
          body: Container(
        color: _randomColor,
        child: Center(
            child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextButton(
              onPressed: () {
                SubScreenPlugin.sendMsgToViceScreen("data",
                    params: {"params": "123"});
              },
              child: const Text('send msg to vice screen'),
            ),
            TextButton(
              onPressed: () {
                SubScreenPlugin.doubleScreenCancel();
              },
              child: const Text('close sub'),
            ),
            TextButton(
              onPressed: () {
                SubScreenPlugin.doubleScreenShow();
              },
              child: const Text('open sub'),
            ),
          ],
        )),
      )),
    );
  }
}

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

  @override
  State<SubApp> createState() => _SubAppState();
}

class _SubAppState extends State<SubApp> {
  Color _randomColor = Colors.white;

  @override
  void initState() {
    super.initState();
    SubScreenPlugin.viceStream.listen((e) {
      _randomColor =
          Colors.primaries[Random().nextInt(Colors.primaries.length)];
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, v) {
      return MaterialApp(
        home: Scaffold(
            body: Container(
          color: _randomColor,
          child: Center(
              child: TextButton(
                  onPressed: () {
                    SubScreenPlugin.sendMsgToMainScreen('data',
                        params: {"1": "1"});
                  },
                  child: const Text('send'))),
        )),
      );
    });
  }
}