ListView如何转到下一行/新行?

huangapple go评论62阅读模式
英文:

Listview How to go to next/new row?

问题

我已经使用Flutter几个月了,现在我遇到了一个问题,需要设计一个UI来显示给用户看,如下面的图片所示。

我想要在ListView内部创建新的行,有人知道如何做吗?

ListView如何转到下一行/新行?

Container(
  width: double.infinity,
  height: 35,
  child: ListView(
    scrollDirection: Axis.horizontal,
    children: [
      GestureDetector(
        onTap: () async {
          setState(() {
            category2 = 1;
          });
          print("选择设置和测试");
        },
        child: Card(
          color: Color.fromARGB(255, 27, 175, 224),
          child: Padding(
            padding: EdgeInsets.all(5),
            child: Column(
              children: [
                Text(
                  "选择设置和测试",
                  style: TextStyle(color: Colors.white),
                ),
              ],
            ),
          ),
        ),
      ),
      GestureDetector(
        onTap: () async {
          setState(() {
            category2 = 2;
          });
          print("选择容量");
        },
        child: Card(
          color: Color.fromARGB(255, 27, 175, 224),
          child: Padding(
            padding: EdgeInsets.all(5),
            child: Column(
              children: [
                Text(
                  "选择容量",
                  style: TextStyle(color: Colors.white),
                ),
              ],
            ),
          ),
        ),
      ),
      // 其他按钮可以继续添加在这里
    ],
  ),
)
英文:

I have been using flutter for past few months and now I have stuck in a problem where i have to design a ui to show the button to user as the image given below.

I want to go to new row inside a listview, anyone know how to make it ?

ListView如何转到下一行/新行?

Container(
            width: double.infinity,
            height: 35,
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: [
                    GestureDetector(
                      onTap: () async {
                        setState(() {
                          category2 = 1;
                        });
                        print("Selection Setting and Testing");
                      },
                      child: Card(
                          color: Color.fromARGB(255, 27, 175, 224),
                          child: Padding(
                            padding: EdgeInsets.all(5),
                            child: Column(
                              children: [
                                Text(
                                  "Selection Setting and Testing",
                                  style: TextStyle(color: Colors.white),
                                ),
                              ],
                            ),
                          )),
                    ),
                    GestureDetector(
                      onTap: () async {
                        setState(() {
                          category2 = 2;
                        });
                        print("Selection Capacity");
                      },
                      child: Card(
                          color: Color.fromARGB(255, 27, 175, 224),
                          child: Padding(
                            padding: EdgeInsets.all(5),
                            child: Column(
                              children: [
                                Text(
                                  "Selection Capacity",
                                  style: TextStyle(color: Colors.white),
                                ),
                              ],
                            ),
                          )),
                    ),

答案1

得分: 2

使用Wrap小部件而不是Listview,希望对您有所帮助。

Wrap(
  // spacing: 8.0, // 邻近芯片之间的间距
  // runSpacing: 4.0, // 行之间的间距
  direction: Axis.horizontal,
  children: [
    GestureDetector(
      onTap: () async {
        print("Selection Setting and Testing");
      },
      child: Card(
        color: Color.fromARGB(255, 27, 175, 224),
        child: Padding(
          padding: EdgeInsets.all(5),
          child: Column(
            children: [
              Text(
                "Selection Setting and Testing",
                style: TextStyle(color: Colors.white),
              ),
            ],
          ),
        ),
      ),
    ),
    GestureDetector(
      onTap: () {
        print("Selection Capacity1");
      },
      child: Card(
        color: Color.fromARGB(255, 27, 175, 224),
        child: Padding(
          padding: EdgeInsets.all(5),
          child: Column(
            children: [
              Text(
                "Selection Capacity",
                style: TextStyle(color: Colors.white),
              ),
            ],
          ),
        ),
      ),
    ),
    GestureDetector(
      onTap: () {
        print("Selection Capacity2");
      },
      child: Card(
        color: Color.fromARGB(255, 27, 175, 224),
        child: Padding(
          padding: EdgeInsets.all(5),
          child: Column(
            children: [
              Text(
                "Selection Capacity",
                style: TextStyle(color: Colors.white),
              ),
            ],
          ),
        ),
      ),
    ),
    GestureDetector(
      onTap: () {
        print("Selection Capacity3");
      },
      child: Card(
        color: Color.fromARGB(255, 27, 175, 224),
        child: Padding(
          padding: EdgeInsets.all(5),
          child: Column(
            children: [
              Text(
                "Selection Capacity",
                style: TextStyle(color: Colors.white),
              ),
            ],
          ),
        ),
      ),
    ),
    GestureDetector(
      onTap: () {
        print("Selection Capacity4");
      },
      child: Card(
        color: Color.fromARGB(255, 27, 175, 224),
        child: Padding(
          padding: EdgeInsets.all(5),
          child: Column(
            children: [
              Text(
                "Selection Capacity",
                style: TextStyle(color: Colors.white),
              ),
            ],
          ),
        ),
      ),
    ),
  ],
)

结果-> ListView如何转到下一行/新行?

英文:

Use Wrap widget instead of Listview hope its help to you.

Wrap(
// spacing: 8.0, // gap between adjacent chips
//  runSpacing: 4.0, // gap between lines
direction: Axis.horizontal,
children: [
GestureDetector(
onTap: () async {
print("Selection Setting and Testing");
},
child: Card(
color: Color.fromARGB(255, 27, 175, 224),
child: Padding(
padding: EdgeInsets.all(5),
child: Column(
children: [
Text(
"Selection Setting and Testing",
style: TextStyle(color: Colors.white),
),
],
),
),
),
),
GestureDetector(
onTap: () {
print("Selection Capacity1");
},
child: Card(
color: Color.fromARGB(255, 27, 175, 224),
child: Padding(
padding: EdgeInsets.all(5),
child: Column(
children: [
Text(
"Selection Capacity",
style: TextStyle(color: Colors.white),
),
],
),
),
),
),
GestureDetector(
onTap: () {
print("Selection Capacity2");
},
child: Card(
color: Color.fromARGB(255, 27, 175, 224),
child: Padding(
padding: EdgeInsets.all(5),
child: Column(
children: [
Text(
"Selection Capacity",
style: TextStyle(color: Colors.white),
),
],
),
),
),
),
GestureDetector(
onTap: () {
print("Selection Capacity3");
},
child: Card(
color: Color.fromARGB(255, 27, 175, 224),
child: Padding(
padding: EdgeInsets.all(5),
child: Column(
children: [
Text(
"Selection Capacity",
style: TextStyle(color: Colors.white),
),
],
),
),
),
),
GestureDetector(
onTap: () {
print("Selection Capacity4");
},
child: Card(
color: Color.fromARGB(255, 27, 175, 224),
child: Padding(
padding: EdgeInsets.all(5),
child: Column(
children: [
Text(
"Selection Capacity",
style: TextStyle(color: Colors.white),
),
],
),
),
),
),
],
),

Result-> ListView如何转到下一行/新行?

huangapple
  • 本文由 发表于 2023年1月9日 14:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定