我确实有一个基本的 Flutter 应用程序,并且希望为 macOS 容器应用程序实现以下目标:
我们的想法是拥有一个 macOS 绘图应用程序,它仅充当完全透明的容器,没有标题或边框,同时完全透视和点击。 flutter 应用程序的侧面将有一个小菜单,可以在桌面上的任何位置绘制。
以下代码有效,除了macOS 应用程序使 flutter 小部件也透明。
// appDelegate.swift
import Cocoa
import FlutterMacOS
@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
// MainFlutterWindow.swift
import Cocoa
import FlutterMacOS
class MainFlutterWindow: NSWindow {
override func awakeFromNib() {
let flutterViewController = FlutterViewController.init()
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)
RegisterGeneratedPlugins(registry: flutterViewController)
super.awakeFromNib()
// make the window transparent
isOpaque = false
backgroundColor = NSColor.clear
}
}
```dart
// main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.transparent,
body: Center(
child: Text(
'Hello, World!',
style: TextStyle(fontSize: 24, color: Colors.white),
),
),
),
);
}
}
如果您使用https://pub.dev/packages/flutter_amic
,它就可以工作并设置:
@override
void initState() {
Window.setEffect(effect: WindowEffect.transparent);
Window.makeWindowFullyTransparent();
Window.enableFullSizeContentView();
Window.exitFullscreen();
// Window.hideWindowControls();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: CustomPaint(
painter: DrawPainter(),
child: Container(
),
),
);
}