Skip to content

Instantly share code, notes, and snippets.

@kitoko552
Created December 7, 2018 01:07
Show Gist options
  • Save kitoko552/7202df23e3bb1e8af0b82c6499a90443 to your computer and use it in GitHub Desktop.
Save kitoko552/7202df23e3bb1e8af0b82c6499a90443 to your computer and use it in GitHub Desktop.
WA
class _MyHomePageState extends State<MyHomePage> {
TextEditingController controllerA = TextEditingController();
TextEditingController controllerB = TextEditingController();
FocusNode nodeA = FocusNode();
FocusNode nodeB = FocusNode();
@override
void initState() {
super.initState();
nodeA.addListener(() {
if (!nodeA.hasFocus) {
// focusが外れる時にテキストを確定状態にする
controllerA.value = controllerA.value.copyWith(
composing: TextRange(start: 0, end: 0),
);
}
});
nodeB.addListener(() {
if (!nodeB.hasFocus) {
    // focusが外れる時にテキストを確定状態にする
controllerB.value = controllerB.value.copyWith(
composing: TextRange(start: 0, end: 0),
);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Debug')),
body: Center(
child: Column(
children: [
_buildComposer(controllerA, nodeA),
_buildComposer(controllerB, nodeB),
],
),
),
);
}
Widget _buildComposer(TextEditingController controller, FocusNode node) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 88.0),
child: TextField(
controller: controller,
focusNode: node,
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
keyboardType: TextInputType.multiline,
maxLines: 1,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment