作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
重现步骤
最佳答案
编辑
当输入23
,然后输入惰性0.
时,此行amount = int.parse(val);
执行并停止,然后单击“计算”按钮,因此数量为23
,而value
的runtimeType
为String
023
TextFormField(
keyboardType: TextInputType.number,
validator: (value) {
print("amount $amount");
print(" ${value.runtimeType}");
print("validator amount value $value ");
if (value.isEmpty) {
return "Enter some amount";
} else if (double.parse(value).toInt() <= 0) {
return "Amount should be greater than 0";
}
return null;
},
onChanged: (val){
print("val1 $val");
amount = int.parse(val);
print("amount $amount");
}
您可以在下面复制粘贴运行完整代码
validator
不返回
0.xyz
的原因
onChanged
在
validator
之前执行
validator
通过
int.parse(value)
接收截断的值
amount = int.parse(value);
,验证程序将获得正确的值,但是
if (int.parse(value) <= 0)
将获得
Invalid radix-10 number
您需要更改为
if (double.parse(value).toInt() <= 0)
工作演示
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.purple,
buttonTheme: ButtonThemeData(
buttonColor: Colors.purple.shade400,
),
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: BillSpitApp(),
);
}
}
class BillSpitApp extends StatefulWidget {
BillSpitApp({Key key}) : super(key: key);
@override
_BillSpitAppState createState() => _BillSpitAppState();
}
class _BillSpitAppState extends State<BillSpitApp> {
int amount;
int numOfPersons;
double splitAmount;
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Bill split app"),
),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
splitAmount == null
? "Fill the details and click on calculate to get your bill split"
: "Bill split is ${splitAmount.toStringAsFixed(2)}",
style: TextStyle(
fontSize: 25,
),
textAlign: TextAlign.center,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 15.0),
child: TextFormField(
keyboardType: TextInputType.number,
validator: (value) {
print("validator amount value $value");
if (value.isEmpty) {
return "Enter some amount";
} else if (double.parse(value).toInt() <= 0) {
return "Amount should be greater than 0";
}
return null;
},
onSaved: (value) {
print("onSaved amount value $value");
amount = int.parse(value);
//int.parse(value);
},
decoration: InputDecoration(
labelText: "Amount",
hintText: "1000",
border: OutlineInputBorder(),
),
),
),
TextFormField(
validator: (value) {
print("person value $value");
if (value.isEmpty) {
return "Enter number of persons";
} else if (double.parse(value).toInt() <= 0) {
return "Number should be greater than 0";
}
return null;
},
onSaved: (value) {
print("onsave numOfPersons $value");
numOfPersons = int.parse(value);
},
keyboardType: TextInputType.number,
decoration: InputDecoration(
labelText: "Number of persons",
hintText: "5",
border: OutlineInputBorder(),
),
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: RaisedButton(
child: Text(
"Calculate",
style: TextStyle(color: Colors.white),
),
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
print("calculate $amount");
print("calculate $numOfPersons");
setState(() {
splitAmount = amount / numOfPersons;
});
}
},
),
),
],
),
),
),
);
}
}
关于validation - 在Flutter中无法在验证器中获得适当的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63913449/
这段代码在 Java 中的等价物是什么?我放了一部分,我对 I/O 部分感兴趣: int fd = open(FILE_NAME, O_WRONLY); int ret = 0; if (fd =
我正在尝试将维度为 d1,d2,d3 的张量 M[a1,a2,a3] reshape 为维度为 d2, d1*d3 的矩阵 M[a2,a1*a3]。我试过 M.reshape(d2,d1*d3) 但是
我是一名优秀的程序员,十分优秀!