Created
September 28, 2020 07:46
-
-
Save panuavakul/a9ccabb461dab5c3523086d4f5d556c3 to your computer and use it in GitHub Desktop.
Example for TextFormField and ValueNotifier
This file contains hidden or 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'; | |
class TestPage extends StatefulWidget { | |
@override | |
_TestPageState createState() => _TestPageState(); | |
} | |
class _TestPageState extends State<TestPage> { | |
final _controller = TextEditingController(); | |
ValueNotifier<String> _textNotifier; | |
@override | |
void initState() { | |
super.initState(); | |
_textNotifier = ValueNotifier<String>(''); | |
_textNotifier.addListener(() { | |
_controller.text = _textNotifier.value; | |
}); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
_textNotifier.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Column( | |
children: [ | |
TextFormField( | |
controller: _controller, | |
), | |
RaisedButton( | |
onPressed: () { | |
_textNotifier.value = 'Clicked'; | |
}, | |
child: const Text('Click Me'), | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment