Flutter文本表单插件simple_text_form_field的使用
Flutter文本表单插件simple_text_form_field的使用
simple_text_form_field 是一个用于创建简单文本表单字段的插件,适用于iOS、Android、Windows和Web平台。此包包含多个自定义小部件,例如:
<code>SimpleTextFormField</code>:用于创建简单的文本输入框。<code>SimpleTextFormFieldDate</code>:用于创建日期选择器或时间选择器。<code>SimpleTextFormFieldDropDown</code>:用于创建通用的下拉菜单。
使用示例
要使用此插件,只需在 pubspec.yaml 文件中添加依赖项 <code>simple_text_form_field</code> 即可。
示例代码
以下是一个完整的示例代码,展示如何使用 simple_text_form_field 插件来创建文本输入框、日期选择器、时间选择器以及下拉菜单,并获取其值。
import 'package:flutter/material.dart';
import 'package:simple_text_form_field/simple_text_form_field.dart';
import 'package:simple_text_form_field/simple_text_form_field_date.dart';
import 'package:simple_text_form_field/simple_text_form_field_dropdown.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// 定义控制器
SimpleTextFormFieldController controller = SimpleTextFormFieldController();
SimpleTextFormFieldDateController controllerDate =
SimpleTextFormFieldDateController();
SimpleTextFormFieldDateController controllerTime =
SimpleTextFormFieldDateController();
SimpleTextFormFieldDropDownController<ItemModel> controllerDropdown =
SimpleTextFormFieldDropDownController();
String? text = "";
String? date = "";
String? time = "";
String? objectSelected;
List<ItemModel> lists = [];
[@override](/user/override)
void initState() {
_populateList();
super.initState();
}
void _populateList() {
setState(() {
lists.add(ItemModel(mainTitle: "First Title", dateTime: DateTime.now(), desc: "This is the first description of title"));
lists.add(ItemModel(mainTitle: "Second Title", dateTime: DateTime.now(), desc: "This is the second description of title"));
lists.add(ItemModel(mainTitle: "Third Title", dateTime: DateTime.now(), desc: "This is the third description of title"));
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Simple Usage"),
),
body: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: [
// 文本输入框
SimpleTextFormField(
controller: controller,
isRequired: true,
errorTextSize: 8,
maxLength: 12,
label: "Name",
placeHolder: "Input your name",
),
const Padding(padding: EdgeInsets.all(10)),
// 日期选择器
SimpleTextFormFieldDate(
isRequired: true,
fillColor: Colors.transparent,
controller: controllerDate,
firstDate: DateTime.now(),
label: "Your Date",
),
const Padding(padding: EdgeInsets.all(10)),
// 时间选择器
SimpleTextFormFieldDate(
isRequired: true,
fillColor: Colors.transparent,
controller: controllerTime,
type: InputDatetimeType.time,
label: "Your Time",
),
const Padding(padding: EdgeInsets.all(10)),
// 下拉菜单
SimpleTextFormFieldDropDown<ItemModel>(
controller: controllerDropdown,
required: true,
label: "Select one",
valueItem: (e) => e.mainTitle!,
listItem: lists,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
// 验证并获取值
if (controller.isValid &&
controllerDate.isValid &&
controllerTime.isValid &&
controllerDropdown.isValid) {
setState(() {
text = controller.value;
date = SimpleConstants.dateToString(controllerDate.value, format: "dd/MM/yyyy");
time = controllerTime.value.format(context);
objectSelected = controllerDropdown.value?.mainTitle;
});
}
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
"Save",
style: TextStyle(color: Colors.blue),
),
),
),
),
Padding(
padding: const EdgeInsets.all(10),
child: Text(
"Text : $text, \nDate : $date, \nTime : $time, \nDropdown : ${objectSelected ?? ""}",
),
)
],
),
),
);
}
}
// 自定义数据模型
class ItemModel {
final String mainTitle;
final DateTime dateTime;
final String desc;
ItemModel({required this.mainTitle, required this.dateTime, required this.desc});
}
更多关于Flutter文本表单插件simple_text_form_field的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter文本表单插件simple_text_form_field的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
simple_text_form_field 是一个用于 Flutter 的简单文本表单字段插件,它可以帮助你快速创建一个带有基本验证功能的文本输入框。以下是使用 simple_text_form_field 插件的步骤和示例代码。
1. 添加依赖
首先,你需要在 pubspec.yaml 文件中添加 simple_text_form_field 插件的依赖:
dependencies:
flutter:
sdk: flutter
simple_text_form_field: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get 来获取依赖。
2. 导入包
在你的 Dart 文件中导入 simple_text_form_field 包:
import 'package:simple_text_form_field/simple_text_form_field.dart';
3. 使用 SimpleTextFormField
你可以在你的表单中使用 SimpleTextFormField。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:simple_text_form_field/simple_text_form_field.dart';
class MyForm extends StatefulWidget {
@override
_MyFormState createState() => _MyFormState();
}
class _MyFormState extends State<MyForm> {
final _formKey = GlobalKey<FormState>();
String _name = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Simple Text Form Field Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
SimpleTextFormField(
labelText: 'Name',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your name';
}
return null;
},
onSaved: (value) {
_name = value!;
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
// 处理表单数据
print('Name: $_name');
}
},
child: Text('Submit'),
),
],
),
),
),
);
}
}
void main() {
runApp(MaterialApp(
home: MyForm(),
));
}
4. 解释代码
SimpleTextFormField: 这是一个自定义的文本输入字段,它包含一个标签和一个验证器。labelText: 输入框的标签文本。validator: 验证输入字段的函数。如果输入无效,返回一个错误消息;如果输入有效,返回null。onSaved: 当表单保存时调用的函数,用于保存输入的值。_formKey: 用于标识和管理表单状态的全局键。
5. 运行应用
运行你的 Flutter 应用,你将看到一个带有文本输入框和提交按钮的表单。如果输入框为空并点击提交按钮,将会显示一个错误提示。
6. 自定义
你可以根据需要自定义 SimpleTextFormField 的其他属性,例如 hintText、obscureText、keyboardType 等。
SimpleTextFormField(
labelText: 'Password',
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
onSaved: (value) {
// 保存密码
},
),

