使用`desktop_multi_window`包在Flutter中如何向新窗口添加内容?

huangapple go评论241阅读模式
英文:

How to add content to a new window using flutter with desktop_multi_window package?

问题

我用这段代码打开了一个窗口,但没有添加内容的选项。

目前它会复制我的当前基础窗口。而且包的文档中没有解释。

https://pub.dev/packages/desktop_multi_window

final window = await DesktopMultiWindow.createWindow(jsonEncode({
   'args1': '子窗口',
   'args2': 100,
   'args3': true,
   'business': '业务测试',
}));
window
   ..setFrame(const Offset(0, 0) & const Size(1280, 720))
   ..center()
   ..setTitle('另一个窗口')
   ..show();

有人知道如何做吗?

感谢您的帮助。

英文:

I used this code for opening a window. but there is no option for adding content.

Currently it's duplicate my current base window. Also there is no explanation in the package documentation.

(https://pub.dev/packages/desktop_multi_window)

   final window = await DesktopMultiWindow.createWindow(jsonEncode({
      'args1': 'Sub window',
      'args2': 100,
      'args3': true,
      'business': 'business_test',
    }));
    window
      ..setFrame(const Offset(0, 0) & const Size(1280, 720))
      ..center()
      ..setTitle('Another window')
      ..show();

Does anyone know how to do it?

Appreciate your help.

答案1

得分: 2

以下是代码的中文翻译:

在包示例侧面,有一个示例代码发布;

    import 'dart:convert';
    
    import 'package:collection/collection.dart';
    import 'package:desktop_lifecycle/desktop_lifecycle.dart';
    import 'package:desktop_multi_window/desktop_multi_window.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_multi_window_example/event_widget.dart';
    
    void main(List<String> args) {
      if (args.firstOrNull == 'multi_window') {
        final windowId = int.parse(args[1]);
        final argument = args[2].isEmpty
            ? const {}
            : jsonDecode(args[2]) as Map<String, dynamic>;
        runApp(_ExampleSubWindow(
          windowController: WindowController.fromWindowId(windowId),
          args: argument,
        ));
      } else {
        runApp(const _ExampleMainWindow());
      }
    }
    
    class _ExampleMainWindow extends StatefulWidget {
      const _ExampleMainWindow({Key? key}) : super(key: key);
    
      @override
      State<_ExampleMainWindow> createState() => _ExampleMainWindowState();
    }
    
    class _ExampleMainWindowState extends State<_ExampleMainWindow> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Column(
              children: [
                TextButton(
                  onPressed: () async {
                    final window =
                        await DesktopMultiWindow.createWindow(jsonEncode({
                      'args1': 'Sub window',
                      'args2': 100,
                      'args3': true,
                      'business': 'business_test',
                    }));
                    window
                      ..setFrame(const Offset(0, 0) & const Size(1280, 720))
                      ..center()
                      ..setTitle('Another window')
                      ..resizable(false)
                      ..show();
                  },
                  child: const Text('Create a new World!'),
                ),
                TextButton(
                  child: const Text('Send event to all sub windows'),
                  onPressed: () async {
                    final subWindowIds =
                        await DesktopMultiWindow.getAllSubWindowIds();
                    for (final windowId in subWindowIds) {
                      DesktopMultiWindow.invokeMethod(
                        windowId,
                        'broadcast',
                        'Broadcast from main window',
                      );
                    }
                  },
                ),
                Expanded(
                  child: EventWidget(controller: WindowController.fromWindowId(0)),
                )
              ],
            ),
          ),
        );
      }
    }
    
    class _ExampleSubWindow extends StatelessWidget {
      const _ExampleSubWindow({
        Key? key,
        required this.windowController,
        required this.args,
      }) : super(key: key);
    
      final WindowController windowController;
      final Map? args;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Column(
              children: [
                if (args != null)
                  Text(
                    'Arguments: ${args.toString()}',
                    style: const TextStyle(fontSize: 20),
                  ),
                ValueListenableBuilder<bool>(
                  valueListenable: DesktopLifecycle.instance.isActive,
                  builder: (context, active, child) {
                    if (active) {
                      return const Text('Window Active');
                    } else {
                      return const Text('Window Inactive');
                    }
                  },
                ),
                TextButton(
                  onPressed: () async {
                    windowController.close();
                  },
                  child: const Text('Close this window'),
                ),
                Expanded(child: EventWidget(controller: windowController)),
              ],
            ),
          ),
        );
      }
    }

希望这能帮助您理解代码的中文翻译。如果您有任何其他问题,请随时提出。

英文:

At packages example side there is an example code posted;

import &#39;dart:convert&#39;;

import &#39;package:collection/collection.dart&#39;;
import &#39;package:desktop_lifecycle/desktop_lifecycle.dart&#39;;
import &#39;package:desktop_multi_window/desktop_multi_window.dart&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter_multi_window_example/event_widget.dart&#39;;

void main(List&lt;String&gt; args) {
  if (args.firstOrNull == &#39;multi_window&#39;) {
    final windowId = int.parse(args[1]);
    final argument = args[2].isEmpty
        ? const {}
        : jsonDecode(args[2]) as Map&lt;String, dynamic&gt;;
    runApp(_ExampleSubWindow(
      windowController: WindowController.fromWindowId(windowId),
      args: argument,
    ));
  } else {
    runApp(const _ExampleMainWindow());
  }
}

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

  @override
  State&lt;_ExampleMainWindow&gt; createState() =&gt; _ExampleMainWindowState();
}

class _ExampleMainWindowState extends State&lt;_ExampleMainWindow&gt; {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text(&#39;Plugin example app&#39;),
        ),
        body: Column(
          children: [
            TextButton(
              onPressed: () async {
                final window =
                    await DesktopMultiWindow.createWindow(jsonEncode({
                  &#39;args1&#39;: &#39;Sub window&#39;,
                  &#39;args2&#39;: 100,
                  &#39;args3&#39;: true,
                  &#39;business&#39;: &#39;business_test&#39;,
                }));
                window
                  ..setFrame(const Offset(0, 0) &amp; const Size(1280, 720))
                  ..center()
                  ..setTitle(&#39;Another window&#39;)
                  ..resizable(false)
                  ..show();
              },
              child: const Text(&#39;Create a new World!&#39;),
            ),
            TextButton(
              child: const Text(&#39;Send event to all sub windows&#39;),
              onPressed: () async {
                final subWindowIds =
                    await DesktopMultiWindow.getAllSubWindowIds();
                for (final windowId in subWindowIds) {
                  DesktopMultiWindow.invokeMethod(
                    windowId,
                    &#39;broadcast&#39;,
                    &#39;Broadcast from main window&#39;,
                  );
                }
              },
            ),
            Expanded(
              child: EventWidget(controller: WindowController.fromWindowId(0)),
            )
          ],
        ),
      ),
    );
  }
}

class _ExampleSubWindow extends StatelessWidget {
  const _ExampleSubWindow({
    Key? key,
    required this.windowController,
    required this.args,
  }) : super(key: key);

  final WindowController windowController;
  final Map? args;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text(&#39;Plugin example app&#39;),
        ),
        body: Column(
          children: [
            if (args != null)
              Text(
                &#39;Arguments: ${args.toString()}&#39;,
                style: const TextStyle(fontSize: 20),
              ),
            ValueListenableBuilder&lt;bool&gt;(
              valueListenable: DesktopLifecycle.instance.isActive,
              builder: (context, active, child) {
                if (active) {
                  return const Text(&#39;Window Active&#39;);
                } else {
                  return const Text(&#39;Window Inactive&#39;);
                }
              },
            ),
            TextButton(
              onPressed: () async {
                windowController.close();
              },
              child: const Text(&#39;Close this window&#39;),
            ),
            Expanded(child: EventWidget(controller: windowController)),
          ],
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年6月14日 23:53:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定