首页 > 解决方案 > 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);

                            }
                        ),
                      ],
                    ),
                  ),
                  ),
                );
              },
            );
          },
        )
    );
  }
}

标签: flutter

解决方案


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

推荐阅读