简体   繁体   English

Flutter中,没有固定高度的嵌套listview有溢出且不可滚动

[英]In Flutter, nested listview without fixed height has a overflow and non-scrollable

In my project, I have implemented a questionnaire format page.在我的项目中,我实现了一个问卷格式页面。

One questionnaire has a many parts (1,2,3...) and each part has a many questions.一份问卷有很多部分(1,2,3...),每个部分都有很多问题。

And, the questions have a many selects (Very bad, bad, so-so, good, Very good, etc.).而且,这些问题有很多选择(非常差、差、一般、好、非常好等)。

To implement this page, I chose a nested listview (inside listview, another listview is existed).为了实现这个页面,我选择了一个嵌套的列表视图(在列表视图中,存在另一个列表视图)。

This is a sample code that has a same widget tree with mine.这是一个与我的具有相同小部件树的示例代码。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          const Text('this is text inside 1st column. blue container'),
          Column(
            children: [
              const Text('this is text inside 2nd column. about part title'),
              ListView.separated(
                shrinkWrap: true,
                itemBuilder: (BuildContext firstListViewContext,
                    int firstListViewIndex) {
                  return Column(
                    children: [
                      const Text(
                          'this is text inside 1st listview and 3nd column. question title'),
                      ListView.separated(
                        shrinkWrap: true,
                        itemBuilder: (BuildContext context, int index) {
                          return Text(
                              'this is text inside 2nd listview and 3nd column, question selector list');
                        },
                        separatorBuilder: (BuildContext context, int index) =>
                            const SizedBox(height: 4),
                        itemCount: 20,
                      ),
                    ],
                  );
                },
                separatorBuilder: (BuildContext context, int index) =>
                    const SizedBox(height: 4),
                itemCount: 1,
              ),
            ],
          ),
          Spacer(),
          ElevatedButton(
              onPressed: () {},
              child: Text('this is button inside 1st column.')),
        ],
      ),
    );
  }
}

Above code has a overflow problem and the question listview cannot be scrolled...上面的代码有溢出问题,问题listview不能滚动...

Is there any way I can get rid of overflows in the code above and scroll down the problem list?有什么办法可以摆脱上面代码中的溢出并向下滚动问题列表?


Edit 1.编辑 1。

In my code, only question's listview should be scrollable.在我的代码中,只有问题的列表视图应该是可滚动的。

The button should be attached to the bottom of the screen.该按钮应附加到屏幕的底部。

Try this:尝试这个:

Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          const Text('this is text inside 1st column. blue container'),
          Expanded(
            child: Column(
              children: [
                const Text('this is text inside 2nd column. about part title'),
                Expanded(
                  child: ListView.separated(
                    itemBuilder: (BuildContext firstListViewContext,
                        int firstListViewIndex) {
                      return Column(
                        children: [
                          const Text(
                              'this is text inside 1st listview and 3nd column. question title'),
                          ListView.separated(
                            shrinkWrap: true,
                            itemBuilder: (BuildContext context, int index) {
                              return Text(
                                  'this is text inside 2nd listview and 3nd column, question selector list');
                            },
                            separatorBuilder:
                                (BuildContext context, int index) =>
                                    const SizedBox(height: 4),
                            itemCount: 20,
                          ),
                        ],
                      );
                    },
                    separatorBuilder: (BuildContext context, int index) =>
                        const SizedBox(height: 4),
                    itemCount: 1,
                  ),
                ),
              ],
            ),
          ),
          Spacer(),
          ElevatedButton(
              onPressed: () {},
              child: Text('this is button inside 1st column.')),
        ],
      )

在此处输入图像描述

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

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