dart Flutter show搜索键盘搜索

3zwjbxry  于 2023-10-13  发布在  Flutter
关注(0)|答案(2)|浏览(153)

我对Flutter和Dart非常陌生,所以我试图构建一个简单的搜索应用程序,基于Flutters showSearch搜索栏中键入的单词进行查询。我理解listtile是为了向用户显示建议而构建的,点击该建议将激活buildresults小部件。但是,我想启用键盘上的搜索按钮,以简单地搜索输入的单词,而不是点击有限的建议列表。Simple android keyboard
当正常单击按钮时,键盘关闭,buildresults不会激活。到目前为止,你必须真正点击一个建议的列表选项。有没有办法让键盘上的搜索按钮搜索输入的文本?或者用户是否仅限于建议的列表选项?我将在下面列出我的showSearch代表:

class StockDelegate extends SearchDelegate<String> {
  final stocks = [
    "IBM",
    "NKLAW",
    "DKNGZ",
    "DRD",
    "PRTS",
    "TSLA",
    "KIRK",
    "VBIV"
  ];
  final suggested = ["IBM", "TSLA", "BNTX"];

  @override
  // TODO: implement textInputAction

  // TODO: implement textInputAction
  TextInputAction get textInputAction => super.textInputAction;

  @override
  List<Widget> buildActions(BuildContext context) {
    //actions for app bar
    StockProvider _stockProvider =
        Provider.of<StockProvider>(context, listen: true);
    return [
      IconButton(
          icon: Icon(Icons.clear),
          onPressed: () {
            query = '';
          }),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    //Leading icon on the left of the app bar
    return IconButton(
        icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow,
          progress: transitionAnimation,
        ),
        onPressed: () {
          close(context, null);
        });
  }

  @override
  Widget buildResults(BuildContext context) {
    //build results code 
   
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    // show when someone search for something
    stocks.insert(0, query.toString());

    final suggestionList = query.isEmpty
        ? suggested
        : stocks.where((p) => p.startsWith(query)).toList();

    return ListView.builder(
      itemCount: suggestionList.length,
      itemBuilder: (context, index) => ListTile(
        onTap: () async {
          Provider.of<StockProvider>(context, listen: false)
              .searchBySymbol(suggestionList[index]);

          if (!stocks.contains(suggestionList[index]))
            suggestionList.add(suggestionList[index]);

          showResults(context);
        },
        leading: Icon(Icons.attach_money),
        title: Text(suggestionList[index]),
      ),
    );
  }
y1aodyip

y1aodyip1#

我不知道你是否知道TextField/Text/TextFormField中使用的onSubmitted property。因为,我在任何地方都看不到你的TexFormField/Text/TextField,但我想你一定在某个地方使用它。
所以,这就是onFieldSubmitted的使用方式,它可以完成你想要的工作,也就是说,当用户点击键盘上的放大透镜时,它会运行你的功能

TextFormField(
                controller: _yourTextEditingController,
                textInputAction: TextInputAction.search,
                onFieldSubmitted: (){
                     // here you do your operation when you hit the 
                     // keypad magnifying lens
                     // check with print()
                     print('Pressed via keypad');
                }
              )

不要与onSubmitted名称混淆,onFieldSubmittedTextFormField/Text/TextField中使用,TextFormField/Text/TextField使用onSubmitted属性。我希望这能回答你的问题:)让我知道,这是你要找的东西。

qybjjes1

qybjjes12#

showResults()方法类似于buildSuggestions()
重写showResults方法如下:

@override
  void showResults(BuildContext context) {
    super.showResults(context);
    showSuggestions(context);
    FocusScope.of(context).unfocus();
  }

相关问题