這是我要限制的 textformfield 的代碼。用戶應該只能輸入 1-10 的值,但我找不到如何實作
TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Please enter the Overall Rating';
}
return null;
},
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
maxLength: 2,
maxLengthEnforced: true,
controller: overall,
decoration: InputDecoration(
hintText: "Overall Rating Out of /10",
),
),
uj5u.com熱心網友回復:
如果您想檢查給定的字串是否小于 11,您可以在驗證器的幫助下進行。但是當使用驗證器時,您需要執行觸發器或需要發生事件如果您想以這種方式運行代碼,您可以使用此代碼......
使用驗證器的代碼(使用 tigger 甚至)
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Textfi extends StatelessWidget {
Textfi({Key? key}) : super(key: key);
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
child: Column(children: [
const SizedBox(
height: 70,
),
TextFormField(
validator: (value) {
if (value!.isEmpty) {
return 'Please enter the Overall Rating';
} else if (int.parse(value) < 1 || int.parse(value) > 10) {
return 'The rating must be between 1 and 10';
}
return null;
},
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be entered
maxLength: 2,
maxLengthEnforced: true,
decoration: const InputDecoration(
hintText: "Overall Rating Out of /10",
),
),
GestureDetector(
onTap: () {
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Validation done')),
);
}
},
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
height: 30,
width: 80,
color: Colors.blue,
child: const Center(child: Text("Submit")),
),
),
)
]),
),
);
}
}
如果您想實時檢查值
您不能使用驗證器,您需要限制輸入值,唯一的方法是使用 inputFormatters:
在您的情況下,您使用 inputFormatter 作為:
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
只會輸入數字
如果你想輸入一個限制數字,你需要使用Regex
為了改變你的
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
到
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp("^(1[0-0]|[1-9])\$")),
],
這將幫助您僅輸入 1 到 10 之間的數字:-
正則運算式("^(1[0-0]|[1-9])$")
**完整代碼**
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Textfi extends StatelessWidget {
Textfi({Key? key}) : super(key: key);
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
child: Column(children: [
const SizedBox(
height: 70,
),
TextFormField(
validator: (value) {
if (value!.isEmpty) {
return 'Please enter the Overall Rating';
} else if (int.parse(value) < 1 || int.parse(value) > 10) {
return 'The rating must be between 1 and 10';
}
return null;
},
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp("^(1[0-0]|[1-9])\$")),
],
// inputFormatters: <TextInputFormatter>[
// FilteringTextInputFormatter.digitsOnly
// ], // Only numbers can be entered
maxLength: 2,
maxLengthEnforced: true,
decoration: const InputDecoration(
hintText: "Overall Rating Out of /10",
),
),
GestureDetector(
onTap: () {
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Validation done')),
);
}
},
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
height: 30,
width: 80,
color: Colors.blue,
child: const Center(child: Text("Submit")),
),
),
)
]),
),
);
}
}
uj5u.com熱心網友回復:
您可以嘗試使用表單和數字驗證,您需要將字串決議為 int
Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
// Only numbers can be entered
maxLength: 2,
maxLengthEnforced: true,
controller: overall,
decoration: InputDecoration(
hintText: "Overall Rating Out of /10",
),
validator: (text) {
if (text == null || text.isEmpty) {
return 'Text is empty';
}
if (int.parse(text) < 1 || int.parse(text) > 10) {
return 'The rating must be between 1 and 10';
}
return null;
},
),
TextButton(
onPressed: () {
if (_formKey.currentState.validate()) {
// TODO submit
}
},
child: Text('Submit'),
)
],
),
)
uj5u.com熱心網友回復:
在inputFormatters里面:你只需把下面的快遞,這應該作業......
// 正則運算式只接受 1-10 的數字
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^[1-9]$|^10$'),
),],
uj5u.com熱心網友回復:
您可以按如下方式更新您的驗證器功能
validator: (value) {
if (value.isEmpty) {
return 'Please enter the Overall Rating';
}
if(int.parse(value) < 1 || int.parse(value) > 10) {
return 'The rating must be between 1 and 10';
}
return null;
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/365058.html
