【问题标题】:Flutter-Dart List-Map get item number or text where i clicked?Flutter-Dart List-Map 获取我单击的项目编号或文本?
【发布时间】:2021-04-05 06:21:27
【问题描述】:

如果我单击 List,我想获得一个 String 或 Map Number int。我想转发到另一个功能。像这样,

我有一个列表,

List <String> konulistesi = <String> ["Max","Adam","Anna","Sophie","Alex",]

如果我单击列表示例 Adam,那么它将打印为数字,例如:1,或者对于 Sophie,打印:3,

body: SafeArea(
              child:
              ListView.separated(
                padding: const EdgeInsets.all(8),
                itemCount: konuListesi.length,
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                    height: 50,
                    color: Colors.green,
                    child: InkWell(child:
                    Center(child:


                    Text(konuListesi[index],style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 18),),),
                    onTap: ()=> {

                   
                   
           
                 print("Number of the Position or direkt Item"),

                      
                    }),

                  );
                },

                separatorBuilder: (BuildContext context, int index) => const Divider(),


              ),
          ),

【问题讨论】:

  • 打印(索引);索引在那里定义

标签: list flutter dictionary dart


【解决方案1】:

您不应将arrow function 用于 InkWell 小部件的 onTap 方法。 print(index) 会给你数组的索引。 print(index + 1) 将为您提供数组的可数索引。 Dart 中数组的索引从 0 开始。

而且,print(konuListesi[index]) 会给你选择的项目:

     onTap: () {
                                            
               print("Number of the Position or direkt Item");
               print(index);
               print(konuListesi[index]);
                  
                }),

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2020-12-18
    • 2021-01-17
    • 2021-03-25
    相关资源
    最近更新 更多