Flutter)我想知道如何将相同的滚动应用到 ListView 中的子小部件列表

Flutter) I want to know how to apply the same scroll to the child widget list within ListView

(稍微修改了内容,ExpansionTile => GridView)

我正在创建一个包含许多 GridView 小部件的 ListView。 这样做的问题是ListView有竖向滚动,但是和ListView中的GridView Widget不兼容。 ListView 中的滚动仅适用于空白区域 space,不适用于子部件区域。

            Container(
              height: size!.height * 0.6,
              padding: EdgeInsets.all(common_padding),
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.grey,
                  width: 0.7,
                ),
                borderRadius: BorderRadius.all(
                  Radius.circular(10),
                ),
              ),
              child: ListView(
                children: [
                  // GridView.builder() .... // many widgets
                ]
             ),
           )

我的代码简单如上。 如何在列表视图中将滚动应用到“子项”小部件?

我不明白您所说的扩展小部件是什么意思,但您应该将列表中的每一项逐一堆叠,而不是将所有项目都打包到一个小部件中。例如,根据您的代码,

  MaterialApp(
      home: Scaffold(
        body: Container(
          height: double.infinity,
          padding: EdgeInsets.all(15),
          decoration: BoxDecoration(
            border: Border.all(
              color: Colors.grey,
              width: 0.7,
            ),
            borderRadius: BorderRadius.all(
              Radius.circular(10),
            ),
          ),
          child: ListView(children: [

//putting each item one by one
            Container(
              height: 500,
              color: Colors.blue,
            ),
            Container(
              height: 500,
              color: Colors.green,
            ),
            Container(
              height: 500,
              color: Colors.red,
            ),
            Container(
              height: 500,
              color: Colors.yellow,
            ),
          ]),
        ),
      ),
    );

对 ListView.builder

使用 shrinkWrap: truephysics: const NeverScrollableScrollPhysics(),

试试这个例子:

import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        itemCount: 30,
        shrinkWrap: true,
        itemBuilder: (BuildContext context, findex) {
          return ExpansionTile(
            key: Key(findex.toString()),
            title: const Text("title",
              style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold,color: Colors.black),
            ),
            children: [
              ListView.builder(
                itemCount: 10,
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemBuilder: (BuildContext context, sindex) {
                  return const ListTile(
                    title: Text(
                      "user tierl",
                      style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold,color: Colors.black),
                    ),
                  );
                },
              ),
            ],
          );
        },
      ),
    );
  }
}