Zeffry Reynando
Zeffry Reynando

Reputation: 3899

Flutter : Parsing String to TextField/ TextFormField

I'm create PinCode , basically when I press the button i want parsing the value to TextField/TextFormField.

Example, When i press button "1" return value 1, Then if i press button "2" return value 2.

I'm success return the value to Text Widget, But i can't parsing the value to TextField/TextFormField. Thank's.PinCodeImage

It's My Code :

class PinCodeScreen extends StatefulWidget {
  static const routeNamed = "/pin-code";
  @override
  _PinCodeScreenState createState() => _PinCodeScreenState();
}

class _PinCodeScreenState extends State<PinCodeScreen> {
  TextEditingController _pinCodeController;
  String pinCodeText = "";
  @override
  void initState() {
    super.initState();
     _pinCodeController = TextEditingController(text: pinCodeText);
  }

   @override
   void dispose() {
     _pinCodeController.dispose();
     super.dispose();

  }

  @override
  Widget build(BuildContext context) {
    double mqHeight = MediaQuery.of(context).size.height;
    double mqWidth = MediaQuery.of(context).size.width;
    final textTheme = Theme.of(context).textTheme;
    return Scaffold(
      backgroundColor: Theme.of(context).primaryColor,
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Flexible(
              flex: 2,
              child: HeaderPinCode(
                alignment: Alignment.center,
                title: pinCodeText,
                textStyle: textTheme.title.copyWith(
                  letterSpacing: 4,
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Flexible(
              flex: 4,
              child: TextFormField(
                controller: _pinCodeController,
                maxLength: 4,
                onChanged: (resultPin) {
                  setState(() {
                    pinCodeText = resultPin;
                  });
                },
              ),
            ),
            Flexible(
              flex: 14,
              child: Container(
                height: double.infinity,
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey, width: .5),
                  borderRadius: BorderRadius.circular(30),
                ),
                child: Wrap(
                  alignment: WrapAlignment.spaceEvenly,
                  spacing: 4,
                  children: <Widget>[
                    ButtonPinCode(
                      numberText: "1",
                      mediaQueryHeight: mqHeight,
                      mediaQueryWidth: mqWidth,
                      textStyle:
                          textTheme.display2.copyWith(color: Colors.black),
                      onPressed: () {
                        setState(() {
                          pinCodeText += 1.toString();
                        });
                      },
                    ),
                    ButtonPinCode(
                      numberText: "2",
                      mediaQueryHeight: mqHeight,
                      mediaQueryWidth: mqWidth,
                      textStyle:
                          textTheme.display2.copyWith(color: Colors.black),
                      onPressed: () {
                        setState(() {
                          pinCodeText += 2.toString();
                        });
                      },
                    ),
                    ButtonPinCode(
                      numberText: "3",
                      mediaQueryHeight: mqHeight,
                      mediaQueryWidth: mqWidth,
                      textStyle:
                          textTheme.display2.copyWith(color: Colors.black),
                      onPressed: () {
                        setState(() {
                          pinCodeText += 3.toString();
                        });
                      },
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Upvotes: 1

Views: 1271

Answers (1)

Arun R. Prajapati
Arun R. Prajapati

Reputation: 2802

the answer is simple

TextEditingController _pinCodeController = new  TextEditingController();
_pinCodeController.text=pinCodeText;
setState((){});

And the Best Practice is to user TextEditingController Directly like

_pinCodeController.text += 1.toString();
 setState((){});

Upvotes: 2

Related Questions