Development Tip

텍스트 필드에 초기 값을 어떻게 제공합니까?

yourdevel 2020. 11. 1. 18:44
반응형

텍스트 필드에 초기 값을 어떻게 제공합니까?


텍스트 필드에 초기 값을 제공하고 빈 값으로 다시 그려 텍스트를 지우고 싶습니다. Flutter의 API로이를 수행하는 가장 좋은 방법은 무엇입니까?


(메일 링리스트에서. 나는이 답변을 찾지 못했습니다.)

class _FooState extends State<Foo> {
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TextEditingController(text: 'Initial value');
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[
        new TextField(
          // The TextField is first built, the controller has some initial text,
          // which the TextField shows. As the user edits, the text property of
          // the controller is updated.
          controller: _controller,
        ),
        new RaisedButton(
          onPressed: () {
            // You can also use the controller to manipuate what is shown in the
            // text field. For example, the clear() method removes all the text
            // from the text field.
            _controller.clear();
          },
          child: new Text('CLEAR'),
        ),
      ],
    );
  }
}

당신은 사용할 수있는 TextFormField대신을 TextField하고 사용하는 initialValue속성을. 예를 들면

TextFormField(initialValue: "I am smart")

TextEditingController사용하는 경우 아래와 같이 텍스트를 설정하십시오.

TextEditingController _controller = new TextEditingController();


_controller.text = 'your initial text';

final your_text_name = TextFormField(
      autofocus: false,
      controller: _controller,
      decoration: InputDecoration(
        hintText: 'Hint Value',
      ),
    );

TextEditingController사용하지 않는 경우 아래와 같이 initialValue를 직접 사용할 수 있습니다.

final last_name = TextFormField(
      autofocus: false,
      initialValue: 'your initial text',
      decoration: InputDecoration(
        hintText: 'Last Name',
      ),
    );

더 많은 참조를 위해 TextEditingController


위젯 범위에서 별도의 변수를 정의 할 필요가 없으며 인라인으로 만 수행하십시오.

TextField(
  controller: TextEditingController()..text = 'Your initial value',
  onChanged: (text) => {},
)

If you want to handle multiple TextInputs as asked by @MRT in the comment to the accepted answer, you can create a function that takes an initial value and returns a TextEditingController like this:

initialValue(val) {
  return TextEditingController(text: val);
}

Then, set this function as the controller for the TextInput and supply its initial value there like this:

controller: initialValue('Some initial value here....')

You can repeat this for the other TextInputs.


inside class,

  final usernameController = TextEditingController(text: 'bhanuka');

TextField,

   child: new TextField(
        controller: usernameController,
    ...
)

If you haven't found the answer for this and for those who come here looking for an answer: Checkout the InputDecoration field's hintText:

new TextField(
  decoration: new InputDecoration(
    hintText:"My Text String."
  ),
...

참고URL : https://stackoverflow.com/questions/43214271/how-do-i-supply-an-initial-value-to-a-text-field

반응형