Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter Tour——Hello World Windows Application #214

Open
soapgu opened this issue Jul 29, 2023 · 0 comments
Open

Flutter Tour——Hello World Windows Application #214

soapgu opened this issue Jul 29, 2023 · 0 comments
Labels

Comments

@soapgu
Copy link
Owner

soapgu commented Jul 29, 2023

  • 补上Windows平台的空白

前面因为一直是在macOS上面做的Flutter的实验,所以暂时没试过Windows 的应用,这次用Windows补齐一下
先看下Windows的安装要求

其他的安卓通用部分可以忽略
对于 Windows 桌面开发而言,除了 Flutter SDK 以外你还需要以下内容:
Visual Studio 2022Visual Studio 2022 生成工具 在选择安装 Visual Studio 时或只安装生成工具的时候,你需要选择「使用 C++ 的桌面开发」,包括其所有默认组件,以安装必要的 C++ 工具链和 Windows SDK 的头文件。

可以看出Windows程序的基础在VC++,原生代码部分基本和C#没有什么关系了(后面再分析)

  • 安装&更新

不知道是去年还是前年的时候其实已经安装过一次,这次实际是要做升级了!

准备工作

接下来只要执行flutter upgrade就行,简单吧

参考资料

C:\Users\guhui>flutter upgrade
ProcessException: Process exited abnormally:
fatal: unable to access 'https://github.com/flutter/flutter.git/': OpenSSL SSL_read: Connection was reset, errno 10054
  Command: git fetch --tags

C:\Users\guhui>flutter upgrade
Upgrading Flutter to 3.10.6 from 2.2.3 in D:\flutter...
Checking Dart SDK version...
Downloading Dart SDK from Flutter engine cdbeda788a293fa29665dc3fa3d6e63bd221cb0d...
Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...
Expanding downloaded archive...
Building flutter tool...
Running pub upgrade...
Resolving dependencies...
Got dependencies.

Upgrading engine...
Downloading Material fonts...                                       5.8s
Downloading android-arm-profile/windows-x64 tools...               20.1s
Downloading android-arm-release/windows-x64 tools...               21.4s
Downloading android-arm64-profile/windows-x64 tools...             33.9s
Downloading android-arm64-release/windows-x64 tools...             24.4s
Downloading android-x64-profile/windows-x64 tools...               22.0s
Downloading android-x64-release/windows-x64 tools...               21.1s
Downloading android-x86 tools...                                  193.6s
Downloading android-x64 tools...                                  145.3s
Downloading android-arm tools...                                  130.4s
Downloading android-arm-profile tools...                           71.9s
Downloading android-arm-release tools...                           50.7s
Downloading android-arm64 tools...                                137.1s
Downloading android-arm64-profile tools...                         65.3s
Downloading android-arm64-release tools...                         49.3s
Downloading android-x64-profile tools...                           54.8s
Downloading android-x64-release tools...                           80.6s
Downloading android-x86-jit-release tools...                      105.2s
Downloading Web SDK...                                            647.1s
Downloading package sky_engine...                                  13.7s
Downloading flutter_patched_sdk tools...                           48.9s
Downloading flutter_patched_sdk_product tools...                   41.9s
Downloading windows-x64 tools...                                  373.3s
Downloading windows-x64-debug/windows-x64-flutter tools...        814.0s
Downloading windows-x64/flutter-cpp-client-wrapper tools...        598ms
Downloading windows-x64-profile/windows-x64-flutter tools...       656.1s
Downloading windows-x64-release/windows-x64-flutter tools...       742.2s
Downloading windows-x64/font-subset tools...                     2,936ms

Flutter 3.10.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f468f3366c (2 weeks ago) • 2023-07-12 15:19:05 -0700
Engine • revision cdbeda788a
Tools • Dart 3.0.6 • DevTools 2.23.1

Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.10.6, on Microsoft Windows [版本 10.0.19045.3208], locale zh-CN)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Enterprise 2019 16.11.11)
[√] Android Studio (version 2021.2)
[√] VS Code (version 1.73.0)
[√] Connected device (3 available)
[!] Network resources
    X A network error occurred while checking "https://maven.google.com/": 信号灯超时时间已到


! Doctor found issues in 2 categories.

Upgrading Flutter to 3.10.6 from 2.2.3,更新幅度还蛮大的。
吐槽下,更新速度有点慢,基本上花了1个小时时间~~~

  • 运行测试

创建项目可以用Flutter命令行或者VSCode插件,这个没有区别。这里我就不debug启动了,直接执行flutter run

PS D:\WorkSpace\PlayPen\flutter_windows> flutter run
Connected devices:
Windows (desktop) • windows • windows-x64    • Microsoft Windows [版本 10.0.19045.3208]
Chrome (web)      • chrome  • web-javascript • Google Chrome 104.0.5112.101
Edge (web)        • edge    • web-javascript • Microsoft Edge 114.0.1823.82
[1]: Windows (windows)
[2]: Chrome (chrome)
[3]: Edge (edge)
Please choose one (or "q" to quit): 1
Launching lib\main.dart on Windows in debug mode...
Building Windows application...                                    30.9s
√  Built build\windows\runner\Debug\flutter_windows.exe.
Syncing files to device Windows...                                  79ms

Flutter run key commands.
r Hot reload. 
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on Windows is available at: http://127.0.0.1:60610/bVxsqQIs9Qw=/
The Flutter DevTools debugger and profiler on Windows is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:60610/bVxsqQIs9Qw=/

基本没碰上什么问题,比较顺利
图片

  • Windows原生代码简单分析

先看代码结构
图片
果然是C++源码
再看程序主入口

#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>

#include "flutter_window.h"
#include "utils.h"

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
                      _In_ wchar_t *command_line, _In_ int show_command) {
  // Attach to console when present (e.g., 'flutter run') or create a
  // new console when running with a debugger.
  if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
    CreateAndAttachConsole();
  }

  // Initialize COM, so that it is available for use in the library and/or
  // plugins.
  ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

  flutter::DartProject project(L"data");

  std::vector<std::string> command_line_arguments =
      GetCommandLineArguments();

  project.set_dart_entrypoint_arguments(std::move(command_line_arguments));

  FlutterWindow window(project);
  Win32Window::Point origin(10, 10);
  Win32Window::Size size(1280, 720);
  if (!window.Create(L"flutter_windows", origin, size)) {
    return EXIT_FAILURE;
  }
  window.SetQuitOnClose(true);

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

  ::CoUninitialize();
  return EXIT_SUCCESS;
}

对VC的印象基本也就停在大学上的《Windows API》这节课了
消息队列和消息泵是yyds。

#include "flutter_window.h"

#include <optional>

#include "flutter/generated_plugin_registrant.h"

FlutterWindow::FlutterWindow(const flutter::DartProject& project)
    : project_(project) {}

FlutterWindow::~FlutterWindow() {}

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());
  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);
}

看下对窗口的实现,大致可以看出VC程序启动以及更新engine的过程。

  • 大致总结

可以看出Windows应用也基本和安卓和iOS等平台一样,都是一个壳子,主要核心还是engine和dart源码。
但是windows的项目集成来说就更弱了

  • 由于使用了更底层的VC,.net framework就被抛弃了。从商用代码上讲,WPF桌面程序是“性价比”最高的。windows项目没有了混编可能性,也就陷入了“有我没他有他没我”的境地了。但是dart的生态和WPF的生态PK也不乐观啊,虽然WPF基本输入活死人状态,但是好歹经营多年。
  • 还是有优势的,毕竟VC++的代码效率还是可以PK一下C#托管代码的,性能上应该问题不大。

感觉相比安卓和iOS,windows平台更差一些。感觉Flutter一个个拉出来单打肯定吃亏。如果凑出一堆兄弟(平台)出来还能看下,这也是Flutter的最大看点了。野心很大,但是具体怎么样还是要走着瞧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant