简体   繁体   English

Flutter FutureBuilder – 在异步构建小部件时不显示进度指示器 function

[英]Flutter FutureBuilder – Progress indicator is not showing while widgets are building in async function

I am new to Flutter and this is my first question on Stackoverflow.我是 Flutter 的新手,这是我在 Stackoverflow 上的第一个问题。 Please let me know if I need to provide more context or details for my question.如果我需要为我的问题提供更多上下文或详细信息,请告诉我。

In my app the user can navigate to a page with a two-level nested list widgets.在我的应用程序中,用户可以导航到具有两级嵌套列表小部件的页面。 When there are more than 20 items in the lowest level list, it takes a few seconds for Flutter to render it before the page is pushed by the Navigator and shown to the user, which makes it feel like the app is frozen for a moment, which is what I dislike a lot.当最底层的列表超过20条时,Flutter需要几秒的渲染时间,页面才会被Navigator推送显示给用户,这让应用有瞬间被冻结的感觉,这是我非常不喜欢的。

I've tried to use FutureBuilder to build the widget tree in an async function and show a CircularProgressIndicator while the widgets are building.我尝试使用 FutureBuilder 在异步 function 中构建小部件树,并在构建小部件时显示 CircularProgressIndicator。 However, it doesn't work, the future builder just waits for the connection state to be done and no progress indicator is displayed.但是,它不起作用,未来的构建器只是等待连接 state 完成并且没有显示进度指示器。 Below is the simplified code that replicates the issue – no progress indicator is showing while the widgets are building.下面是复制问题的简化代码——在构建小部件时没有进度指示器显示。 I need to use the cacheExtent property in the List.builder to improve the scrolling smoothness, as each item on the list has text fields and inkWell which causes a laggy scrolling when not using the cache extent.我需要使用 List.builder 中的 cacheExtent 属性来提高滚动平滑度,因为列表中的每个项目都有文本字段和 inkWell,这会在不使用缓存范围时导致滚动延迟。 I do not need a lazy loading here as the list length is fixed and not that large.我不需要在这里进行延迟加载,因为列表长度是固定的并且不是那么大。 However, I can see that the future builder issue is not related to the cache extent.但是,我可以看到未来的构建器问题与缓存范围无关。

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  Future<Widget> _buildFutureWidget() async {
    List<Widget> list = List.generate(
        1000, (index) => Text('List item #' + (index + 1).toString()));
    return Scaffold(
      body: ListView.builder(
        itemCount: list.length,
        cacheExtent: 999999999999999,
        itemBuilder: (context, i) {
          print('Building item ${list[i]}');
          return ListTile(title: list[i]);
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Widget>(
      future: _buildFutureWidget(),
      builder: (context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.active:
          case ConnectionState.none:
          case ConnectionState.waiting:
            return Center(child: CircularProgressIndicator());
          default:
            if (snapshot.hasError)
              return Text('Error: ${snapshot.error}');
            else
              return snapshot.data;
        }
      },
    );
  }
}

Can you please help me to find what went wrong with my code and how to display the progress indicator while the widgets are being built?你能帮我找出我的代码出了什么问题以及如何在构建小部件时显示进度指示器吗?

Any insight is much appreciated!非常感谢任何见解!

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

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