dart Flutter:在自动完成小部件中提供给fieldViewBuilder的textEditingController有什么用途?

vohkndzv  于 2023-06-27  发布在  Flutter
关注(0)|答案(2)|浏览(93)

Autocomplete小部件中提供给fieldViewBuildertextEditingController有什么用途?它能用来做什么?它可以用来修改或清除TextField的内容吗?

Autocomplete<String>(
        fieldViewBuilder: (
          BuildContext context,
          TextEditingController textEditingController,
          FocusNode focusNode,
          VoidCallback onFieldSubmitted,
        ) {
          return TextField(
            controller: textEditingController,
            focusNode: focusNode,
            onChanged: (String value) {
              print('The text has changed to: $value');
            },
          );
        },
w3nuxt5m

w3nuxt5m1#

Autocomplete小部件中提供给fieldViewBuildertextEditingController属性用于控制文本字段。它可用于修改或清除文本字段的内容。

3bygqnnd

3bygqnnd2#

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Clear Text Field',
      home: Scaffold(
        body: Center(
          child: TextField(
            controller: TextEditingController(text: 'This is the text'),
            onChanged: (String value) {
              print('The text has changed to: $value');
            },
            onEditingComplete: () {
              // Clear the text field's value.
              textEditingController.clear();
            },
          ),
        ),
      ),
    );
  }
}

在本例中,TextField小部件具有一个控制器属性,该属性使用字符串值初始化。TextField小部件的onChanged回调用于将文本字段的当前值打印到控制台。onEditingComplete回调用于在用户按Enter键时清除文本字段的值。

相关问题