
原文
https://medium.com/flutterdevs/keyboard-actions-in-flutter-ae4781286806
代碼
https://github.com/flutter-devs/KeyboardAction
參考
正文
了解如何在您的 Flutter 應(yīng)用程序自定義默認(rèn)鍵盤(pán)

Flutter 中的鍵盤(pán)動(dòng)作
安卓/IoS 提供的鍵盤(pán)沒(méi)有隱藏鍵盤(pán)的按鈕,這給用戶帶來(lái)了很多不便。當(dāng)我們的應(yīng)用程序有許多需要在工具欄上顯示操作鍵和處理定義為字段的函數(shù)的 textfield 時(shí)。鍵盤(pán)操作是當(dāng)用戶點(diǎn)擊當(dāng)前字段時(shí)指示該字段操作的鍵。
在這篇文章中,我將演示如何使用包含字段的表單輸入在應(yīng)用程序中顯示鍵盤(pán)操作。我們還將實(shí)現(xiàn)一個(gè)演示程序,并使用包 keyboard action 來(lái)演示這些特性。我試圖用一種簡(jiǎn)單的方式來(lái)解釋我的項(xiàng)目
簡(jiǎn)介:
KEYBOARD_ACTION 提供了幾個(gè)軟件包,使您的設(shè)備鍵盤(pán)可定制。今天,我們討論 KEYBOARD action。在 iOS 中有一個(gè)眾所周知的問(wèn)題,當(dāng)我們使用數(shù)字輸入字段時(shí),它不會(huì)顯示鍵盤(pán)內(nèi)部/上方的完成按鈕。因此,鍵盤(pán)操作提供了各種功能,有助于克服用戶和開(kāi)發(fā)人員目前面臨的問(wèn)題。
https://pub.dev/packages/keyboard_actions
特點(diǎn):
- 完成鍵盤(pán)按鈕(您可以自定義按鈕)
- 在文本字段之間上下移動(dòng)(可以隱藏設(shè)置)
nextFocus: false). - 鍵盤(pán)欄定制
- 鍵盤(pán)欄下方的自定義頁(yè)腳小部件
- 用簡(jiǎn)單的方法創(chuàng)建你自己的鍵盤(pán)
- 你可以在安卓、 iOS 或者兩個(gè)平臺(tái)上使用它
- 與對(duì)話框兼容
設(shè)立項(xiàng)目:
第一步: 使用包裝
keyboard_actions | Flutter Package
鍵盤(pán)操作 | Flutter Package
以一種簡(jiǎn)單的方式為 Android/iOS 鍵盤(pán)添加特性。因?yàn)榘沧?iOS 提供的鍵盤(pán)..
pub.dev
https://pub.dev/packages/keyboard_actions
在 pubspec.yaml 文件的依賴關(guān)系中添加 youtube player_iframe 插件,然后運(yùn)行 $flutter pub get 命令。
dependencies:
keyboard_actions: ^3.4.4
步驟 2: 將包導(dǎo)入為
import 'package:keyboard_actions/keyboard_actions.dart';
Code Implementation:
代碼實(shí)施:
1. 創(chuàng)建一個(gè)新的 dart 文件,名為home_screen.dart . 文件夾來(lái)設(shè)計(jì)用戶界面,并編寫(xiě)您希望在項(xiàng)目中實(shí)現(xiàn)的邏輯
2. 我在 flutter demo 項(xiàng)目中構(gòu)建了長(zhǎng)長(zhǎng)的 Forms,并在 Android 上運(yùn)行了這個(gè)應(yīng)用程序。如果我們使用的是 IOS 設(shè)備,那么它不會(huì)顯示done 完成 在 iOS 系統(tǒng)中,當(dāng)我們使用數(shù)字輸入字段時(shí),在 Android 系統(tǒng)中,鍵盤(pán)內(nèi)/上方的按鈕不會(huì)顯示
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
elevation: 8.0,
child: Container(
padding: EdgeInsets.only(left: 12),
child: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'Input Number with Custom Footer'),
controller: _customController,
focusNode: _nodeText6,
keyboardType: TextInputType._number_,
),
TextFormField(
decoration: InputDecoration(labelText: 'Input Number'),
focusNode: _nodeText1,
keyboardType: TextInputType._number_,
textInputAction: TextInputAction.next
),
TextFormField(
decoration: InputDecoration(
labelText: 'Custom cross Button'),
focusNode: _nodeText2,
keyboardType: TextInputType._text_,
),
TextFormField(
decoration: InputDecoration(
labelText: 'Input Number with Custom Action'),
focusNode: _nodeText3,
keyboardType: TextInputType._number_,
),
TextFormField(
decoration: InputDecoration(
labelText: 'Input Text without Done button'),
focusNode: _nodeText4,
keyboardType: TextInputType._text_,
),
TextFormField(
decoration: InputDecoration(
labelText: 'Input Number with Toolbar Buttons'),
focusNode: _nodeText5,
keyboardType: TextInputType._number_,
),
],
),
),
),
),
);
3. 現(xiàn)在,要在項(xiàng)目中添加鍵盤(pán)操作,您需要將所有 TextFormField 包裝在 Widget KeyboardAction 下,這個(gè) Widget KeyboardAction 需要 keyboardactivesconfig 配置才能在鍵盤(pán)上添加配置。
return KeyboardActions(
tapOutsideBehavior: TapOutsideBehavior.translucentDismiss,
_//autoScroll: true,_ config: _buildConfig(context),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
elevation: 8.0,
child: Container(
padding: EdgeInsets.only(left: 12),
child: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'Input Number with Custom Footer'),
controller: _customController,
focusNode: _nodeText6,
keyboardType: TextInputType._number_,
),
4. 提供了一個(gè)功能,當(dāng)我們按下設(shè)備屏幕上鍵盤(pán)以外的任何地方時(shí),可以關(guān)閉鍵盤(pán)。所以,我們需要加上這一行
tapOutsideBehavior: TapOutsideBehavior.translucentDismiss,
5. 我們應(yīng)該初始化分配給不同文本字段的 FocusNode 對(duì)象。因此,開(kāi)發(fā)人員進(jìn)行定制,因?yàn)樗试S鍵盤(pán)將焦點(diǎn)集中在這個(gè)小部件上。
final FocusNode _nodeText1 = FocusNode();//Add In TextFormField TextFormField(
decoration: InputDecoration(
labelText: 'Input Number with Toolbar Buttons'),
focusNode: _nodeText1,
keyboardType: TextInputType._number_,
)
6. 我們將定義 keyboardansconfig。包裝器為單個(gè)配置鍵盤(pán)的動(dòng)作欄。在 keyboardansconfig 中,我們根據(jù)您的需求為每個(gè) TextFormField 單獨(dú)定義由鍵盤(pán)執(zhí)行的操作。我們可以自定義鍵盤(pán)顏色,鍵盤(pán)和內(nèi)容之間的分隔線顏色,顯示箭頭前/后移動(dòng)輸入之間的焦點(diǎn)。
KeyboardActionsConfig _buildConfig(BuildContext context) {
return KeyboardActionsConfig(
keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
keyboardBarColor: Colors._grey_[200],
keyboardSeparatorColor: Colors._redAccent_,
nextFocus: true,
actions: [
7. 現(xiàn)在,我們將根據(jù)不同的 TextFormField 中的需求定義動(dòng)作。
actions: [
KeyboardActionsItem(
focusNode: _nodeText1,
),
8. 要在應(yīng)用程序中顯示帶有自定義頁(yè)腳的輸入,您需要在您的 KeyboardActionsItem 中實(shí)現(xiàn)下面的代碼,在這里我們必須在 Text 小部件中傳遞 TextController。
KeyboardActionsItem(
focusNode: _nodeText6,
footerBuilder: (_) => PreferredSize(
child: SizedBox(
height: 40,
child: Center(
child: Text(_customController.text),
)),
preferredSize: Size.fromHeight(40)),
),
9. 為了在你的應(yīng)用程序中顯示自定義對(duì)話框,將這個(gè)邏輯添加到 KeyboardActionsItem 中指定的焦點(diǎn)節(jié)點(diǎn)。
KeyboardActionsItem(
focusNode: _nodeText3,
onTapAction: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Text("Show Custom Action"),
actions: <Widget>[
FlatButton(
child: Text("OK"),
onPressed: () => Navigator._of_(context).pop(),
)
],
);
});
},
),
當(dāng)我們運(yùn)行應(yīng)用程序時(shí),我們得到屏幕的輸出視頻,如下所示,用戶可以觀察輸出。

結(jié)語(yǔ):
在本文中,我已經(jīng)簡(jiǎn)單地介紹了 KeyboardAction 包的基本結(jié)構(gòu); 您可以根據(jù)自己的選擇修改這段代碼,也可以根據(jù)自己的需求使用這個(gè)包。這是一個(gè)小的介紹鍵盤(pán)定制用戶交互從我這邊,它的工作使用 Flutter 。
? 貓哥
微信群 ducafecat
往期
開(kāi)源
GetX Quick Start
https://github.com/ducafecat/getx_quick_start
新聞客戶端
https://github.com/ducafecat/flutter_learn_news
strapi 手冊(cè)譯文
微信討論群 ducafecat
系列集合
譯文
https://ducafecat.tech/categories/%E8%AF%91%E6%96%87/
開(kāi)源項(xiàng)目
https://ducafecat.tech/categories/%E5%BC%80%E6%BA%90/
Dart 編程語(yǔ)言基礎(chǔ)
https://space.bilibili.com/404904528/channel/detail?cid=111585
Flutter 零基礎(chǔ)入門(mén)
https://space.bilibili.com/404904528/channel/detail?cid=123470
Flutter 實(shí)戰(zhàn)從零開(kāi)始 新聞客戶端
https://space.bilibili.com/404904528/channel/detail?cid=106755
Flutter 組件開(kāi)發(fā)
https://space.bilibili.com/404904528/channel/detail?cid=144262
Flutter Bloc
https://space.bilibili.com/404904528/channel/detail?cid=177519
Flutter Getx4
https://space.bilibili.com/404904528/channel/detail?cid=177514
Docker Yapi
https://space.bilibili.com/404904528/channel/detail?cid=130578