Last active
September 27, 2021 20:32
-
-
Save lukepighetti/55d367808e994e426fc0c7f7032fab9c to your computer and use it in GitHub Desktop.
Revisions
-
lukepighetti revised this gist
Sep 14, 2020 . 1 changed file with 2 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -42,9 +42,6 @@ class _TextEditingControllerBuilderState extends State<TextEditingControllerBuilder> { TextEditingController controller; @override void initState() { controller = TextEditingController(text: widget.text); @@ -59,10 +56,8 @@ class _TextEditingControllerBuilderState @override void didUpdateWidget(covariant TextEditingControllerBuilder oldWidget) { if (oldWidget.text != widget.text) { controller.value = controller.value.copyWith(text: widget.text); } super.didUpdateWidget(oldWidget); -
lukepighetti revised this gist
Sep 14, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,7 @@ class TextEditingControllerBuilder extends StatefulWidget { /// builder: (context, controller) { /// return TextField( /// controller: controller, /// onChanged: (value) => appState.updateName(value), /// ); /// }, /// ); -
lukepighetti revised this gist
Sep 14, 2020 . 1 changed file with 17 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,9 +9,23 @@ class TextEditingControllerBuilder extends StatefulWidget { /// /// If [text] is updated, the consuming [TextField] will also be updated. /// If the ancestor is rebuilt, the composing state will not be lost like it typically is. /// /// ```dart /// TextEditingControllerBuilder( /// text: appState.name, /// builder: (context, controller) { /// return TextField( /// controller: controller, /// onChanged: (value) => appState.name = value, /// ); /// }, /// ); /// ``` const TextEditingControllerBuilder({ Key key, @required this.text, @required this.builder, }) : super(key: key); /// The text to declaratively update in the text controller final String text; -
lukepighetti revised this gist
Sep 14, 2020 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ import 'package:flutter/widgets.dart'; class TextEditingControllerBuilder extends StatefulWidget { /// Exposes a [TextEditingController] to the child, which allows @@ -40,7 +40,6 @@ class _TextEditingControllerBuilderState @override void dispose() { controller?.dispose(); super.dispose(); } -
lukepighetti created this gist
Sep 14, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ import 'package:flutter/material.dart'; class TextEditingControllerBuilder extends StatefulWidget { /// Exposes a [TextEditingController] to the child, which allows /// us to convert any [TextField] into a declarative version. /// /// Typically used for wiring up many state fields to form inputs /// and making sure everything stays in sync. /// /// If [text] is updated, the consuming [TextField] will also be updated. /// If the ancestor is rebuilt, the composing state will not be lost like it typically is. const TextEditingControllerBuilder( {Key key, @required this.text, @required this.builder}) : super(key: key); /// The text to declaratively update in the text controller final String text; /// The builder which exposes the [TextEditingController] to the child final Widget Function(BuildContext, TextEditingController) builder; @override _TextEditingControllerBuilderState createState() => _TextEditingControllerBuilderState(); } class _TextEditingControllerBuilderState extends State<TextEditingControllerBuilder> { TextEditingController controller; bool get isBeingEdited => controller.value.composing.isValid || controller.value.selection.isValid; @override void initState() { controller = TextEditingController(text: widget.text); super.initState(); } @override void dispose() { controller?.dispose(); super.dispose(); } @override void didUpdateWidget(covariant TextEditingControllerBuilder oldWidget) { final hasTextChanges = oldWidget.text != widget.text; if (hasTextChanges && isBeingEdited == false) { controller.text = widget.text; } super.didUpdateWidget(oldWidget); } @override Widget build(BuildContext context) { return widget.builder(context, controller); } }