当前位置: 首页 > article >正文

【Nativeshell】flutter的pc跨平台框架学习记录<二> 窗口间通信

首先是初始化:

查看Nativeshell的demo代码

// ignore_for_file: undefined_hidden_name, // not in main
import 'package:flutter/material.dart' hide MenuItem;
import 'package:nativeshell/nativeshell.dart';

import 'pages/other_window.dart';
import 'pages/platform_channels.dart';
import 'main_window.dart';
import 'pages/modal_window.dart';
import 'widgets/veil.dart';

void main() async {
  // Disable shader warmup - it delays producing first frame, which we want to
  // produce as soon as possible to reduce time to open new windows.
  disableShaderWarmUp();
  runApp(Main());
}

// Common scaffold code used by each window
class ExamplesWindow extends StatelessWidget {
  const ExamplesWindow({Key? key, required this.child}) : super(key: key);

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTextStyle(
        style: TextStyle(
          color: Colors.white,
          fontSize: 14,
        ),
        child: WindowLayoutProbe(child: child),
      ),
    );
  }
}

class Main extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Veil(
      child: Container(
        color: Color.fromARGB(255, 30, 30, 35),
        child: WindowWidget(
          onCreateState: (initData) {
            WindowState? state;

            state ??= PlatformChannelsWindowState.fromInitData(initData);
            state ??= ModalWindowState.fromInitData(initData);
            state ??= OtherWindowState.fromInitData(initData);
            state ??= MainWindowState();

            return state;
          },
        ),
      ),
    );
  }
}

这里要注意 ??= 表示 state如果为null则赋值后面的值,否则返回state的自己的值

子窗口:

PlatformChannelsWindowState.fromInitData(initData);
ModalWindowState.fromInitData(initData);
OtherWindowState.fromInitData(initData);

主窗口:
MainWindowState();

其中initData为创建子窗口时传递过来的参数

initData['class'] 这个字段用于标识应该启动哪个子窗口

通过子窗口主动调用子窗口中的方法,并传参

await otherWindow?.callMethod('showMessage', 'Hello from parent window!');

通过父窗口调用父窗口中的方法,并传参

await Window.of(context).parentWindow?.callMethod('showMessage', 'Hello');


http://www.kler.cn/a/417687.html

相关文章:

  • 一文速览DeepSeek-R1的本地部署——可联网、可实现本地知识库问答:包括671B满血版和各个蒸馏版的部署
  • 实战:如何利用网站日志诊断并解决收录问题?
  • 4 前置技术(下):git使用
  • 2023CCPC-Final A. Add One 2
  • git进阶--6---git stash
  • vscode软件操作界面UI布局@各个功能区域划分及其名称称呼
  • 条件数:概念、矩阵中的应用及实际工业场景应用
  • 鬼谷子的捭阖之道
  • BBC将 IT 系统迁移至基于AWS的RISE with SAP
  • 【PX4_Autopolite飞控源码】中飞控板初始化过程中的引脚IO控制(拉低/拉高)
  • YOLO系列论文综述(从YOLOv1到YOLOv11)【第15篇(完结):讨论和未来展望】
  • 数据结构 (14)数组的定义与运算
  • 【网络安全】记一次杀猪盘渗透实战
  • 36 基于单片机的电磁炉系统设计
  • Web3的核心技术解析:从区块链到智能合约的全景探索
  • HarmonyOS 应用跨团队 Debug 协作
  • 芯片测试-射频中的单位
  • Java面向对象. 多态
  • 二十一、QT C++
  • 家庭打印机如何连接电脑
  • 提升用户体验、创新产品与高效运营,企业发展三驾马车
  • 损失函数分类
  • 鸿蒙生态崛起的机遇有什么
  • 【人工智能】探索自然语言生成(NLG):用GPT生成文本
  • 23种设计模式-单例(Singleton)设计模式
  • 洛谷 P1746 离开中山路 C语言 bfs