텍스트 필드에 초기 값을 어떻게 제공합니까?
텍스트 필드에 초기 값을 제공하고 빈 값으로 다시 그려 텍스트를 지우고 싶습니다. 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 TextInput
s 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 TextInput
s.
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
'Development Tip' 카테고리의 다른 글
각 Docker 이미지에 대한 레이어 및 레이어 크기 찾기 (0) | 2020.11.01 |
---|---|
uWSGI의 요점은 무엇입니까? (0) | 2020.11.01 |
SwiftUI는 iOS 12.x 및 이전 버전과 역 호환됩니까? (0) | 2020.11.01 |
크로스 플랫폼 IPC (0) | 2020.11.01 |
ItemsControl DataTemplate에서 캔버스 속성 설정 (0) | 2020.11.01 |