Flutter键盘输入监测插件keyboard_typing的使用
Flutter键盘输入监测插件keyboard_typing的使用
安装插件
flutter pub add keyboard_typing
导入包
import 'package:keyboard_typing/keyboard_typing.dart';
更新
0.0.7 版本
如果你定义了 cursorStyle
参数,你可以在 TextWidget 旁边看到光标(caret)。
CursorStyle 📍
参数 | 必填 | 类型 | 默认值 |
---|---|---|---|
mode | ✔️ | KeyboardTypingCursorMode | none |
color | ❌ | Color? | |
width | ❌ | double | 1.0 |
KeyboardTyping(
text: Text("KeyboardTyping Test Sentence"),
controller: controller,
cursorStyle: CursorStyle(
mode: KeyboardTypingCursorMode.vertical
)
)
使用
如果你不定义一个 KeyboardTypingController
,KeyboardTyping
小部件将自动播放。
创建 KeyboardTyping 小部件 ⌨
参数 | 必填 | 类型 | 默认值 |
---|---|---|---|
text | ✔️ | Text | |
controller | ❌ | KeyboardTypingController? | |
previewTextColor | ❌ | Color? | |
mode | ❌ | KeyboardTypingMode | normal |
intervalDuration | ❌ | Duration? | Duration(milliseconds: 150) |
cursorStyle | ❌ | CursorStyle? | CursorStyle(mode: KeyboardTypingCursorMode.none) |
KeyboardTyping(
text: Text("Something Text"),
)
播放打字动画 🚩
// 自动播放
KeyboardTypingController controller = KeyboardTypingController();
[@override](/user/override)
void initState() {
controller
..addStateEventListener(keyboardStateEventListener)
..play();
}
// 用户控制
[@override](/user/override)
Widget build(BuildContext context) {
...
ElevatedButton(
onPressed: () {
controller.play();
}
)
}
停止打字动画 🚧
如果你想清除文本,可以将 cancel
参数设为 true
。
KeyboardTypingController controller = KeyboardTypingController();
[@override](/user/override)
Widget build(BuildContext context) {
...
ElevatedButton(
onPressed: () {
// 正常停止
controller.stop();
// 强制停止
controller.stop(cancel: true); // 效果是清除文本并重新开始。
}
)
}
重复打字动画 🌀
KeyboardTyping(
text: Text("Something Text"),
mode: KeyboardTypingMode.repeat,
)
监听打字事件状态 🎈
controller.addStateEventListener((KeyboardTypingState state) {
setState(() {
switch (state) {
case KeyboardTypingState.idle:
// TODO: 处理这种情况。
case KeyboardTypingState.active:
// TODO: 处理这种情况。
case KeyboardTypingState.pause:
// TODO: 处理这种情况。
case KeyboardTypingState.stop:
// TODO: 处理这种情况。
case KeyboardTypingState.complete:
// TODO: 处理这种情况。
}
});
},);
// 移除
// controller.removeStateEventListener(...);
预览文本 👥
0.0.6 版本
如果你定义了 previewTextColor
参数,你可以看到预览 TextWidget 的数据。
KeyboardTyping(
text: Text("If you define 'previewTextColor' parameter,\nThen you can see a preview TextWidget :)"),
previewTextColor: Colors.grey.withOpacity(0.5),
)
完整示例 Demo
以下是完整的示例代码:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_logcat/flutter_logcat.dart';
import 'package:keyboard_typing/keyboard_typing.dart';
void main() {
Log.configure(visible: kDebugMode);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Typing Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Typing Demo Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final KeyboardTypingController controller = KeyboardTypingController();
final KeyboardTypingController controllerRepeat = KeyboardTypingController();
final KeyboardTypingController controllerAutoStart = KeyboardTypingController();
late String state;
late String stateRepeat;
bool isCursorVisible = true;
[@override](/user/override)
void initState() {
super.initState();
state = controller.state.name.toUpperCase();
stateRepeat = controllerRepeat.state.name.toUpperCase();
controller.addStateEventListener((state) {
Log.i("StateEventListener.. state:$state");
setState(() {
this.state = state.name.toUpperCase();
});
},);
controllerRepeat..addStateEventListener((state) {
Log.x("StateEventListener.. stateRepeat:$state");
setState(() {
stateRepeat = state.name.toUpperCase();
});
},);
controllerAutoStart.play();
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
KeyboardTyping(
text: Text("Only TextWidget,\n Not define KeyboardTypingController :)"),
// cursorMode: KeyboardTypingCursorMode.none,
),
const SizedBox(height: 36.0,),
Container(
color: Colors.green,
child: KeyboardTyping(
text: Text("If you define 'previewTextColor' parameter,\nThen you can see a preview TextWidget :)",
),
previewTextColor: Colors.grey.withOpacity(0.5),
),
),
const SizedBox(height: 18.0,),
KeyboardTyping(
text: Text(
'You have pressed the Start Button and Typing action! '
'also pressed the Stop Button and stop Typing action!',
style: TextStyle(
color: Colors.green[900],
fontSize: 24.0
),
),
controller: controller,
intervalDuration: const Duration(milliseconds: 50),
),
const SizedBox(height: 18.0,),
Text(state,
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 24.0,),
KeyboardTyping(
text: Text("You have pressed the Repeat Button and Typing action!",
style: TextStyle(
fontSize: 21.0,
color: Colors.blue[900],
),
),
controller: controllerRepeat,
mode: KeyboardTypingMode.repeat,
previewTextColor: Colors.grey.withOpacity(0.7),
),
const SizedBox(height: 18.0,),
Text(stateRepeat,
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 18.0,),
KeyboardTyping(
text: Text("Added AutoStart KeyboardTyping Animation when you add controller parameter"),
controller: controllerAutoStart,
),
],
),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
key: null,
onPressed: () {
setState(() {
isCursorVisible = !isCursorVisible;
controller.cursor(visible: isCursorVisible);
controllerRepeat.cursor(visible: isCursorVisible);
});
},
child: Text("Cursor\n$isCursorVisible",
textAlign: TextAlign.center,
),
),
const SizedBox(width: 16.0,),
FloatingActionButton(
key: null,
onPressed: () {
setState(() {
controller.play();
controllerRepeat.play();
});
},
child: const Text("Play"),
),
],
),
const SizedBox(height: 16.0,),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
key: null,
onPressed: () {
setState(() {
controller.stop();
controllerRepeat.stop();
});
},
child: const Text("Stop"),
),
const SizedBox(width: 16.0,),
FloatingActionButton(
key: null,
onPressed: () {
setState(() {
controller.stop(cancel: true);
controllerRepeat.stop(cancel: true);
});
},
child: const Text("Forced\nStop",
textAlign: TextAlign.center,
),
)
],
),
],
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
更多关于Flutter键盘输入监测插件keyboard_typing的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复