1

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();
                        });
                      },
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
6
  • Do you dispose the _pinCodeController object? Commented Dec 14, 2019 at 15:49
  • No, I'm not dispose it. Commented Dec 14, 2019 at 15:50
  • Please provide more code so we can help you in a better way. But for sure you have to override dispose() in the State<T> class and call _pinCodeController.dispose(); before super.dispose() Commented Dec 14, 2019 at 15:53
  • remove initState and initialize the calss variable immediateli with TextEditingController _pinCodeController = TextEditingController(text: pinCodeText); . Rebuild and let me know Commented Dec 14, 2019 at 16:02
  • give me error Only static members can be accessed in initializers. Commented Dec 14, 2019 at 16:03

1 Answer 1

2

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((){});
Sign up to request clarification or add additional context in comments.

1 Comment

It's work! i'm using second option. but i'm dont know implement option 1. can you tell me where i can place the code option 1 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.