Flutter车辆专用键盘插件flutter_vehicle_keyboard的使用
Flutter车辆专用键盘插件flutter_vehicle_keyboard的使用
flutter_vehicle_keyboard
Flutter Vehicle Keyboard
仿交管12123车牌键盘
- 支持自动切换省份简称和字母键盘
- 支持输入完成自动关闭键盘
- 支持设置车牌长度
- 过滤字母“I”
开始使用
添加依赖
你可以使用以下命令将 flutter_vehicle_keyboard
的最新稳定版依赖添加至你的项目:
dependencies:
flutter_vehicle_keyboard: ^replace-with-latest-version
使用示例
import 'package:flutter/material.dart';
import 'package:flutter_vehicle_keyboard/flutter_vehicle_keyboard.dart';
class MyHomePage extends StatefulWidget {
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String vehicleNumber = "";
bool showKeyboard = true;
TextEditingController controller = new TextEditingController();
FocusNode focusNode = FocusNode();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('仿12123车牌输入键盘'),
),
body: new Center(
child: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.all(10),
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text('车牌号码:'),
Expanded(
child: VehicleField(
focusNode: focusNode,
autoSwitchLetterKeyBoard: true,
controller: controller,
textAlign: TextAlign.end,
),
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
child: Text("隐藏/显示"),
onPressed: () {
showKeyboard = !showKeyboard;
if (showKeyboard) {
focusNode.requestFocus();
} else {
focusNode.unfocus();
}
},
),
SizedBox(
width: 10,
),
RaisedButton(
child: Text("获取车牌号"),
onPressed: () {
vehicleNumber = controller.text;
setState(() {});
},
),
],
),
Text(vehicleNumber),
],
)),
);
}
}
完整示例Demo
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_vehicle_keyboard/flutter_vehicle_keyboard.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
/// 键盘的整体回调,根据不同的按钮事件来进行相应的逻辑实现
// void _onKeyDown(KeyDownEvent data) {
// debugPrint("keyEvent:" + data.key);
//
// if (data.isClose() || data.isCommit()) {
// setState(() {
// showKeyboard = false;
// });
// }
// }
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(home: MyHomePage());
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String vehicleNumber = "";
bool showKeyboard = true;
bool isEdit = true;
TextEditingController controller = new TextEditingController(text: "粤A12345");
FocusNode focusNode = FocusNode();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('仿12123车牌输入键盘'),
),
body: new Center(
child: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.all(10),
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text('车牌号码:'),
Expanded(
child: VehicleField(
focusNode: focusNode,
autoSwitchLetterKeyBoard: true,
controller: controller,
enabled: isEdit,
textAlign: TextAlign.end,
onChanged: (value) => {print("vihicle number onChanged: $value")},
),
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
MaterialButton(
child: Text("隐藏/显示"),
onPressed: () {
showKeyboard = !showKeyboard;
if (showKeyboard) {
focusNode.requestFocus();
} else {
focusNode.unfocus();
}
},
),
SizedBox(
width: 10,
),
MaterialButton(
child: Text("获取车牌号"),
onPressed: () {
vehicleNumber = controller.text;
setState(() {});
},
),
SizedBox(
width: 10,
),
Text("是否可编辑"),
Switch(
value: isEdit,
onChanged: (value) {
isEdit = value;
setState(() {});
},
),
// MaterialButton(
// child: Text("是否可编辑"),
// onPressed: () {
// isEdit = !isEdit;
// setState(() {});
// },
// ),
],
),
Text(vehicleNumber),
],
)),
);
}
}
更多关于Flutter车辆专用键盘插件flutter_vehicle_keyboard的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter车辆专用键盘插件flutter_vehicle_keyboard的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_vehicle_keyboard
是一个专为 Flutter 应用设计的车辆专用键盘插件,通常用于输入车牌号、车辆识别码(VIN)等车辆相关信息。这个插件提供了一个定制化的键盘布局,方便用户快速输入车辆相关的字符。
1. 安装插件
首先,你需要在 pubspec.yaml
文件中添加 flutter_vehicle_keyboard
插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_vehicle_keyboard: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
2. 基本使用
import 'package:flutter/material.dart';
import 'package:flutter_vehicle_keyboard/flutter_vehicle_keyboard.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: VehicleKeyboardDemo(),
);
}
}
class VehicleKeyboardDemo extends StatefulWidget {
@override
_VehicleKeyboardDemoState createState() => _VehicleKeyboardDemoState();
}
class _VehicleKeyboardDemoState extends State<VehicleKeyboardDemo> {
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('车辆专用键盘示例'),
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: _controller,
decoration: InputDecoration(
labelText: '输入车牌号',
border: OutlineInputBorder(),
),
readOnly: true, // 使TextField不可直接编辑
onTap: () {
// 显示车辆专用键盘
_showVehicleKeyboard(context);
},
),
),
],
),
);
}
void _showVehicleKeyboard(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return VehicleKeyboard(
onTextInput: (text) {
_controller.text = text;
},
onBackspace: () {
if (_controller.text.isNotEmpty) {
_controller.text = _controller.text.substring(0, _controller.text.length - 1);
}
},
);
},
);
}
}
3. 自定义键盘
flutter_vehicle_keyboard
提供了多种自定义选项,例如:
- 键盘类型:你可以选择不同的键盘类型,例如车牌号键盘、VIN键盘等。
- 键盘布局:你可以自定义键盘的布局,添加或删除特定的按键。
- 主题:你可以自定义键盘的颜色、字体等。
VehicleKeyboard(
keyboardType: VehicleKeyboardType.plateNumber, // 车牌号键盘
onTextInput: (text) {
_controller.text = text;
},
onBackspace: () {
if (_controller.text.isNotEmpty) {
_controller.text = _controller.text.substring(0, _controller.text.length - 1);
}
},
theme: VehicleKeyboardThemeData(
keyColor: Colors.blue,
textColor: Colors.white,
),
);