简体   繁体   English

FLUTTER - 将列表的一部分映射到小部件

[英]FLUTTER - Mapping part of a list into Widgets

I am working on an ANAGRAM Game Screen.我正在处理 ANAGRAM 游戏屏幕。 The idea is to shuffle the letters of a word and make them into separate draggable boxes.这个想法是将单词的字母打乱,并将它们变成单独的可拖动框。 For now, I have succeeded in mapping my list of letters into widgets (LetterBox).现在,我已经成功地将我的字母列表映射到小部件 (LetterBox)。 Here is the code :这是代码:

 Widget build(BuildContext context) {
    var uD = Provider.of<UdProvider>(context);
    List letters = widget.word!.split("");
    letters.shuffle();
    print(letters);
    return Scaffold(
      backgroundColor: Colors.indigo[900],
             body: Row(children: [
        ...letters.map((e) => Expanded(child: LetterBox(letter: e))),
      ]),
    );

It works fine, but I am encountering a problem.它工作正常,但我遇到了问题。 If the word has too many letters, it will eventually raise a problem for my row.如果这个词有太多字母,它最终会给我的行带来问题。 What would be great is to display a maximum of 7 letters per row in a kind of column if the word is longer than 7. Is there a way to map part of the list into a row and the rest into another row ?如果单词长于 7,那么在一种列中每行最多显示 7 个字母会很棒。有没有办法将列表的一部分映射到一行,其余部分映射到另一行?

You might want to have a look at GridView .您可能想看看GridView With the GridView.count constructor, you can specify how many items you want to display in one direction (horizontal by default), before adding another row.使用GridView.count构造函数,您可以在添加另一行之前指定要在一个方向(默认为水平)显示的项目数。

GridView.count(
  crossAxisCount: 7,
  children: [...letters.map((e) => LetterBox(letter: e))]),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM