简体   繁体   English

我如何在发短信 flutter 时使用向上和向下箭头从文本字段建议 select

[英]how can i select suggestion from textfield using up & down arrow while texting flutter

how can I select text from suggestion while writing in textfield with up & down arrow in flutter.如何在 flutter 中使用向上和向下箭头在文本字段中写入时从建议中获得 select 文本。 like shown in image如图所示

图 1图 2

Use Autocomplete class使用自动完成 class

import 'package:flutter/material.dart';

void main() => runApp(const AutocompleteExampleApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Autocomplete Basic User'),
        ),
        body: const Center(
          child: AutocompleteBasicUserExample(),
        ),
      ),
    );
  }
}

@immutable
class User {
  const User({
    required this.email,
    required this.name,
  });

  final String email;
  final String name;

  @override
  String toString() {
    return '$name, $email';
  }

  @override
  bool operator ==(Object other) {
    if (other.runtimeType != runtimeType) {
      return false;
    }
    return other is User && other.name == name && other.email == email;
  }

  @override
  int get hashCode => Object.hash(email, name);
}

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

  static const List<User> _userOptions = <User>[
    User(name: 'Alice', email: 'alice@example.com'),
    User(name: 'Bob', email: 'bob@example.com'),
    User(name: 'Charlie', email: 'charlie123@gmail.com'),
  ];

  static String _displayStringForOption(User option) => option.name;

  @override
  Widget build(BuildContext context) {
    return Autocomplete<User>(
      displayStringForOption: _displayStringForOption,
      optionsBuilder: (TextEditingValue textEditingValue) {
        if (textEditingValue.text == '') {
          return const Iterable<User>.empty();
        }
        return _userOptions.where((User option) {
          return option
              .toString()
              .contains(textEditingValue.text.toLowerCase());
        });
      },
      onSelected: (User selection) {
        debugPrint('You just selected ${_displayStringForOption(selection)}');
      },
    );
  }
}

code Taken From: https://api.flutter.dev/flutter/material/Autocomplete-class.html代码取自: https://api.flutter.dev/flutter/material/Autocomplete-class.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 颤振,显示箭头指示用户可以向上或向下滚动 - Flutter, display arrow to indicate the user can scroll up or down 我可以使用 flutter 中的哪个小部件在单击向下箭头时向上滚动屏幕? - Which widget in flutter can I use to scroll the screen up on click of down arrow? 如何使用颤振中的文本字段制作数字计数器应用程序? - How can I make a number counter app using textfield in flutter? 使用Flutter,如何在iOS上触发TextField的onSubmitted()? - Using Flutter, how can I trigger onSubmitted() of TextField on iOS? 我如何在我的 flutter 应用程序上上下滚动 - how can i scroll up and down on my flutter app 如何将数据从 ListView 发送到 Flutter 中的 TextField? - How can I send data from ListView into TextField in Flutter? 如何在 Flutter 的 TextField 底部隐藏字母计数器 - How can I hide letter counter from bottom of TextField in Flutter Flutter 覆盖 Textfield 上/下键行为 - Flutter override Textfield up / down key behavior 使用 ViewModelProvider 时如何从 TextField 获取文本值? - How can i get text value from TextField while using a ViewModelProvider? Flutter &amp; Textfield :如何通过自动删除该空间来限制我的用户使用文本字段中的空间? - Flutter & Textfield : How do I restrict my user from using space in textfield by automatically removing that space?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM