Android 在给定无限高的情况下,如何解决颤振抽屉显示垂直视口的问题

Android 在给定无限高的情况下,如何解决颤振抽屉显示垂直视口的问题,android,ios,flutter,mobile,Android,Ios,Flutter,Mobile,要提到的是,我对flifter和Stackoverflow都是新手 在我的新闻阅读器应用程序中,我添加了一个侧抽屉,可以使用FutureBuilder从API中提取新闻类别。有一个DrawerHeader包含名为popularnews的类别,该类别不是从API获取的,它是静态的 每次我打开API时,热门新闻类别都会显示出来,并且工作正常,但其他类别不会显示在控制台中,而是显示如下错误 I/flutter ( 4486): ══╡ EXCEPTION CAUGHT BY RENDERING LIB

要提到的是,我对flifter和Stackoverflow都是新手

在我的新闻阅读器应用程序中,我添加了一个侧抽屉,可以使用FutureBuilder从API中提取新闻类别。有一个
DrawerHeader
包含名为popularnews的类别,该类别不是从API获取的,它是静态的

每次我打开API时,热门新闻类别都会显示出来,并且工作正常,但其他类别不会显示在控制台中,而是显示如下错误

I/flutter ( 4486): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4486): The following assertion was thrown during performResize():
I/flutter ( 4486): Vertical viewport was given unbounded height.
I/flutter ( 4486): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 4486): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 4486): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 4486): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 4486): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 4486): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 4486): the height of the viewport to the sum of the heights of its children.
我的代码如下

返回抽屉的小部件

@override
  Widget build(BuildContext context) {
    SizeConfig().init(context);

    return SizedBox(
      width: SizeConfig.safeBlockHorizontal*50,
      child: Theme(
        data: Theme.of(context).copyWith(canvasColor: const Color(0xFF2b4849)),
        child: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[
              DrawerHeader(
                child:  ListTile(
                  title: Text(
                    "Popular News",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 25
                    ),
                  ),
                  onTap: (){
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (BuildContext context) => MostPopularNewsfeed(),
                        )
                    );
                  },
                ),
              ),
          FutureBuilder(
            future: category,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if(snapshot.data == null) {
                return Container(
                  child: Center(
                    child: Text(
                      "Loading",
                      style: TextStyle(
                          color: Colors.white
                      ),
                    ),
                  ),
                );
              } else {
                return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _getDrawer(snapshot, index);
                  },
                );
              }
            },
          ),
            ],
          ),
        ),
      ),
    );
  }

fetchCategory():这个函数从API中获取类别

Future<List<Category>> fetchCategory() async {
    //try {
    String url = "https://tbsnews.net/json/category/news/list";
    dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl: url)).interceptor);
    Response response = await Dio().get(url);
    print(response);

    List<Category> categoryList = [];
    final Future<Database> dbFuture = categoryDatabaseHelper.initDb();

    if(response.statusCode == 200) {
      var decode = response.data;
      print(decode);
      for (var c in decode) {
        Category category = Category(c['tid'], c['name']);
        await categoryDatabaseHelper.insertCategory(category);
        categoryList.add(category);
      }
      return categoryList;
      //categoryDatabaseHelper.insertCategory(categoryList);
    } else {
      throw Exception("Failed to fetch category");
    }
  }

Future fetchCategory()异步{
//试一试{
字符串url=”https://tbsnews.net/json/category/news/list";
添加(DioCacheManager(CacheConfig(baseUrl:url)).interceptor);
Response=wait Dio().get(url);
打印(回复);
列表类别列表=[];
final Future dbFuture=categoryDatabaseHelper.initDb();
如果(response.statusCode==200){
var decode=响应数据;
打印(解码);
for(解码中的var c){
类别=类别(c['tid',c['name']);
等待categoryDatabaseHelper.insertCategory(类别);
类别列表。添加(类别);
}
返回类别列表;
//categoryDatabaseHelper.insertCategory(categoryList);
}否则{
抛出异常(“未能获取类别”);
}
}
到目前为止我所尝试的

@override
  Widget build(BuildContext context) {
    SizeConfig().init(context);

    return SizedBox(
      width: SizeConfig.safeBlockHorizontal*50,
      child: Theme(
        data: Theme.of(context).copyWith(canvasColor: const Color(0xFF2b4849)),
        child: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[
              DrawerHeader(
                child:  ListTile(
                  title: Text(
                    "Popular News",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 25
                    ),
                  ),
                  onTap: (){
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (BuildContext context) => MostPopularNewsfeed(),
                        )
                    );
                  },
                ),
              ),
          FutureBuilder(
            future: category,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if(snapshot.data == null) {
                return Container(
                  child: Center(
                    child: Text(
                      "Loading",
                      style: TextStyle(
                          color: Colors.white
                      ),
                    ),
                  ),
                );
              } else {
                return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _getDrawer(snapshot, index);
                  },
                );
              }
            },
          ),
            ],
          ),
        ),
      ),
    );
  }

我曾尝试将
DrawerHeader
FutureBuilder
放在
扩展的
小部件中,而不是
列表视图
。但没有成功

在ListView中,我添加了
shrinkWrap:true
,也添加了
scrollDirection:Axis.vertical
。这也没有用

还试图将它们放入
大小框
小部件中。这也不起作用

以上是我在StackOverflow中搜索之前与此问题相关的问题时发现的。最后,我自己发布了一个问题

需要注意的是,当我取下
DrawerHeader
时,一切都开始正常工作,不再有任何问题。但是这个
DrawerHeader
必须在那里


非常感谢您的时间和真诚的帮助。

如果问题只出现在抽屉阅读器上,请尝试用
大小框
约束框
将其包裹起来,并给它一定的高度和宽度。 问题在于
DrawerHeader
未被压缩,因此在构建时
ListView
无法了解其维度或约束,并引发此错误


这是我有根据的猜测,让我知道它是否有效。

尝试添加收缩膜:true和physics:NeverScrollableScrollPhysics()您的listview生成器。因为您正试图在listview中构建listview。谢谢您的评论。我刚刚添加了这两个,但再次显示了相同的错误。我刚刚使用了这两个。根据抽屉大小具有一定高度和宽度的SizeBox…然后使用minHeight和maxHeight的ContrinedBox…所有这些都是一样的…但谢谢您或者你的答案。