flutter 在ListView下添加更多项目

50pmv0ei  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(184)

我在ListView中有一些项目列表。当在现有ListView项之后添加另一个ListView甚至一个具有高度的容器时,所有ListItem都将消失。

Expanded(
            child: Container(
              color: theme.primaryColor,
              child: Scrollbar(
                /*isAlwaysShown: true,*/
                thumbVisibility: false,
                controller: _scrollController,
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: ListView(
                    controller: _scrollController,
                    children: categoriesWidgets!,
                  ),
                ),
              ),
            ),
          ),

我试过添加列和显示他们在一个孩子,但仍然没有成功。

mklgxw1f

mklgxw1f1#

使用列表视图生成器或网格视图生成器的shrinkWrap属性

GridView.builder(
            shrinkWrap: true,
    )

ListView.builder(
            shrinkWrap: true,
    )

有关详细信息,请运行以下代码

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    home: HomePage(),
  ));
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List data = ["1", "2", "3", "4", "5", "6", "7", "8"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          const SizedBox(height: 20),
          const Text(
            "Title",
            style: TextStyle(fontSize: 20),
          ),
          const SizedBox(height: 20),
          GridView.builder(
            shrinkWrap: true,
            itemCount: data.length,
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3, crossAxisSpacing: 10, mainAxisSpacing: 10),
            itemBuilder: (context, index) => Container(
              alignment: Alignment.center,
              color: Colors.grey.shade400,
              child: Text(
                data[index],
              ),
            ),
          ),
          const SizedBox(height: 30),
          const Text(
            "Title",
            style: TextStyle(fontSize: 20),
          ),
          const SizedBox(height: 20),
          ListView.builder(
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (context, index) => Padding(
              padding: const EdgeInsets.only(bottom: 10),
              child: ListTile(
                tileColor: Colors.grey.shade400,
                title: Text(
                  data[index],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

相关问题