Created
October 7, 2020 04:20
-
-
Save NebulaFox/d4622f01ae17d19122d61e116b7224c5 to your computer and use it in GitHub Desktop.
TextField does not behave correctly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Text Field'), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: Center( | |
child: SizedBox( | |
width: 250.0, | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
TextFieldHandler(), | |
SizedBox(height: 20), | |
ElevatedButton( | |
child: Text("Print Line"), | |
onPressed: () { | |
print("=== === === === ==="); | |
} | |
) | |
] | |
) | |
), | |
), | |
); | |
} | |
} | |
class TextFieldHandler extends StatefulWidget { | |
@override | |
TextFieldHandlerState createState() => TextFieldHandlerState(); | |
} | |
class TextFieldHandlerState extends State<TextFieldHandler> { | |
TextEditingController _controller; | |
int _lastCharacterCount; | |
int _textChangesAfter = 0; | |
bool _shouldIgnoreNextTextChange = false; | |
void _onTextChange() { | |
if (_shouldIgnoreNextTextChange) { | |
_shouldIgnoreNextTextChange = false; | |
return; | |
} | |
final text = _controller.text; | |
final lastCharacterCount = _lastCharacterCount; | |
_lastCharacterCount = text.characters.length; | |
print("$_textChangesAfter onTextChange '$text'"); | |
print("$_textChangesAfter - ${_controller.value}"); | |
if (_controller.text.isNotEmpty) { | |
_updateText(text, _controller.value, lastCharacterCount); | |
} | |
print("$_textChangesAfter next ${_textChangesAfter + 1}"); | |
_textChangesAfter++; | |
} | |
void _updateText(String text, TextEditingValue editingValue, int lastCharacterCount) { | |
print("$_textChangesAfter _update '$text' $lastCharacterCount ${text.characters.length}"); | |
if (lastCharacterCount == 0 && text.characters.length == 1) { | |
print("$_textChangesAfter rest to 0"); | |
_textChangesAfter = 0; | |
final newText = text + ' '; | |
_controller.value = TextEditingValue( | |
text: newText, | |
selection: TextSelection.collapsed(offset: newText.length), | |
composing: TextRange.empty, | |
); | |
} else if (lastCharacterCount == 2 && text.characters.length == 1) { | |
print("$_textChangesAfter rest to 0"); | |
_textChangesAfter = 0; | |
_controller.clear(); | |
_shouldIgnoreNextTextChange = true; | |
} | |
} | |
@override | |
void initState() { | |
super.initState(); | |
_lastCharacterCount = 0; | |
_textChangesAfter = 0; | |
_controller = TextEditingController(); | |
_controller.addListener(_onTextChange); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return TextField( | |
controller: _controller, | |
decoration: const InputDecoration( | |
hintText: '🐉 Hint', | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment