Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

flutter - TextFormField cuts last character input

I'm having a weird issue with a TextFormField, it cuts off the last input. If I type something like hello, it's gonna cut off the o and save hell. if I give it a space like hello , it's gonna save hello. Has anyone been through the same issue and know how to fix it?

here is my code:

 String messageText;

 _ChatScreenState() {
 _formKey = GlobalKey<FormState>();
 _scrollController = ScrollController();
 messageText = "";
 }

  Widget messageTextField() {
    return SizedBox(
      width: deviceWidth * 0.55,
      child: TextFormField(
      style: TextStyle(color: Colors.white),
      validator: (text) {
        if (text.isEmpty) {
          return 'Digite sua mensagem';
      }
        return null;
      },
      onChanged: (text) {
        _formKey.currentState.save();
      },
      onSaved: (text) {
        setState(() {
          messageText = text;
        });
      },
      cursorColor: Colors.white,
      decoration: InputDecoration(
        border: InputBorder.none,
        hintText: 'Digite uma mensagem',
        hintStyle: TextStyle(color: Colors.white.withAlpha(100))),
      autocorrect: false,
    ),
  );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

I do not think save() is intended to be used with onChanged.

If you place a button somewhere and perform _formKey.currentState.save(); within onPressed of the button, you should see the entire text value being saved.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share

755k questions

754k answers

5 comments

53.3k users

...