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

后端 未结 10 1698
忘掉有多难
忘掉有多难 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: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(() {});
          },
        ),
      );
    }
    

提交回复
热议问题