【问题标题】:Dart, Flutter List onTap to get index of taped itemDart,Flutter List onTap 以获取磁带项目的索引
【发布时间】:2023-04-03 23:35:01
【问题描述】:

我创建了一个搜索委托类,我想要的东西在逻辑上听起来很简单,但我无法理解它,因为我是新编程人员 我想获取文本,或者我应该说我点击的 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

【问题讨论】:

    标签: list flutter dart indexing


    【解决方案1】:

    首先让我们在 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():

    @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
                ],
              ),
            ],
          ),
        );
      }
    

    【讨论】:

    • 请问您如何建议我修改showResult() 函数以接受int?
    • 您能否将 showResult() 的代码添加到您的帖子中,以便我查看它当前的功能?
    • 我正在使用默认的颤振搜索类,扩展搜索代理
    猜你喜欢
    • 2021-04-05
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多