飞镖,颤振列表onTap以获得录音项目的索引

时间:2020-04-05 17:47:46

标签: list flutter dart indexing

我创建了一个搜索委托类,在逻辑上我想要的听起来很简单,但是由于我是新编程人员,所以我无法解决这个问题 我想获取文本,或者也许我应该在listTile中说出文本字符串,然后点击, 然后将该文本传递给搜索委托的showResult并在文本小部件中查看 ...这是我的代码

import 'package:flutter/material.dart';
import 'package:schooler/style/const.dart';
//follow steps 1 - 3
class DataSearch extends SearchDelegate<String> {
  final List<String> languages = [//3.here is my list
    'dart',
    'Csharp',
    'Java',
    'JavaScript',
    'C++',
    'go ',
    'python',
    'php'
  ];

  final List<String> recentSearch = [
    'go ',
    'python',
    'php',
  ];
  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = '';
        },
      )
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: AnimatedIcon(
        icon: AnimatedIcons.menu_arrow,
        progress: transitionAnimation,
      ),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Scaffold(
      body: ListView(
        physics: BouncingScrollPhysics(),
        children: <Widget>[
          Row(
            children: <Widget>[
              CircleAvatar(),
              Text(languages[0].substring(query.length)),//2.i want to return the gotten index here
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    final suggestionList = query.isEmpty
        ? recentSearch
        : languages.where((element) => element.startsWith(query)).toList();
    return ListView.builder(
      physics: BouncingScrollPhysics(),
      itemBuilder: (context, index) => ListTile(
        onTap: () {
          showResults(context); //1.when i tap the item on the listTile i want it to get the index of the taped item
        },
        leading: query.isEmpty
            ? Icon(
                Icons.history,
              )
            : Icon(Icons.search),
        title: RichText(
          text: TextSpan(
            text: suggestionList[index].substring(
              0,
              query.length,
            ),
            style: TextStyle(
              color: kBlackColor,
              fontWeight: kBold,
            ),
            children: [
              TextSpan(
                text: suggestionList[index].substring(query.length),
                style: TextStyle(color: kGreyColor),
              ),
            ],
          ),
        ),
      ),
      itemCount: suggestionList.length,
    );
  }
}

here if i tap on 'go' it should get get the text and pass it to the showResult

here is my show result ..but i just hard coded my 'list[0]index in there' i guess what i am trying to say is get the text from the item i taped and show it here

1 个答案:

答案 0 :(得分:0)

首先让我们在ListView中修复itemBuilder,以便为每个元素建立索引,然后将该索引添加到showResults()函数调用中,以便您可以访问它:

ListView.builder(
      physics: BouncingScrollPhysics(),
      itemBuilder: (context, index) => ListTile( // Add index here to pass the index to the builder.
        onTap: () {
          showResults(context, index); // Add the index to function call
        },

只需确保将showResults()函数修改为接受一个int索引参数,虽然我没有看到该函数,但我假设它在需要修改以接受索引的某个点调用buildResults(): / p>

@override
  Widget buildResults(BuildContext context, int index) {
    return Scaffold(
      body: ListView(
        physics: BouncingScrollPhysics(),
        children: <Widget>[
          Row(
            children: <Widget>[
              CircleAvatar(),
              Text(languages[index].substring(query.length)),// Use the index here to return the desired text
            ],
          ),
        ],
      ),
    );
  }