如何在 flutter 中创建一个带有容器和列表视图构建器的可滚动列?

问题描述 投票:0回答:1

我想创建一个视图,其中将有一个包含一些数据的容器,下面是一些其他数据的列表。我希望它们都一起滚动。因此,为此我使用了一个 singlechildscrollview (physics: ScrollPhysics(),) 在里面我使用了一个列,在列内部我使用了容器和一个列表视图构建器(shrinkWrap: true,physics: NeverScrollableScrollPhysics(),)。但是什么时候这样做我得到了一个异常错误。

** FlutterError(RenderFlex 子项具有非零弹性,但传入的高度约束是无界的。 当列在不提供有限高度约束的父级中时,例如,如果它在垂直可滚动中,它将尝试沿垂直轴收缩包装其子级。为孩子设置 flex(例如使用 Expanded)表示孩子将展开以填充垂直方向的剩余空间。 **

然后我在用 Expended() 包装构建器后尝试了相同的代码。我收到了这个错误。 ** FlutterError(RenderFlex 子项具有非零弹性,但传入的高度约束是无界的。 当列在不提供有限高度约束的父级中时,例如,如果它在垂直可滚动中,它将尝试沿垂直轴收缩包装其子级。为孩子设置 flex(例如使用 Expanded)表示孩子将展开以填充垂直方向的剩余空间。 **

** 我的代码 **

import 'package:Healthwise/helpers/dataVariables.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get_navigation/get_navigation.dart';
import '../helpers/backEnd.dart';
import '../helpers/frontEnd.dart';

class ResultPage extends StatefulWidget {
  const ResultPage({Key? key}) : super(key: key);

  @override
  State<ResultPage> createState() => _ResultPageState();
}

class _ResultPageState extends State<ResultPage> {
  // String docId = '';
  String objectToString = '';

  // List of string for storing..
  var dataAsString = <String>[];

  @override
  void initState() {
    super.initState();
    // getUsers();
    getUserById();
  }

  getUserById() {
    String id = itemName.toLowerCase().trim();

    fruitInfoDoc.doc(id).get().then((DocumentSnapshot doc) {
      // final x = doc.data();
      // docId= doc.id;
      objectToString = doc.data().toString();
      String temp = '';
      // print(doc.data());
      // print(doc.id);
      int i = 1;

      bool end = false;
      //We are just parsing the object into string.
      while (objectToString[i] != '}') {
        if (objectToString[i - 1] == ' ' && objectToString[i - 2] == ':') {
          while (objectToString[i] != ',' && end != true) {
            temp += objectToString[i];
            if (objectToString[i + 1] != '}') {
              i++;
            } else {
              end = true;
            }
          }
          //Here I add all the strings to list...
          // This line works fine.
          dataAsString.add(temp);
          temp = '';
          print("The code below this line prints everything perfectly");
          print(dataAsString.length);
          print(dataAsString);
        }
        i++;
      }

      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        Navigator.pop(context);
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
            automaticallyImplyLeading: false,
            backgroundColor: primary_color,
            title: Center(
              child: Text(
                itemName.trim().toUpperCase(),
                style: TextStyle(
                    //Fruit Name
                    color: Color.fromARGB(255, 255, 255, 255),
                    fontSize: 15),
              ),
            )),
        body: SingleChildScrollView(
          physics: ScrollPhysics(),
          child: Column(
            children: [
              Container(
                color: Color.fromARGB(255, 150, 50, 50),
                height: 200,
                width: 100,
              ),
              Expanded(
                child: Expanded(
                  child: Builder(
                    builder: (context) {
                      if (dataAsString.length > 0) {
                        return ListView.builder(
                            shrinkWrap: true,
                            physics: NeverScrollableScrollPhysics(),
                            itemCount: dataAsString.length,
                            itemBuilder: (BuildContext context, int index) {
                              return EditedListTile(
                                dataAsString: dataAsString,
                                index: index,
                              );
                            });
                      } else {
                        return const Center(
                          child: CircularProgressIndicator(),
                        );
                      }
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
android flutter
1个回答
0
投票

由于我无法使用您的代码进行调试,这里有一些与您尝试做的类似的事情。您收到该错误是因为您没有将扩展小部件放在正确的位置。

你有整个页面可滚动,里面的容器有可滚动的 Listview 或删除 Listview 并在此处添加其他小部件。

在它下面有另一个小部件可滚动列表视图,您可以用其他小部件替换它,但保持扩展小部件不变。

有关扩展小部件的更多信息

class ResultPage extends StatefulWidget {
  const ResultPage({Key? key}) : super(key: key);

  @override
  State<ResultPage> createState() => _ResultPageState();
}

class _ResultPageState extends State<ResultPage> {
  // String docId = '';
  String objectToString = '';

  // List of string for storing..
  List<String> list = List<String>.generate(100, (counter) => "Item $counter");

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        Navigator.pop(context);
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          title: Center(
            child: Text(
              'itemName'.trim().toUpperCase(),
              style: TextStyle(
                  //Fruit Name
                  color: Color.fromARGB(255, 255, 255, 255),
                  fontSize: 15),
            ),
          ),
        ),
        body: SingleChildScrollView(
          child: Container(
            height: MediaQuery.of(context).size.height * 1,
            child: Column(
              children: [
                Expanded(
                  child: Container(
                    color: Color.fromARGB(255, 150, 50, 50),
                    width: 100,
                    child: ListView.builder(
//                   physics: NeverScrollableScrollPhysics(), // Uncomment this to stop scroll
                      itemCount: list.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Text(list[index]);
                      },
                    ),
                  ),
                ),
                Expanded(
                  child: ListView.builder(
//                   physics: NeverScrollableScrollPhysics(), // Uncomment this to stop scroll
                    itemCount: list.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Text(list[index]);
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.