Created
November 11, 2020 18:22
-
-
Save goderbauer/b82efcb154c3fe1814a0d0b60d277def to your computer and use it in GitHub Desktop.
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( | |
home: MyExample(), | |
); | |
} | |
} | |
class MyExample extends StatefulWidget { | |
MyExample({Key key, this.controller}) : super(key: key); | |
final TextEditingController controller; | |
@override | |
_MyExampleState createState() => _MyExampleState(); | |
} | |
class _MyExampleState extends State<MyExample> with SingleTickerProviderStateMixin { | |
@override | |
void initState() { | |
super.initState(); | |
// Add initial listener. | |
widget.controller.addListener(_textChanged); | |
} | |
@override | |
void didUpdateWidget(MyExample oldWidget) { | |
super.didUpdateWidget(oldWidget); | |
// Move listener. | |
if (widget.controller != oldWidget.controller) { | |
oldWidget.controller.removeListener(_textChanged); | |
widget.controller.addListener(_textChanged); | |
} | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
// Remove listener. | |
widget.controller.removeListener(_textChanged); | |
} | |
void _textChanged() { | |
setState(() { | |
// The controller state changed. | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// Doing something useful with it. | |
return Text(widget.controller.text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment