Skip to content

Instantly share code, notes, and snippets.

@Huxpro
Created April 8, 2020 00:16
Show Gist options
  • Save Huxpro/b239af887e4c297730bb32dbb3b94f4c to your computer and use it in GitHub Desktop.
Save Huxpro/b239af887e4c297730bb32dbb3b94f4c to your computer and use it in GitHub Desktop.
A Flutter TextField that maintain selection state like TextInput in React Native
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