英文:
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 '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)),
],
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论