From 5d9b19407f14593a9cb89ae1d89a8f266d006697 Mon Sep 17 00:00:00 2001 From: Junyuan Feng Date: Mon, 31 Jan 2022 16:26:01 +0800 Subject: [PATCH] Auto unfocus to hide IME --- lib/data/res/build_data.dart | 9 +++++---- lib/data/res/tab.dart | 2 +- lib/data/store/setting.dart | 3 +-- lib/view/page/convert.dart | 20 ++++--------------- lib/view/page/home.dart | 10 +++++++++- lib/view/page/ping.dart | 33 ++++++++------------------------ lib/view/page/setting.dart | 2 +- lib/view/widget/input_field.dart | 16 ++++++++++++++++ 8 files changed, 45 insertions(+), 50 deletions(-) create mode 100644 lib/view/widget/input_field.dart diff --git a/lib/data/res/build_data.dart b/lib/data/res/build_data.dart index a97c6fdb..c0119f5e 100644 --- a/lib/data/res/build_data.dart +++ b/lib/data/res/build_data.dart @@ -2,8 +2,9 @@ class BuildData { static const String name = "ServerBox"; - static const int build = 85; - static const String engine = "Flutter 2.8.1 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision 77d935af4d (5 weeks ago) • 2021-12-16 08:37:33 -0800\nEngine • revision 890a5fca2e\nTools • Dart 2.15.1\n"; - static const String buildAt = "2022-01-18 13:38:59.083528"; - static const int modifications = 1; + static const int build = 86; + static const String engine = + "Flutter 2.8.1 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision 77d935af4d (5 weeks ago) • 2021-12-16 08:37:33 -0800\nEngine • revision 890a5fca2e\nTools • Dart 2.15.1\n"; + static const String buildAt = "2022-01-19 16:54:18.470020"; + static const int modifications = 0; } diff --git a/lib/data/res/tab.dart b/lib/data/res/tab.dart index a147a006..fb289c39 100644 --- a/lib/data/res/tab.dart +++ b/lib/data/res/tab.dart @@ -1 +1 @@ -final List tabs = ['Servers', 'En/Decode', 'Ping']; \ No newline at end of file +final List tabs = ['Servers', 'En/Decode', 'Ping']; diff --git a/lib/data/store/setting.dart b/lib/data/store/setting.dart index cf4541ad..564ab569 100644 --- a/lib/data/store/setting.dart +++ b/lib/data/store/setting.dart @@ -6,6 +6,5 @@ class SettingStore extends PersistentStore { property('primaryColor', defaultValue: Colors.deepPurpleAccent.value); StoreProperty get serverStatusUpdateInterval => property('serverStatusUpdateInterval', defaultValue: 3); - StoreProperty get launchPage => - property('launchPage', defaultValue: 0); + StoreProperty get launchPage => property('launchPage', defaultValue: 0); } diff --git a/lib/view/page/convert.dart b/lib/view/page/convert.dart index 846c91b0..b722fb5c 100644 --- a/lib/view/page/convert.dart +++ b/lib/view/page/convert.dart @@ -4,6 +4,7 @@ import 'package:clipboard/clipboard.dart'; import 'package:flutter/material.dart'; import 'package:toolbox/core/utils.dart'; import 'package:toolbox/data/res/color.dart'; +import 'package:toolbox/view/widget/input_field.dart'; class ConvertPage extends StatefulWidget { const ConvertPage({Key? key}) : super(key: key); @@ -82,14 +83,14 @@ class _ConvertPageState extends State case 3: return Uri.decodeFull(text); default: - return 'Unknown'; + return 'Unknown Convert Method'; } } Widget _buildInputTop() { return SizedBox( height: _media.size.height * 0.33, - child: _buildInput(_textEditingController), + child: buildInput(context, _textEditingController), ); } @@ -161,20 +162,7 @@ class _ConvertPageState extends State Widget _buildResult() { return SizedBox( height: _media.size.height * 0.33, - child: _buildInput(_textEditingControllerResult), - ); - } - - Widget _buildInput(TextEditingController controller) { - return Card( - child: TextField( - maxLines: 20, - decoration: InputDecoration( - fillColor: Theme.of(context).cardColor, - filled: true, - border: InputBorder.none), - controller: controller, - ), + child: buildInput(context, _textEditingControllerResult), ); } diff --git a/lib/view/page/home.dart b/lib/view/page/home.dart index 18afe422..118b009d 100644 --- a/lib/view/page/home.dart +++ b/lib/view/page/home.dart @@ -44,7 +44,15 @@ class _MyHomePageState extends State super.initState(); _serverProvider = locator(); WidgetsBinding.instance?.addObserver(this); - _tabController = TabController(initialIndex: locator().launchPage.fetch()!, length: tabs.length, vsync: this); + _tabController = TabController( + initialIndex: locator().launchPage.fetch()!, + length: tabs.length, + vsync: this); + _tabController.addListener(() { + if (_tabController.index == 0) { + FocusScope.of(context).unfocus(); + } + }); } @override diff --git a/lib/view/page/ping.dart b/lib/view/page/ping.dart index 5b61a515..243ce774 100644 --- a/lib/view/page/ping.dart +++ b/lib/view/page/ping.dart @@ -5,6 +5,7 @@ import 'package:dart_ping_ios/dart_ping_ios.dart'; import 'package:flutter/material.dart'; import 'package:toolbox/core/utils.dart'; import 'package:toolbox/data/res/color.dart'; +import 'package:toolbox/view/widget/input_field.dart'; class PingPage extends StatefulWidget { const PingPage({Key? key}) : super(key: key); @@ -44,9 +45,10 @@ class _PingPageState extends State padding: const EdgeInsets.symmetric(horizontal: 7), child: Column(children: [ const SizedBox(height: 13), - _buildInputTop(), + buildInput(context, _textEditingController, + maxLines: 1, hint: 'Type here.'), _buildControl(), - _buildResult(), + buildInput(context, _textEditingControllerResult, hint: 'Result here.'), ])), onTap: () => FocusScope.of(context).requestFocus(FocusNode()), ), @@ -68,7 +70,9 @@ class _PingPageState extends State return SizedBox( height: 57, child: Card( - child: Row( + child: InkWell( + onTap: () => FocusScope.of(context).unfocus(), + child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ TextButton( @@ -109,28 +113,7 @@ class _PingPageState extends State ) ], ), - ), - ); - } - - Widget _buildInputTop() { - return _buildInput(_textEditingController, maxLines: 1, hint: 'Type here.'); - } - - Widget _buildResult() { - return _buildInput(_textEditingControllerResult, hint: 'Result here.'); - } - - Widget _buildInput(TextEditingController controller, {int maxLines = 20, String? hint}) { - return Card( - child: TextField( - maxLines: maxLines, - decoration: InputDecoration( - fillColor: Theme.of(context).cardColor, - hintText: hint, - filled: true, - border: InputBorder.none), - controller: controller, + ), ), ); } diff --git a/lib/view/page/setting.dart b/lib/view/page/setting.dart index a4d7f75b..a53af869 100644 --- a/lib/view/page/setting.dart +++ b/lib/view/page/setting.dart @@ -204,7 +204,7 @@ class _SettingPageState extends State { title: Text( e, style: TextStyle( - fontSize: 14, + fontSize: 14, color: _theme.textTheme.bodyText2!.color!.withAlpha(177)), ), trailing: _buildRadio(tabs.indexOf(e)), diff --git a/lib/view/widget/input_field.dart b/lib/view/widget/input_field.dart new file mode 100644 index 00000000..d79377fc --- /dev/null +++ b/lib/view/widget/input_field.dart @@ -0,0 +1,16 @@ +import 'package:flutter/material.dart'; + +Widget buildInput(BuildContext context, TextEditingController controller, + {int maxLines = 20, String? hint}) { + return Card( + child: TextField( + maxLines: maxLines, + decoration: InputDecoration( + fillColor: Theme.of(context).cardColor, + hintText: hint, + filled: true, + border: InputBorder.none), + controller: controller, + ), + ); +}