当前位置:网站首页>Actual combat of flutter - customized keyboard (II)
Actual combat of flutter - customized keyboard (II)
2022-07-22 12:13:00 【Blue faced scholar】
It took two years flutter, Have some experience , Some cases will be updated from today , Not empty head and brain , Only for practical use , For study or use flutter My little friend's reference , Learning is still shallow , If there is anything wrong, we hope all gods will correct it , So as not to mislead others , Thank you here. ~( Originality is not easy. , Please indicate the source and author when forwarding )
Be careful : No special instructions ,flutter Version is 3.0+
Most of the code content in this article comes from https://github.com/Im-Kevin/cool_ui, Thank God for giving directions , You can go to the source code if you like , Of course, it is also different from the original author's implementation , Stay tuned .
In the first article , We introduced how to push up components like a system keyboard , In this article, let's take a look at what operations are done in initialization
After the first step of realizing the numeric keyboard , Then the key point of our next step is how to monitor the input , We know Flutter It is through the native channel to Android and ios The system sends instructions , For example, a value entered by the keyboard is to call "flutter/textinput" Sending input instructions to native .
One . Keyboard initialization
Add an interceptor in initialization , Deal with it in our own way
static init(KeyboardRootState root, BuildContext context) {
_root = root;
_context = context;
interceptorInput(); // Keyboard interception ,
}static interceptorInput() {
if (isInterceptor) return;
isInterceptor = true;
WidgetsBinding.instance.defaultBinaryMessenger
.setMessageHandler("flutter/textinput", _textInputHanlde); // Use your own processing
}
Here are important concepts BinaryMessenger, This is an abstract class , The official explanation is as follows
/// A messenger which sends binary data across the Flutter platform barrier.
We can cover his setMessageHandler How to deal with , What we realize is keyboard , Then the coverage is ”flutter/textinput” Treatment method .
Two . Key methods cover
1. 'TextInput.show'
TextField Call this method after getting the focus , In this method, we need to open the keyboard
static openKeyboard() {
var keyboardHeight = _currentKeyboard!.getHeight(_context!); // Get configuration height
_keyboardHeightNotifier.value = keyboardHeight;
if (_root!.hasKeyboard && _pageKey != null) return;
_pageKey = GlobalKey<KeyboardPageState>();
var tempKey = _pageKey;
_root!.setKeyboard((ctx) {
if (_currentKeyboard != null && _keyboardHeightNotifier.value != 0) {
return KeyboardPage(
key: tempKey,
builder: (ctx) {
return _currentKeyboard!
.builder(ctx, _keyboardController!, _keyboardParam);
},height: _keyboardHeightNotifier.value);
} else {
return Container();
}
});
The logic of opening the keyboard is to add a builder, This builder It's a keyboard page , And set the height , And assign it to _keyboardHeightNotifier, This was mentioned in the last article , Will automatically override viewInsets Value , Add system level masking
2. 'TextInput.hide'
Hidden keyboard , call root!.clearKeyboard(), That is to say builder Set to null .
3. 'TextInput.setClient'
stay TextInput.setClient Find the keyboard configuration corresponding to the corresponding keyboard according to the keyboard type defined in the input box , And initial input change monitoring KeyboardController.
At this time, we can customize our type and KeyboardController, Used to respond to our own keyboard events .
The key codes are as follows
_keyboardController = KeyboardController(client: client!)
..addListener(() {
var callbackMethodCall = MethodCall(
"TextInputClient.updateEditingState", [
_keyboardController!.client.connectionId,
_keyboardController!.value.toJSON()
]);
WidgetsBinding.instance.defaultBinaryMessenger
.handlePlatformMessage("flutter/textinput",
_codec.encodeMethodCall(callbackMethodCall), (data) {});
});
3、 ... and .KeyboardController
Control the input of text , And implement a series of buttons
addText(),deleteOne(), For entering and deleting characters , And line breaks , Complete and so on
/// complete
doneAction() {
CoolKeyboard.sendPerformAction(TextInputAction.done);
}/// next
nextAction() {
CoolKeyboard.sendPerformAction(TextInputAction.next);
}
/// the previous
preAction() {
CoolKeyboard.sendPerformAction(TextInputAction.previous);
}
among sendPerformAction Will call the native channel "TextInputClient.performAction", Used to respond to keyboard non input operations , These operations are defined in text_input.dart in .
static sendPerformAction(TextInputAction action) {
var callbackMethodCall = MethodCall("TextInputClient.performAction",
[_keyboardController!.client.connectionId, action.toString()]);
WidgetsBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
"flutter/textinput",
_codec.encodeMethodCall(callbackMethodCall),
(data) {});
}
Four . Implement keyboard components
This should be the simplest part of the full text , It's also the most basic part , use Row and Column Realize horizontal and vertical buttons
use controller.deleteOne() To achieve deletion
controller.nextAction() Implement line break
use controller.doneAction() Realize deletion
problem : There is a problem in the implementation , That is, all my buttons are used onTap Implement click event , When continuous clicks occur, characters will be lost
Solution
GestureDetector(
onPanDown: (details) {
controller.addText(value ?? title);
},
onPanDown, Call when touching the screen , Will take precedence onTap perform , For specific explanations, please refer to GestureDetector Source code , Altogether 7 Categories: 25 Kind of .
边栏推荐
- 传奇架设教程GOM引擎微端设置方法
- 9.5~10.5 GHz频段室内离体信道的测量与建模
- Real time joint simulation of smart grid hardware based on FPGA Jetson
- How does redis like and cancel? (glory Collection Edition)
- 网信办严厉查处诱导未成年人参与直播打赏行为:直播打赏行业乱象必须整治
- How to give selenium Chrome write extension intercepts or forwards requests
- Figure neural network driven traffic prediction technology: Exploration and challenge
- FTP service
- LeetCode:814. Binary tree pruning [recursion]
- As a test, how to effectively communicate with unreasonable developers
猜你喜欢
循环结构之for()循环和二次for嵌套
Detailed evaluation of current popular redis visual management tools
Titre 01: Registre distribué
Defect report operation
Web3 can't escape from the Wuzhishan of these old giants no matter how powerful it is
Jmeter-测试脚本学习(登录脚本)
windows服务器安全设置怎样操作,要注意什么?
传奇手游开服教程:传奇手游战神引擎架设教程
The evolution history of the background platform transformation of flybook management after the introduction of cloudwego
不掌握这些坑,你敢用BigDecimal吗?
随机推荐
windows服务器安全设置怎样操作,要注意什么?
[stl]vector Simulation Implementation
【oops-framework】全局事件
【MySQL】20-MySQL如何创建和管理表超详细汇总
Invasion investigation of HVV blue team
What is a single page application
Network metering - transport layer
C language explanation series -- Explanation of goto sentences and simple exercises of circular sentences
How to give selenium Chrome write extension intercepts or forwards requests
工业厂区三维仿真可视化展示的应用
Defect report operation
不谈源码,聊聊位运算的实际应用
Bloom filter and cuckoo filter (classic version)
盘一盘接口测试的那些痛点,你现在会解决了吗
Opencv将一个文件夹下所有图片合成视频
Why is it said that the software testing post is a huge pit? The 10-year-old tester told you not to be fooled
去中心化边缘渲染元宇宙协议Caduceus如何引爆Metaverse Summit 2022
Day010 cycle structure
Mapping of cyberspace assets
FPGA——SPI总线控制flash(1)(含代码)