Flutter自定义键盘插件ity_customized_keyboard的使用
Flutter自定义键盘插件ity_customizeded_keyboard的使用
1. 整理后的内容中尽量提供关于“Flutter自定义键盘插件ity_customizeded_keyboard的使用”的完整示例demo
import 'package:flutter/material.dart';
import 'package:ity_customizeded_keyboard/ity_customizeded_keyboard.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final keyboardMomo = CustomKeyboard(
keyEvent: CustomKeyboardEvent.clear(),
borderRadius: BorderRadius.circular(1),
padding: EdgeInsets.all(2),
color: Colors.red,
child: Center(child: Text('AC')),
);
@override
double get height => 230;
@override
String get name => 'YOUR_NAME_KEYBOARD';
@override
Color get backgroundColor => const Color(0xEAEFEFEF);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: KeyboardWrapper(
keyboards: [keyboardMomo],
child: Scaffold(
appBar: AppBar(title: const Text('ity_customizeded_keyboard')),
resizeToAvoidBottomInset: true,
bottomNavigationBar: Column(
mainAxisSize: MainAxisSize.min,
children: [
FilledButton(
onPressed: () {},
child: const Text('Button'),
),
],
),
body: SingleChildScrollView(
child: Column(
children: [
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () async {
await showModalBottomSheet(
context: context,
builder: (context) {
return KeyboardWrapper.bottomSheet(
keyboards: [keyboardMomo],
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CustomTextField(
autofocus: true,
keyboardType: keyboardMomo.inputType,
onSubmitted: (value) =>
FocusScope.of(context).unfocus(),
),
],
),
);
},
);
},
child: const Text('BottomSheet'),
),
),
const SizedBox(width: 15),
Expanded(
child: FilledButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return KeyboardWrapper.dialog(
keyboards: [keyboardMomo],
child: Dialog(
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CustomTextField(),
CustomTextField(
autofocus: true,
keyboardType: keyboardMomo.inputType,
onSubmitted: (value) =>
FocusScope.of(context).unfocus(),
),
],
),
),
),
);
},
);
},
child: const Text('Dialog'),
),
),
],
),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(keyboardType: TextInputType.datetime),
CustomTextField(
keyboardType: keyboardMomo.inputType,
onSubmitted: (value) => FocusScope.of(context).unfocus(),
),
CustomTextFormField(keyboardType: keyboardMomo.inputType),
],
),
),
),
),
);
}
}
2. 参考我提供的内容和示例代码回答“Flutter自定义键盘插件ity_customizeded_keyboard的使用”对应的内容
✨ 特性
- 自定义键盘可以是您想象中的任何东西,它们本质上是由您制作的自定义键盘。
- 自定义键盘会根据文本字段是否聚焦或失去焦点自动出现和消失。
onFieldSubmitted
功能工作流畅,并包含像退格和下一个这样的特殊按钮,以实现平滑导航。
🚀 开始
- 注意:为了确保库能够正常运行,请强烈建议使用提供的
CustomTextField
和CustomTextFormField
。这些组件已经优化以与您的自定义键盘无缝集成,从而减少潜在问题并确保高性能。
示例代码
import 'package:flutter/material.dart';
import 'package:ity_customizeded_keyboard/ity_customizeded_keyboard.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final keyboardMomo = CustomKeyboard(
keyEvent: CustomKeyboardEvent.clear(),
borderRadius: BorderRadius.circular(10),
padding: EdgeInsets.all(2),
color: Colors.red,
child: Center(child: Text('AC')),
);
@override
double get height => 230;
@override
String get name => 'YOUR_NAME_KEYBOARD';
@override
Color get backgroundColor => const Color(0xEAEFEFEF);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: KeyboardWrapper(
keyboards: [keyboardMomo],
child: Scaffold(
appBar: AppBar(title: const Text('ity_customizeded_keyboard')),
resizeToAvoidBottomInset: true,
bottomNavigationBar: Column(
mainAxisSize: MainAxisSize.min,
children: [
FilledButton(
onPressed: () {},
child: const Text('Button'),
),
],
),
body: SingleChildScrollView(
child: Column(
children: [
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () async {
await showModalBottomSheet(
context: context,
builder: (context) {
return KeyboardWrapper.bottomSheet(
keyboards: [keyboardMomo],
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CustomTextField(
autofocus: true,
keyboardType: keyboardMomo.inputType,
onSubmitted: (value) =>
FocusScope.of(context).unfocus(),
),
],
),
);
},
);
},
child: const Text('BottomSheet'),
),
),
const SizedBox(width: 15),
Expanded(
child: FilledButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return KeyboardWrapper.dialog(
keyboards: [keyboardMomo],
child: Dialog(
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CustomTextField(),
CustomTextField(
autofocus: true,
keyboardType: keyboardMomo.inputType,
onSubmitted: (value) =>
FocusScope.of(context).unfocus(),
),
],
),
),
),
);
},
);
},
child: const Text('Dialog'),
),
),
],
),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(),
const CustomTextField(keyboardType: TextInputType.datetime),
CustomTextField(
keyboardType: keyboardMomo.inputType,
onSubmitted: (value) => FocusScope.of(context).unfocus(),
),
CustomTextFormField(keyboardType: keyboardMomo.inputType),
],
),
),
),
),
);
}
}
📱 示例
要查看 ity_customizeded_keyboard
的的效果,请访问 GitHub 上的完整示例项目:
https://github.com/ityhoang/ity_customizeded_keyboard/blob/main/example/lib/main.dart
#### 💻 贡献
我们非常欢迎贡献!如果您有新的功能、修复bug或改进的建议,请不要犹豫打开一个议题或提交一个拉取请求。您的输入总是受欢迎的!
#### 🐞 问题和反馈
我们重视您的反馈!请通过以下链接分享您的想法或报告bug:
https://github.com/ityhoang/ity_customizeded_keyboard/issues
更多关于Flutter自定义键盘插件ity_customized_keyboard的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义键盘插件ity_customized_keyboard的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter项目中使用自定义键盘插件 ity_customized_keyboard
的示例代码。这个示例展示了如何集成和使用该插件来创建一个简单的自定义键盘界面。
1. 添加依赖
首先,你需要在你的 pubspec.yaml
文件中添加 ity_customized_keyboard
插件的依赖:
dependencies:
flutter:
sdk: flutter
ity_customized_keyboard: ^最新版本号 # 请替换为实际的最新版本号
然后运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入该插件:
import 'package:ity_customized_keyboard/ity_customized_keyboard.dart';
3. 创建自定义键盘布局
你需要在资源文件夹(如 assets
)中创建一个自定义键盘布局文件,例如 custom_keyboard.json
。以下是一个简单的布局示例:
{
"rows": [
[
{"key": "1", "type": "number"},
{"key": "2", "type": "number"},
{"key": "3", "type": "number"}
],
[
{"key": "4", "type": "number"},
{"key": "5", "type": "number"},
{"key": "6", "type": "number"}
],
[
{"key": "7", "type": "number"},
{"key": "8", "type": "number"},
{"key": "9", "type": "number"}
],
[
{"key": "0", "type": "number"},
{"key": "删除", "type": "function"},
{"key": "完成", "type": "action"}
]
]
}
4. 使用自定义键盘
在你的 Flutter 界面中使用 ItyCustomizedKeyboard
组件。以下是一个完整的示例:
import 'package:flutter/material.dart';
import 'package:ity_customized_keyboard/ity_customized_keyboard.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Custom Keyboard Example'),
),
body: CustomKeyboardExample(),
),
);
}
}
class CustomKeyboardExample extends StatefulWidget {
@override
_CustomKeyboardExampleState createState() => _CustomKeyboardExampleState();
}
class _CustomKeyboardExampleState extends State<CustomKeyboardExample> with SingleTickerProviderStateMixin {
final TextEditingController _controller = TextEditingController();
String _inputText = '';
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
controller: _controller,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter text',
),
onEditingComplete: () {
setState(() {
_inputText = _controller.text;
});
},
),
SizedBox(height: 16),
Expanded(
child: ItyCustomizedKeyboard(
keyboardLayout: 'assets/custom_keyboard.json', // 指定你的键盘布局文件路径
onKeyPressed: (key) {
if (key == '删除') {
_controller.text = _controller.text.substring(0, _controller.text.length - 1);
} else if (key == '完成') {
setState(() {
_inputText = _controller.text;
});
_controller.clear();
} else {
_controller.value = _controller.value.copyWith(
text: _controller.text + key,
selection: TextSelection.fromPosition(
TextPosition(
offset: _controller.text.length + 1,
),
),
);
}
},
),
),
],
),
);
}
}
5. 运行应用
确保你的资源文件(如 custom_keyboard.json
)已经正确放置在 assets
文件夹中,并在 pubspec.yaml
中声明了资源路径:
flutter:
assets:
- assets/custom_keyboard.json
现在你可以运行你的 Flutter 应用,并看到一个带有自定义键盘的输入界面。当你点击键盘上的按钮时,输入框中的内容会相应地更新。
希望这个示例能帮你更好地理解和使用 ity_customized_keyboard
插件。