Justin Waweru
Justin Waweru

Reputation: 33

Is there a way to autofill a textformfield when a button is pressed in flutter

The button generates a unique timestamp that I would like to be auto-filled in a form-field in a form within the page. This field value is pushed to the database.

 new RaisedButton(
                    elevation: 5.0,
                    shape: new RoundedRectangleBorder(
                        borderRadius: new BorderRadius.circular(10.0)),
                    onPressed: () {
                      showDialog(
                        context: context,
                        barrierDismissible: false,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(32.0),
                            ),
                            title: SelectableText(
                              DateTime.now().millisecondsSinceEpoch.toString(),
                              ),
                            actions: <Widget>[
                              FlatButton(
                                child: Text('Done'),
                                textColor: Theme.Colors.loginGradientStart,
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                              )
                            ],
                          );
                        },
                      );
                    },
                    child: const Text(
                      'GENERATE\nTRACKING-No',
                      style: TextStyle(
                          fontSize: 20,
                          fontFamily: 'Spectral',
                          fontWeight: FontWeight.bold,
                          fontStyle: FontStyle.normal),
                      textAlign: TextAlign.center,
                    ),
                  ),

Upvotes: 1

Views: 3497

Answers (2)

Rutvik Moradiya
Rutvik Moradiya

Reputation: 88

intialize controller and callbackfunction into init() function when the execution start.

 final _controller = TextEditingController();

Call the function onPressedMethod() and pass the string of time by putting the following code on

onTap:(){
..
onPressedMethod(DateTime.now().millisecondsSinceEpoch.toString())
}  

After Calling onPressedMethod the value will be stored into TextEditingController and It can be usable into TextFormField.

void onPressedMethod(String timeValue){
      _controller.text = timeValue;
    }

TextFormField(
        controller: _controller.text,)

After Submitting the final Submit value can be deleted by dispose method.

 void dispose() {
      _controller.dispose();
      super.dispose();
    }

Upvotes: 0

Kahou
Kahou

Reputation: 3488

Set the TextField text.

void onButtonPressed(){
  _controller.text = /* new value*/;
}
final _controller = TextEditingController();

// ...
TextFormField(controller: _controller)
// ...

void dispose() {
  _controller.dispose();
  super.dispose();
}

Upvotes: 3

Related Questions