简体   繁体   English

加载数据后如何更改循环进度指示器 Flutter

[英]how to change the circular progress indicator after loading the data Flutter

the circular progress indicator dont disappear after loading the data.加载数据后,圆形进度指示器不会消失。

this is my code where im using the progress indicator这是我使用进度指示器的代码

and when i reach the end of the grid view it should load the other data but当我到达网格视图的末尾时,它应该加载其他数据,但是

the progress indicator makes the same thing it loads and dont disappear after getting data.进度指示器与它加载的内容相同,并且在获取数据后不会消失。

i tried to make a boolean isLoading and tried to change it true or false but couldnt find the place where i can do this我尝试制作 boolean isLoading 并尝试将其更改为真或假,但找不到我可以这样做的地方

int pageNumber = 1;
String filterName = '';

class ShowsListDesign extends StatefulWidget {
  @override
  _ShowsListDesignState createState() => _ShowsListDesignState();
}

class _ShowsListDesignState extends State<ShowsListDesign> {
  ScrollController controller = ScrollController();
  ServicesClass service = ServicesClass();
  bool isLoading = false;

  @override
  void initState() {
controller.addListener(listenScrolling);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
  future: service.getFilms('posts/$pageNumber/$filterName'),
  builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
    if (snapshot.hasData) {
      return Stack(
        alignment: Alignment.bottomCenter,
            children: [
              GridView.builder(
                itemCount: snapshot.data.length,
                gridDelegate: const             
SliverGridDelegateWithMaxCrossAxisExtent(
                  maxCrossAxisExtent: 250,
                  crossAxisSpacing: 24,
                  mainAxisSpacing: 24,
                  childAspectRatio: (3 / 5),
                ),
                controller: controller,
                itemBuilder: (context, index) {
                  return FilmsCard(
                    image: snapshot.data[index]['thumbnailUrl'],
                    title: snapshot.data[index]['title'],
                    year: snapshot.data[index]['year'],
                  );
                },
              ),
              FloatingActionButton(
                onPressed: () {
                  scrollUp();
                },
                elevation: 24,
                backgroundColor: PRIMARY,
                child: const Text(
                  'Scroll Up',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
              ),
            ],
          );
        } else {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
      },
    );
  }

  void scrollUp() {
    const double start = 0;
    controller.animateTo(start,
        duration: const Duration(seconds: 1, milliseconds: 50),
        curve: Curves.easeIn);
  }

  void listenScrolling() {
    if (controller.position.atEdge) {
      final isTop = controller.position.pixels == 0;
      if (isTop) {
      } else {
        setState(() {
          pageNumber++;

          ShowsListDesign();
        });
      }
    }
  }
}

It is possible to get errors or no data on future, it will be better with handling those states.未来可能会出现错误或没有数据,处理这些状态会更好。

builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
  if (snapshot.connectionState == ConnectionState.waiting) {
    return const Center(
      child: CircularProgressIndicator(),
    );
  } else if (snapshot.hasData &&
      snapshot.connectionState == ConnectionState.done) {
    return Stack(
      alignment: Alignment.bottomCenter,
      children: [...],
    );
  } else if (!snapshot.hasData &&
      snapshot.connectionState == ConnectionState.done) {
    return const Text("couldn't find any data");
  } else if (snapshot.hasError) {
    return Text("${snapshot.error}");
  } else {
    return const Text(" any other");
  }
},

More about FutureBuilder class .更多关于FutureBuilder class的信息。

You can't use FutureBuilder if it's not a one page load.如果不是单页加载,则不能使用 FutureBuilder。 Try this: (I don't understand your scrolling mechanism though), also call super.initState when you override试试这个:(虽然我不明白你的滚动机制),当你覆盖时也调用 super.initState



String filterName = '';

class ShowsListDesign extends StatefulWidget {
  @override
  _ShowsListDesignState createState() => _ShowsListDesignState();
}

class _ShowsListDesignState extends State<ShowsListDesign> {
  ScrollController controller = ScrollController();
  ServicesClass service = ServicesClass();
  bool isLoading = false;

// New
  int pageNumber = 1;
  List? data;

  Future<void> load() async {
    setState(() {
      isLoading = true;
     
    });
    data = await service.getFilms('posts/1/$filterName');
    setState(() => isLoading = false);
    
  }

  @override
  void initState() {
// Make sure you call super.initState() when you override
    controller.addListener(listenScrolling);
    super.initState();
    load();
    
    
  }

// Also call dispose to remove listener
@override
  void dispose() {
    controller.removeListener(listener);
    controller.dispose();
    super.dispose();
}

  @override
  Widget build(BuildContext context) {
    return Builder(
  
  builder: (BuildContext context) {
    if (data != null) {
      return Stack(
        alignment: Alignment.bottomCenter,
            children: [
              GridView.builder(
                itemCount: data!.length,
                gridDelegate: const             
SliverGridDelegateWithMaxCrossAxisExtent(
                  maxCrossAxisExtent: 250,
                  crossAxisSpacing: 24,
                  mainAxisSpacing: 24,
                  childAspectRatio: (3 / 5),
                ),
                controller: controller,
                itemBuilder: (context, index) {
                  return FilmsCard(
                    image: data![index]['thumbnailUrl'],
                    title: data![index]['title'],
                    year: data![index]['year'],
                  );
                },
              ),
              FloatingActionButton(
                onPressed: () {
                  scrollUp();
                },
                elevation: 24,
                backgroundColor: PRIMARY,
                child: const Text(
                  'Scroll Up',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
              ),
            ],
          );
        } else {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
      },
    );
  }

  void scrollUp() {
    const double start = 0;
    controller.animateTo(start,
        duration: const Duration(seconds: 1, milliseconds: 50),
        curve: Curves.easeIn);
  }

  Future<void> listenScrolling() async {
// Change this depending on the scrolling works ?!
    if (controller.position.pixels == controller.position.maxScrollExtent && data != null) {
        List new_data = await service.getFilms('posts/${pageNumber + 1}/$filterName');
        data.addAll(new_data);
        setState(() {
          
          pageNumber++;

        });
      
    }
  }
}

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

相关问题 Flutter 循环进度指示器 - Flutter Circular Progress Indicator 颤动如何使循环进度指示器工作 - flutter how to get a circular progress indicator working Flutter 在获取的数据上有一个加载图标,而不是整个循环进度指示器屏幕 - Flutter Have a loading icon on the fetched data instead of a whole Circular Progress Indicator screen 为什么我的 flutter 循环进度条一直在加载? - Why is my flutter circular progress indicator keeps on loading? 如何在 flutter 中设置循环进度指示器的加载值,例如 60 天的进度百分比? - How to Set the Loading Value for the Circular progresss Indicator like progress percentage for 60 days in flutter? 如何在Material - &gt; Material Button中加载flutter时禁用按钮并设置圆形进度指示器 - How to disable the button and set a circular progress indicator while loading in flutter in Material -> Material Button FLUTTER | 自定义循环进度指示器 - FLUTTER | Custom Circular Progress Indicator Flutter:在某个值上更改圆形进度指示器的颜色 - Flutter: Change Color of Circular Progress Indicator on a certain value FutureBuilder加载数据时无法显示循环进度指示器 - Unable to show Circular progress indicator when FutureBuilder is loading data 如何使用 flutter 获得自定义循环进度指示器? - how to get custom circular progress indicator as shown in image with flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM