如何在Flutter中手动获取用户的出生日期?
dob_input_field包可帮助您手动获取用户的出生日期。这个包自动验证用户输入的出生日期,即自动验证。许多应用程序需要输入用户的出生日期,然后显式验证数据,这很复杂。但是Flutter中的 dob_input_field 包以一种简单的方式克服了这个复杂的任务。
特性
- 手动取 DOB。
- 自动验证。
- 固定字符长度。
- 日期类型输入键盘。
- 用户首选的装饰品。
安装
将依赖项添加到 pubspec.yaml 文件中。
dob_input_field: ^1.0.0
用法语法
没有标签,也没有验证。
Dart
DOBInputField(
firstDate: DateTime(1900),
lastDate:DateTime.now() ,
autovalidateMode: AutovalidateMode.disabled,
),
Dart
DOBInputField(
firstDate: DateTime(1900),
lastDate:DateTime.now() ,
),
Dart
DOBInputField(
firstDate: DateTime(1900),
lastDate:DateTime.now() ,
showLabel: true,
autovalidateMode: AutovalidateMode.always,
fieldLabelText: "With label",
),
Dart
import 'package:dob_input_field/dob_input_field.dart';
import 'package:flutter/material.dart';
void main() {
runApp(DobInputField());
}
class DobInputField extends StatelessWidget {
const DobInputField({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.teal),
home: Scaffold(
appBar: AppBar(
title: const Text("DOB input field"),
),
body: Container(
padding: const EdgeInsets.symmetric(vertical: 50, horizontal: 20),
child: DOBInputField(
firstDate: DateTime(1900),
lastDate: DateTime.now(),
showLabel: true,
showCursor: true,
autovalidateMode: AutovalidateMode.always,
fieldLabelText: "With label",
),
),
),
);
}
}
没有标签,也没有验证。
Dart
DOBInputField(
firstDate: DateTime(1900),
lastDate:DateTime.now() ,
),
带有标签和自动验证。
Dart
DOBInputField(
firstDate: DateTime(1900),
lastDate:DateTime.now() ,
showLabel: true,
autovalidateMode: AutovalidateMode.always,
fieldLabelText: "With label",
),
Note: firstDate and lastDate properties are required properties.
代码示例
Dart
import 'package:dob_input_field/dob_input_field.dart';
import 'package:flutter/material.dart';
void main() {
runApp(DobInputField());
}
class DobInputField extends StatelessWidget {
const DobInputField({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.teal),
home: Scaffold(
appBar: AppBar(
title: const Text("DOB input field"),
),
body: Container(
padding: const EdgeInsets.symmetric(vertical: 50, horizontal: 20),
child: DOBInputField(
firstDate: DateTime(1900),
lastDate: DateTime.now(),
showLabel: true,
showCursor: true,
autovalidateMode: AutovalidateMode.always,
fieldLabelText: "With label",
),
),
),
);
}
}