flutter - 如何根据对 textformfield 的更改来更改按钮的文本
问题描述
TextEditingController _popUpDeleteSoldText = new TextEditingController(text: "Delete");
return showDialog(context: context, builder: (context) {
return AlertDialog(
title: Text(_popUpDeleteSoldText.text + " " + doc['name'] + "?"),
content: Container(
height: 145,
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 0.0),
child: Align(alignment: Alignment.centerLeft, child: Text('Did you sold the property?', style: TextStyle(color: myColors.blue),)),
),
MyTextFormField(
soldController,
'For how much?',
false,
keyboardType: TextInputType.number,
digitsOnly: true,
onChanged: (text) {
if (soldController.text != "0.00") {
print('text field: $text');
setState(() {
_popUpDeleteSoldText = new TextEditingController(text: "Sell");
});
} else {
setState(() {
_popUpDeleteSoldText = new TextEditingController(text: "Delete");
});
}
},
),
如您所见,我正在根据正在更改的 textformfield 更改 _popUpDeleteSoldText 控制器(mytextformfield 只是我自己的基于原始字段的字段,以标准化整个应用程序的设计)。该控制器是我在弹出按钮上用来说“出售”或“删除”的控制器,因为我想根据字段上的值更改按钮文本我知道在我使用打印测试时触发了 onchanged,但是,按钮总是说“删除”(初始初始化)并且不会更新为“卖出”
解决方案
这是另一个解决方案,除了将按钮文本保持为状态外,我还保留了属性值。我还添加了一个 TextInputFormatter 以便只接受正数。
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: Scaffold(
body: MyWidget(),
),
),
);
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
int propertyValue = 0;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('Did you sell the property???'),
IntrinsicWidth(
child: TextFormField(
initialValue: propertyValue.toString(),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly,
PositiveNumberFormatter(),
],
textAlign: TextAlign.center,
onChanged: (value) =>
setState(() => propertyValue = int.parse(value)),
),
),
SizedBox(height: 16.0),
propertyValue > 0
? ElevatedButton.icon(
icon: Icon(Icons.attach_money),
label: Text('Sell'),
onPressed: () => print('SELL'),
)
: ElevatedButton.icon(
icon: Icon(Icons.delete),
label: Text('Delete'),
onPressed: () => print('DELETE'),
),
],
),
);
}
}
class PositiveNumberFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: max(0, double.tryParse(newValue.text) ?? 0).toString(),
selection: newValue.text.length == 0
? TextSelection.collapsed(offset: 1)
: newValue.selection,
);
}
}
推荐阅读
- reactjs - 无法在 TypeScript 中自定义 Material UI 主题的调色板类型
- sql-server - 使用 OPENROWSET 调用 T-SQL 过程
- sql - 计算运行总计逻辑,但第一个单元格出现空白
- javascript - 为什么在 pug 模板中迭代时出现“无法读取未定义的属性‘长度’”错误?
- html - 从另一个页面访问页面时CSS未加载
- c++ - 在 Ubuntu 16.04 上安装 Cilk 时出现问题
- java - 我应该将 JAVA_HOME 用于 OpenJDK 的 JDK 还是 OpenJDK 的 JRE?
- python-3.x - 为什么 continue_game 在我的 Python 代码中不是 False
- python - 将编码字符串转换为普通的可打印字符
- javascript - 使用reduce函数将数组汇总为对象