flutter - How to get values from a textfield
问题描述
I am having a insane time trying to get 4 values from 4 textfields inside a widget, inside a column.
I am a newbie on flutter, barely had 6 hours of training before trying to do this.
What I need is just this: one way to extract the input of someone using my application that touch the flatbutton, gets inside the "AlertDialog", enters the "form", inputs the 4 values on the 4 lines (foto,artigo,quantidade, precototal) and then clicks the "plus" button on the raisedbutton. I need to have those 4 values but soo far, I only get either empty 4 slot lists or just "[null, , , , ,]
Thz in advance!
I wasted almost 10 hours on the entire project, soo hopefully someone can point me in the right direction...
this is my code:
"main.dart"
class _EcrainicialState extends State<Ecrainicial> {
final _formKey = GlobalKey<FormState>();
String fotografia, nomedoproduto;
int quantidade, precototal;
var listaactual = <List>[["agua.png","Agua","7","14"],["bacalhau.png","Bacalhau","2","42"],["frango.png", "Frango","3","18"],["maca.png","Maca","4"," 4"]];
TextEditingController controlofoto = TextEditingController();
TextEditingController controloartigo = TextEditingController();
TextEditingController controloquantidade = TextEditingController();
TextEditingController controloprecototal = TextEditingController();
String obterfotografia = "";
String obterartigo = "";
String obterquantidade = "";
String obterprecototal = "";
submicaofoto(String valor) {
setState(() => obterfotografia = valor);
return obterfotografia;
}
submicaoartigo(String valor) {
setState(() =>obterartigo = valor);
}
submicaoquantidade(String valor) {
setState(() => obterquantidade = valor);
}
submicaoprecototal(String valor) {
setState(() => obterprecototal = valor);
print(obterprecototal);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Lista de produtos")),
body: ListView(
shrinkWrap: true,
padding: const EdgeInsets.fromLTRB(2.0, 10.0, 2.0, 10.0),
children: <Widget>[
ProductBox (
fotografia: "agua.png",
nomedoproduto: "Agua",
quantidade: 7,
precototal: 14,
),
ProductBox(
fotografia: "bacalhau.png",
nomedoproduto: "Bacalhau",
quantidade: 2,
precototal: 42,
),
ProductBox(
fotografia: "frango.png",
nomedoproduto: "Frango",
quantidade: 3,
precototal: 18
),
ProductBox(
fotografia: "maca.png",
nomedoproduto: "Maca",
quantidade: 4,
precototal: 4,
),
],
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: TextField(
decoration: new InputDecoration(
hintText: 'atum, acucar disponiveis'),
maxLength: 25,
inputFormatters: [WhitelistingTextInputFormatter(RegExp("[a-z.]"))],
controller: controlofoto,
onSubmitted: submicaofoto,
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: TextField(
decoration: new InputDecoration(
hintText: 'atum, acucar disponiveis'),
maxLength: 25,
inputFormatters: [WhitelistingTextInputFormatter(RegExp("[a-z]"))],
controller: controloartigo,
onSubmitted: submicaoartigo,
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: TextField(
decoration: new InputDecoration(
hintText: 'Indique a quantidade, apenas numero .'),
inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
keyboardType: TextInputType.number,
maxLength: 2,
controller: controloquantidade,
onSubmitted: submicaoquantidade,
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: TextField(
decoration: new InputDecoration(
hintText: 'Indique o preco total, apenas numero'),
inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
keyboardType: TextInputType.number,
maxLength: 2,
controller: controloprecototal,
onSubmitted: submicaoquantidade,
),
),
new RaisedButton(
child: Icon(Icons.add),
onPressed: () {
print(submicaofoto);
Artigo.adicionarartigo(obterfotografia, obterartigo, obterquantidade, precototal, listaactual);
}
),
],
),
),
),
);
},
);
},
)
);
}
}
解决方案
U can use textediting controller like that :
TextEditingController controlofoto = TextEditingController();
& put it in TextField Widget like that :
TextField(
decoration: InputDecoration(
hintText: 'atum, acucar disponiveis'),
maxLength: 25,
inputFormatters: [WhitelistingTextInputFormatter(RegExp("[a-z.]"))],
controller: controlofoto,
),
no need to use setstate & onSubmitted
use this to get text enter by user
String someValue = controlofoto.text
推荐阅读
- assembly - 在 x86_64 中,如果条件为假,32 位 cmov 是否会清除最高位?
- php - Laravel - SQLSTATE [23000]:完整性约束违规:1062 键“posts_user_id_unique”的重复条目“1”
- android - 背景/文本大小调整问题
- grafana - 将仪表板保存到 Grafana 中的物理文件夹
- json - 希望在 Json [松弛] 中将按钮与块集成
- java - Docker 容器显示没有端口,可以找到 IP 但需要同时连接到 Java 应用程序
- multithreading - 在 Julia 中使用多线程时,矩阵乘法速度较慢
- django - Django - 基于类的视图问题如何在同一模板中结合 DetailView 和 ListView
- angular - 如何在 HTML 中重叠表格单元格?
- autohotkey - 自动热键 | 正则表达式匹配()。我该怎么做?