2022年8月11日 星期四

Flutter學習-10 form組件

Textfiled可以提供輸入的功能,也可以檢查格式,像是利用keyboardType這個屬性檢查是否符合以下類型 .number .text .phone .datetime .emailAdress .url但基本上它所提供的檢查方法,都較為簡單,若有很多 表單要輸入,而輸入時要判斷裡面輸入的內容是否正確,就要用到Form來統一提供檢查輸入內容是否正確的功能。

Form的方法與屬性如下:

const Form({
Key? key, //Form的key,這個key在form常會用到。
required this.child, //內容
this.onWillPop, //傳出去的連結是否要提供navigator回傳至上一頁。
this.onChanged, //改變時的回調
AutovalidateMode? autovalidateMode, //autovalidateMode: AutovalidateMode.onUserInteraction,
Navigator 的使用可參考:https://yanwei-liu.medium.com/learning-navigation-route-of-flutter-bbc0ee964222

在Form裡面會包著 很多TextFormField()輸入內容。
TextFormField()有以下的屬性,基本上跟TextField是相同的。
可參考:http://acao0429.blogspot.com/search?updated-max=2022-08-07T10:04:00-07:00&max-results=3
TextFormField({
Key? key,
this.controller,
String? initialValue,
FocusNode? focusNode,
InputDecoration? decoration = const InputDecoration(),
TextInputType? keyboardType,
TextCapitalization textCapitalization = TextCapitalization.none,
TextInputAction? textInputAction,
TextStyle? style,
StrutStyle? strutStyle,
TextDirection? textDirection,
TextAlign textAlign = TextAlign.start,
TextAlignVertical? textAlignVertical,
bool autofocus = false,
bool readOnly = false,
ToolbarOptions? toolbarOptions,
bool? showCursor,
String obscuringCharacter = '•',
bool obscureText = false,
bool autocorrect = true,
SmartDashesType? smartDashesType,
SmartQuotesType? smartQuotesType,
bool enableSuggestions = true,
MaxLengthEnforcement? maxLengthEnforcement,
int? maxLines = 1,
int? minLines,
bool expands = false,
int? maxLength,
ValueChanged<String>? onChanged,
GestureTapCallback? onTap,
VoidCallback? onEditingComplete,
ValueChanged<String>? onFieldSubmitted,
FormFieldSetter<String>? onSaved,
FormFieldValidator<String>? validator,
List<TextInputFormatter>? inputFormatters,
bool? enabled,
double cursorWidth = 2.0,
double? cursorHeight,
Radius? cursorRadius,
Color? cursorColor,
Brightness? keyboardAppearance,
EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
bool? enableInteractiveSelection,
TextSelectionControls? selectionControls,
InputCounterWidgetBuilder? buildCounter,
ScrollPhysics? scrollPhysics,
Iterable<String>? autofillHints,
AutovalidateMode? autovalidateMode,
ScrollController? scrollController,
String? restorationId,
bool enableIMEPersonalizedLearning = true,
MouseCursor? mouseCursor,
這裡只介紹validator的用法。
在validator中可以檢查你輸入的時候,是否符合你要求格式的要求,並將它顯示在框的上面。


沒有留言:

張貼留言