Created
April 8, 2020 00:16
-
-
Save Huxpro/b239af887e4c297730bb32dbb3b94f4c to your computer and use it in GitHub Desktop.
A Flutter TextField that maintain selection state like TextInput in React Native
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'; | |
/// A TextField that maintain selection state like React Native | |
class TextInput extends StatefulWidget { | |
TextInput({@required this.decoration, @required this.text, @required this.onChanged}); | |
final String text; | |
final InputDecoration decoration; | |
final Function onChanged; | |
@override | |
_TextInputState createState() => _TextInputState(); | |
} | |
class _TextInputState extends State<TextInput> { | |
final _ctrl = TextEditingController(); | |
@override | |
void initState() { | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
_ctrl.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
_ctrl.value = _ctrl.value.copyWith( | |
text: widget.text, | |
); | |
return TextField( | |
controller: _ctrl, | |
decoration: widget.decoration, | |
onChanged: widget.onChanged, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment