英文:
Flutter platform-channel on windows
问题
以下是您要翻译的内容:
"I need to access Windows Desktop API to change network settings. I have read flutter document and experiences with flutter platform channel. When I ran the code, I just got true
in output, but I expect receiving string message from host.
Any help would be appreciated.
windows/runner/flutter_windows.cpp
file
#include "flutter_window.h"
#include <optional>
#include "flutter/generated_plugin_registrant.h"
#include <flutter/binary_messenger.h>
#include <flutter/standard_method_codec.h>
#include <flutter/method_channel.h>
#include <flutter/method_result_functions.h>
#include <iostream>
using namespace std;
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
: project_(project) {}
FlutterWindow::~FlutterWindow() {}
void initMethodChannel(flutter::FlutterEngine* flutter_instance) {
const static std::string channel_name("test_channel");
flutter::BinaryMessenger *messenger = FlEngine->messenger();
const flutter::StandardMethodCodec *codec = &flutter::StandardMethodCodec::GetInstance();
auto channel = std::make_unique<flutter::MethodChannel<>>(messenger ,channel_name ,codec);
auto channel =
std::make_unique<flutter::MethodChannel<>>(
flutter_instance->messenger(), channel_name,
&flutter::StandardMethodCodec::GetInstance());
channel->SetMethodCallHandler(
[](const flutter::MethodCall<>& call,
std::unique_ptr<flutter::MethodResult<>> result) {
// check method name called from dart
if (call.method_name().compare("test") == 0) {
// do what ever you want
res = flutter::EncodableValue("Channel is working!");
result->Success(res);
}
else {
result->NotImplemented();
}
}
);
}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
}
RECT frame = GetClientArea();
// The size here must match the window dimensions to avoid unnecessary surface
// creation / destruction in the startup path.
flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
frame.right - frame.left, frame.bottom - frame.top, project_);
// Ensure that basic setup of the controller was successful.
if (!flutter_controller_->engine() || !flutter_controller_->view()) {
return false;
}
RegisterPlugins(flutter_controller_->engine());
initMethodChannel(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());
flutter_controller_->engine()->SetNextFrameCallback([&]() {
this->Show();
});
return true;
}
void FlutterWindow::OnDestroy() {
if (flutter_controller_) {
flutter_controller_ = nullptr;
}
Win32Window::OnDestroy();
}
LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Give Flutter, including plugins, an opportunity to handle window messages.
if (flutter_controller_) {
std::optional<LRESULT> result =
flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
lparam);
if (result) {
return *result;
}
}
switch (message) {
case WM_FONTCHANGE:
flutter_controller_->engine()->ReloadSystemFonts();
break;
}
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}
调用dart中的平台和方法通道
MethodChannel channel = const MethodChannel('test_channel');
print(await channel.invokeMethod("test"));
输出
flutter: true
但期望在输出中获得 Channel is working!
。"
英文:
I need to access Windows Desktop API to change network settings. I have read flutter document and experiences with flutter platform channel. When I ran the code, I just got true
in output, but I expect receiving string message from host.
Any help would be appreciated.
windows/runner/flutter_windows.cpp
file
#include "flutter_window.h"
#include <optional>
#include "flutter/generated_plugin_registrant.h"
#include <flutter/binary_messenger.h>
#include <flutter/standard_method_codec.h>
#include <flutter/method_channel.h>
#include <flutter/method_result_functions.h>
#include <iostream>
using namespace std;
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
: project_(project) {}
FlutterWindow::~FlutterWindow() {}
void initMethodChannel(flutter::FlutterEngine* flutter_instance) {
const static std::string channel_name("test_channel");
flutter::BinaryMessenger *messenger = FlEngine->messenger();
const flutter::StandardMethodCodec *codec = &flutter::StandardMethodCodec::GetInstance();
auto channel = std::make_unique<flutter::MethodChannel<>>(messenger ,channel_name ,codec);
auto channel =
std::make_unique<flutter::MethodChannel<>>(
flutter_instance->messenger(), channel_name,
&flutter::StandardMethodCodec::GetInstance());
channel->SetMethodCallHandler(
[](const flutter::MethodCall<>& call,
std::unique_ptr<flutter::MethodResult<>> result) {
// check method name called from dart
if (call.method_name().compare("test") == 0) {
// do what ever you want
res = flutter::EncodableValue("Channel is working!");
result->Success(res);
}
else {
result->NotImplemented();
}
}
);
}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
}
RECT frame = GetClientArea();
// The size here must match the window dimensions to avoid unnecessary surface
// creation / destruction in the startup path.
flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
frame.right - frame.left, frame.bottom - frame.top, project_);
// Ensure that basic setup of the controller was successful.
if (!flutter_controller_->engine() || !flutter_controller_->view()) {
return false;
}
RegisterPlugins(flutter_controller_->engine());
initMethodChannel(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());
flutter_controller_->engine()->SetNextFrameCallback([&]() {
this->Show();
});
return true;
}
void FlutterWindow::OnDestroy() {
if (flutter_controller_) {
flutter_controller_ = nullptr;
}
Win32Window::OnDestroy();
}
LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Give Flutter, including plugins, an opportunity to handle window messages.
if (flutter_controller_) {
std::optional<LRESULT> result =
flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
lparam);
if (result) {
return *result;
}
}
switch (message) {
case WM_FONTCHANGE:
flutter_controller_->engine()->ReloadSystemFonts();
break;
}
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}
Calling the platform and method channel in dart
MethodChannel channel = const MethodChannel('test_channel');
print(await channel.invokeMethod("test"));
Output
flutter: true
But expect getting Channel is working!
in the output.
答案1
得分: 0
flutter_window.cpp
文件中存在一些错误:
1- 缺少头文件引用
我在文件顶部添加了这些头文件:
> + #include <string>
> + #include <flutter/encodable_value.h>
> + #include <../standard_codec.cc>
> + #include "windows.h"
> + #include "windef.h"
> + #include "windowsx.h"
并删除了这些头文件:
> - #include <iostream>
> - 使用命名空间 std;
2- 重复的 channel
声明:
在这种情况下,我需要这样声明:
auto channel = std::make_unique<flutter::MethodChannel<>>(messenger ,channel_name ,codec);
3- 未定义的 res
变量:
在使用之前,只需像下面这样定义它:
flutter::EncodableValue res;
res = flutter::EncodableValue("pass result here");
result->Success(res);
经过所有这些更改,它可以正常工作。
最终的 flutter_window.cpp
文件如下所示:
#include "flutter_window.h"
#include <optional>
#include "flutter/generated_plugin_registrant.h"
#include <string>
#include <flutter/binary_messenger.h>
#include <flutter/standard_method_codec.h>
#include <flutter/method_channel.h>
#include <flutter/method_result_functions.h>
#include <flutter/encodable_value.h>
#include <../standard_codec.cc>
#include "windows.h"
#include "windef.h"
#include "windowsx.h"
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
: project_(project) {}
FlutterWindow::~FlutterWindow() {}
void initMethodChannel(flutter::FlutterEngine* flutter_instance) {
const static std::string channel_name("test");
flutter::BinaryMessenger* messenger = flutter_instance->messenger();
const flutter::StandardMethodCodec* codec = &flutter::StandardMethodCodec::GetInstance();
auto channel = std::make_unique<flutter::MethodChannel<>>(messenger ,channel_name ,codec);
channel->SetMethodCallHandler(
[](const flutter::MethodCall<>& call,
std::unique_ptr<flutter::MethodResult<>> result) {
// 检查从 Dart 调用的方法名
if (call.method_name().compare("test") == 0) {
// 做你想做的事情
flutter::EncodableValue res;
res = flutter::EncodableValue("pass result here");
result->Success(res);
}
else {
result->NotImplemented();
}
}
);
}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
}
RECT frame = GetClientArea();
// 这里的大小必须与窗口尺寸相匹配,以避免在启动路径中不必要的表面创建/销毁。
flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
frame.right - frame.left, frame.bottom - frame.top, project_);
// 确保控制器的基本设置成功。
if (!flutter_controller_->engine() || !flutter_controller_->view()) {
return false;
}
RegisterPlugins(flutter_controller_->engine());
initMethodChannel(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());
flutter_controller_->engine()->SetNextFrameCallback([&]() {
this->Show();
});
return true;
}
void FlutterWindow::OnDestroy() {
if (flutter_controller_) {
flutter_controller_ = nullptr;
}
Win32Window::OnDestroy();
}
LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// 让 Flutter,包括插件,有机会处理窗口消息。
if (flutter_controller_) {
std::optional<LRESULT> result =
flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
lparam);
if (result) {
return *result;
}
}
switch (message) {
case WM_FONTCHANGE:
flutter_controller_->engine()->ReloadSystemFonts();
break;
}
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}
> 祝编码愉快!
英文:
There were some errors in the flutter_window.cpp
file:
1- Missing Header Inclusion
I added these headers at the top of the file:
> + #include <string>
> + #include <flutter/encodable_value.h>
> + #include <../standard_codec.cc>
> + #include "windows.h"
> + #include "windef.h"
> + #include "windowsx.h"
And removed these headers:
> - #include <iostream>
> - using namespace std;
2- Duplicated channel
declaration:
This is what I need in this situation:
auto channel = std::make_unique<flutter::MethodChannel<>>(messenger ,channel_name ,codec);
3- Undefined res
variable:
just defined it as below, before using it:
flutter::EncodableValue res;
res = flutter::EncodableValue("pass result here");
result->Success(res);
After all of those changes, It got working fine.
The final flutter_window.cpp
file is like that:
#include "flutter_window.h"
#include <optional>
#include "flutter/generated_plugin_registrant.h"
#include <string>
#include <flutter/binary_messenger.h>
#include <flutter/standard_method_codec.h>
#include <flutter/method_channel.h>
#include <flutter/method_result_functions.h>
#include <flutter/encodable_value.h>
#include <../standard_codec.cc>
#include "windows.h"
#include "windef.h"
#include "windowsx.h"
FlutterWindow::FlutterWindow(const flutter::DartProject& project)
: project_(project) {}
FlutterWindow::~FlutterWindow() {}
void initMethodChannel(flutter::FlutterEngine* flutter_instance) {
const static std::string channel_name("test");
flutter::BinaryMessenger* messenger = flutter_instance->messenger();
const flutter::StandardMethodCodec* codec = &flutter::StandardMethodCodec::GetInstance();
auto channel = std::make_unique<flutter::MethodChannel<>>(messenger ,channel_name ,codec);
channel->SetMethodCallHandler(
[](const flutter::MethodCall<>& call,
std::unique_ptr<flutter::MethodResult<>> result) {
// check method name called from dart
if (call.method_name().compare("test") == 0) {
// do what ever you want
flutter::EncodableValue res;
res = flutter::EncodableValue("pass result here");
result->Success(res);
}
else {
result->NotImplemented();
}
}
);
}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
}
RECT frame = GetClientArea();
// The size here must match the window dimensions to avoid unnecessary surface
// creation / destruction in the startup path.
flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
frame.right - frame.left, frame.bottom - frame.top, project_);
// Ensure that basic setup of the controller was successful.
if (!flutter_controller_->engine() || !flutter_controller_->view()) {
return false;
}
RegisterPlugins(flutter_controller_->engine());
initMethodChannel(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());
flutter_controller_->engine()->SetNextFrameCallback([&]() {
this->Show();
});
return true;
}
void FlutterWindow::OnDestroy() {
if (flutter_controller_) {
flutter_controller_ = nullptr;
}
Win32Window::OnDestroy();
}
LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Give Flutter, including plugins, an opportunity to handle window messages.
if (flutter_controller_) {
std::optional<LRESULT> result =
flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
lparam);
if (result) {
return *result;
}
}
switch (message) {
case WM_FONTCHANGE:
flutter_controller_->engine()->ReloadSystemFonts();
break;
}
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}
> Happy coding!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论