How do you change the value inside of a textfield flutter?

后端 未结 10 1665
忘掉有多难
忘掉有多难 2020-12-06 08:49

I have a TextEditingController where if a user clicks a button it fills in with information. I can\'t seem to figure out how to change the text inside of a

相关标签:
10条回答
  • 2020-12-06 09:24

    The problem with just setting

    _controller.text = "New value";
    

    is that the cursor will be repositioned to the beginning (in material's TextField). Using

    _controller.text = "Hello";
    _controller.selection = TextSelection.fromPosition(
        TextPosition(offset: _controller.text.length),
    );
    setState(() {});
    

    is not efficient since it rebuilds the widget more than it's necessary (when setting the text property and when calling setState).

    --

    I believe the best way is to combine everything into one simple command:

    final _newValue = "New value";
    _controller.value = TextEditingValue(
          text: _newValue,
          selection: TextSelection.fromPosition(
            TextPosition(offset: _newValue.length),
          ),
        );
    

    It works properly for both Material and Cupertino Textfields.

    0 讨论(0)
  • 2020-12-06 09:24

    Here is a full example where the parent widget controls the children widget. The parent widget updates the children widgets (Text and TextField) with a counter.

    To update the Text widget, all you do is pass in the String parameter. To update the TextField widget, you need to pass in a controller, and set the text in the controller.

    main.dart:

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Demo',
          home: Home(),
        );
      }
    }
    
    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('Update Text and TextField demo'),
            ),
            body: ParentWidget());
      }
    }
    
    class ParentWidget extends StatefulWidget {
      @override
      _ParentWidgetState createState() => _ParentWidgetState();
    }
    
    class _ParentWidgetState extends State<ParentWidget> {
      int _counter = 0;
      String _text = 'no taps yet';
      var _controller = TextEditingController(text: 'initial value');
    
      void _handleTap() {
        setState(() {
          _counter = _counter + 1;
          _text = 'number of taps: ' + _counter.toString();
          _controller.text  = 'number of taps: ' + _counter.toString();
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Column(children: <Widget>[
            RaisedButton(
              onPressed: _handleTap,
              child: const Text('Tap me', style: TextStyle(fontSize: 20)),
            ),
            Text('$_text'),
            TextField(controller: _controller,),
          ]),
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-06 09:26
    _mytexteditingcontroller.value = new TextEditingController.fromValue(new TextEditingValue(text: "My String")).value;
    

    This seems to work if anyone has a better way please feel free to let me know.

    0 讨论(0)
  • 2020-12-06 09:28

    Using this solution, you will also be able to put the cursor at the end of newly text.

    final TextEditingController _controller = TextEditingController();
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(child: TextField(controller: _controller)),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            _controller.text = "Hello";
    
            // this changes cursor position
            _controller.selection = TextSelection.fromPosition(TextPosition(offset: _controller.text.length));
            setState(() {});
          },
        ),
      );
    }
    
    0 讨论(0)
  • 2020-12-06 09:28

    simply change the text or value property of controller. if you do not edit selection property cursor goes to first of the new text.

    onPress: () {
             _controller.value=TextEditingValue(text: "sample text",selection: TextSelection.fromPosition(TextPosition(offset: sellPriceController.text.length)));                 
                 }
    

    or in case you change the .text property:

     onPress: () {
             _controller.text="sample text";
             _controller.selection = TextSelection.fromPosition(TextPosition(offset:_controller.text.length));          
                  }
    

    in cases that do not matter to you just don't change the selection property

    0 讨论(0)
  • 2020-12-06 09:29

    step 1) Declare TextEditingController.

    step 2) supply controller to the TextField.

    step 3) user controller's text property to change the value of the textField.

    follow this official solution to the problem

    0 讨论(0)
提交回复
热议问题