Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

loops - Flutter - Get iteration index from List.map()

I searched alot on net for answer.

I wrote iteration on list of letters and put inside cards on screen using "map" class

In the code you can see that i made a row and using "map" printed aall the userBoard on cards to the screen. I want to add some logics inside so i need to gett the id of the elemnt (for taping event). Theres a way that i can do taht?

Actually i want to get a specific index of element over userBoard

code:

Widget build(BuildContext context) {
return Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: <Widget>[
        Row(
          children: userBoard
              .map((element) => Stack(children: <Widget>[
                    Align(
                      alignment: Alignment(0, -0.6),
                      child: GestureDetector(
                        onTap: (() {
                          setState(() {
                            // print("element=${element.toString()}");
                            // print("element=${userBoard[element]}");
                          });
                        }),
                        child: SizedBox(
                          width: 40,
                          height: 60,
                          child: Card(
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                              ),
                              child: Center(
                                child: Text(element,
                                    style: TextStyle(fontSize: 30)),
                              )),
                        ),
                      ),
                    )
                  ]))
              .toList(),
        )
      ],
    ),

}

Picture - each card is "element" of the map. I want to get the indexes for the function onTap.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get access to index, you need to convert your list to a map using the asMap operator.

Example

final fruitList = ['apple', 'orange', 'mango'];
final fruitMap = fruitList.asMap(); // {0: 'apple', 1: 'orange', 2: 'mango'}

// To access 'orange' use the index 1.
final myFruit = fruitMap[1] // 'orange'

// To convert back to list
final fruitListAgain = fruitMap.values.toList();

Your Code

userBoard.asMap().map((i, element) => MapEntry(i, Stack(
  GestureDetector(onTap: () {
    setState(() {
      // print("element=${element.toString()}");
      // print("element=${userBoard[i].toString()}");
    });
  }),
))).values.toList();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...