{"id":6710,"date":"2023-02-22T11:53:51","date_gmt":"2023-02-22T02:53:51","guid":{"rendered":"https:\/\/www.lancard.com\/blog\/?p=6710"},"modified":"2025-03-12T11:23:58","modified_gmt":"2025-03-12T02:23:58","slug":"flutter_windows_toggle_ime","status":"publish","type":"post","link":"https:\/\/www.lancard.com\/blog\/2023\/02\/22\/flutter_windows_toggle_ime\/","title":{"rendered":"Flutter\u306eWindows\u30a2\u30d7\u30ea\u3067IME\u306e\u5207\u308a\u66ff\u3048"},"content":{"rendered":"<p>Flutter\u306eWindows\u30a2\u30d7\u30ea\u3067\u3001TextField\u30d5\u30a9\u30fc\u30ab\u30b9\u6642\u306bIME\u3092\u30aa\u30f3\/\u30aa\u30d5\u5207\u308a\u66ff\u3048\u308b\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u6700\u521d\u3001TextField\u306eTextInputType\u3092\u4f7f\u3063\u3066\u30ad\u30fc\u30dc\u30fc\u30c9\u3092<code>phone<\/code>\u3084<code>emailAddress<\/code>\u306b\u6307\u5b9a\u3059\u308c\u3070IME\u304c\u5207\u308a\u66ff\u308f\u308b\u306e\u3067\u306f\u3001\u3068\u601d\u3044\u8a66\u3057\u307e\u3057\u305f\u304c\u3001\u5207\u308a\u66ff\u308f\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002<br \/>\n\u30d7\u30e9\u30b0\u30a4\u30f3\u3067IME\u3092\u5207\u308a\u66ff\u3048\u3089\u308c\u308b\u3082\u306e\u3092\u63a2\u3057\u307e\u3057\u305f\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u4ee5\u4e0b\u3092\u53c2\u8003\u306bwin32 API\u3092\u547c\u3073\u51fa\u3059\u65b9\u6cd5\u3067\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002<br \/>\n<a href=\"https:\/\/docs.flutter.dev\/development\/platform-integration\/platform-channels#step-5-add-a-windows-platform-specific-implementation\">https:\/\/docs.flutter.dev\/development\/platform-integration\/platform-channels#step-5-add-a-windows-platform-specific-implementation<\/a><br \/>\n<!--more--><br \/>\n\u4e0a\u8a18\u306b\u66f8\u304b\u308c\u3066\u308b\u3088\u3046\u306b\u3001\u307e\u305a<code>windows\/runner\/flutter_window.cpp<\/code>\u306e<code>#include \"flutter_window.h\"<\/code>\u306e\u5f8c\u306b\u4ee5\u4e0b\u3092\u8ffd\u8a18\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"bash\">#include &lt;flutter\/event_channel.h&gt;\n#include &lt;flutter\/event_sink.h&gt;\n#include &lt;flutter\/event_stream_handler_functions.h&gt;\n#include &lt;flutter\/method_channel.h&gt;\n#include &lt;flutter\/standard_method_codec.h&gt;\n#include &lt;windows.h&gt;\n\n#include &lt;memory&gt;\n<\/code><\/pre>\n<p>dll\u3092\u547c\u3073\u51fa\u3059\u305f\u3081\u4ee5\u4e0b\u3092flutter_window.cpp\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"bash\">#pragma comment(lib, \"imm32.lib\")\n#pragma comment(lib, \"user32.lib\")\n<\/code><\/pre>\n<p><code>FlutterWindow::OnCreate<\/code> \u30e1\u30bd\u30c3\u30c9\u3092\u7de8\u96c6\u3057\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u540d <code>hoge.flutter.dev\/ime<\/code>\u306b\u7d50\u3073\u3064\u3044\u305f <code>flutter::MethodChannel<\/code>\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"bash\">bool FlutterWindow::OnCreate() {\n  \/\/ ...\n\n  flutter::MethodChannel&lt;&gt; channel(\n      flutter_controller_-&gt;engine()-&gt;messenger(), \"hoge.flutter.dev\/ime\",\n      &amp;flutter::StandardMethodCodec::GetInstance());\n  channel.SetMethodCallHandler(\n      [](const flutter::MethodCall&lt;&gt;&amp; call,\n         std::unique_ptr&lt;flutter::MethodResult&lt;&gt;&gt; result) {\n\n            HWND hwnd = GetForegroundWindow();\n            HIMC himc = ImmGetContext(hwnd);\n            if (call.method_name() == \"imeOn\") {\n                ImmSetOpenStatus(himc, true);\n            } else {\n                ImmSetOpenStatus(himc, false);\n            }\n            ImmReleaseContext(hwnd, himc);\n            result-&gt;Success();\n\n      });\n  return true;\n}\n\n<\/code><\/pre>\n<p>\u6b21\u306bFlutter\u5074\u3067\u30c1\u30e3\u30cd\u30eb\u3092\u4ecb\u3057\u3066\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306eWidget\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"bash\">import 'package:flutter\/material.dart';\nimport 'package:flutter\/services.dart';\n\nclass ToggleIme extends StatelessWidget {\n  final Widget child;\n  final bool enable;\n  const ToggleIme({super.key, required  this.child, required this.enable });\n  static const platform = MethodChannel('hoge.flutter.dev\/ime');\n\n  @override\n  Widget build(BuildContext context) {\n    return Focus(\n      skipTraversal: true,\n      child: child,\n      onFocusChange: (hasFocus) {\n        if (hasFocus) {\n          if (enable) {\n            platform.invokeMethod('imeOn');\n          } else {\n            platform.invokeMethod('imeOff');\n          }\n        }\n      } ,\n    );\n  }\n}\n<\/code><\/pre>\n<p>TextField\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u30e9\u30c3\u30d7\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"bash\">ToggleIme(\n    enable: false,\n    child: TextField(),\n)\n<\/code><\/pre>\n<p>\u3053\u308c\u3067TextField\u30d5\u30a9\u30fc\u30ab\u30b9\u6642\u306bIME\u304c\u30aa\u30d5\u306b\u306a\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F6710&amp;t=Flutter%E3%81%AEWindows%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7IME%E3%81%AE%E5%88%87%E3%82%8A%E6%9B%BF%E3%81%88&amp;s=100&amp;p[url]=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F6710&amp;p[images][0]=&amp;p[title]=Flutter%E3%81%AEWindows%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7IME%E3%81%AE%E5%88%87%E3%82%8A%E6%9B%BF%E3%81%88\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/facebook.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"http:\/\/twitter.com\/share?url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F6710&amp;text=Flutter%E3%81%AEWindows%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7IME%E3%81%AE%E5%88%87%E3%82%8A%E6%9B%BF%E3%81%88\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/twitter.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&amp;url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F6710&amp;title=Flutter%E3%81%AEWindows%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7IME%E3%81%AE%E5%88%87%E3%82%8A%E6%9B%BF%E3%81%88\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/linkedin.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-tumblr nolightbox\" data-provider=\"tumblr\" target=\"_blank\" rel=\"nofollow\" title=\"Share on tumblr\" href=\"https:\/\/tumblr.com\/share?s=&amp;v=3&amp;t=Flutter%E3%81%AEWindows%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7IME%E3%81%AE%E5%88%87%E3%82%8A%E6%9B%BF%E3%81%88&amp;u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F6710\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"tumblr\" title=\"Share on tumblr\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/tumblr.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Flutter%E3%81%AEWindows%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7IME%E3%81%AE%E5%88%87%E3%82%8A%E6%9B%BF%E3%81%88&amp;body=%E3%82%B7%E3%82%A7%E3%82%A2%E3%81%99%E3%82%8B%EF%BC%9A:%20https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F6710\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/mail.png\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>Flutter\u306eWindows\u30a2\u30d7\u30ea\u3067\u3001TextField\u30d5\u30a9\u30fc\u30ab\u30b9\u6642\u306bIME\u3092\u30aa\u30f3\/\u30aa\u30d5\u5207\u308a\u66ff\u3048\u308b\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u6700\u521d\u3001TextField\u306eTextInputType\u3092\u4f7f\u3063\u3066\u30ad\u30fc\u30dc\u30fc\u30c9\u3092phone\u3084email [&hellip;]<\/p>\n","protected":false},"author":25,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[215],"class_list":["post-6710","post","type-post","status-publish","format-standard","hentry","category-1","tag-flutter"],"_links":{"self":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/6710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/comments?post=6710"}],"version-history":[{"count":7,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/6710\/revisions"}],"predecessor-version":[{"id":6717,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/6710\/revisions\/6717"}],"wp:attachment":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/media?parent=6710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/categories?post=6710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/tags?post=6710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}