简体   繁体   English

如何在加载数据之前在 flutter 中显示进度条

[英]How to show progress bar in flutter before loading data

I have a request function and I want to show a progress bar before loading data but idk how to do that.我有一个请求 function 并且我想在加载数据之前显示一个进度条,但我不知道该怎么做。 can someone show me an example?有人可以给我举个例子吗?

You can implement flutter_easyloading package, https://pub.dev/packages/flutter_easyloading你可以实现flutter_easyloading package, https://pub.dev/packages/flutter_easyloading

for the progress widget is self you can use [CircularProgressIndicator] https://api.flutter.dev/flutter/material/CircularProgressIndicator-class.html )对于进度小部件是自我的,您可以使用 [CircularProgressIndicator] https://api.flutter.dev/flutter/material/CircularProgressIndicator-class.html

for managing the state show loading -> then the actual data -> in case of failure show the error message and stop loading用于管理 state 显示加载 -> 然后是实际数据 -> 在失败的情况下显示错误消息并停止加载

this can be achieved throw many different ways 1- the easies one FutureBuilder这可以通过多种不同的方式来实现 1-最简单的一个FutureBuilder

FutureBuilder<String>(
        future: _calculation, // a previously-obtained Future<String> or null
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          List<Widget> children;
          if (snapshot.hasData) {
            children = <Widget>[
              const Icon(
                Icons.check_circle_outline,
                color: Colors.green,
                size: 60,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Result: ${snapshot.data}'),
              )
            ];
          } else if (snapshot.hasError) {
            children = <Widget>[
              const Icon(
                Icons.error_outline,
                color: Colors.red,
                size: 60,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Error: ${snapshot.error}'),
              )
            ];
          } else {
            children = const <Widget>[
              SizedBox(
                child: CircularProgressIndicator(),
                width: 60,
                height: 60,
              ),
              Padding(
                padding: EdgeInsets.only(top: 16),
                child: Text('Awaiting result...'),
              )
            ];
          }
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: children,
            ),
          );
        },
      );

or you can use any state management you want或者您可以使用任何您想要的 state 管理

  • bloc (example)[https://bloclibrary.dev/#/flutterweathertutorial] bloc(示例)[https://bloclibrary.dev/#/flutterweathertutorial]

This code calls your function after running the linear progress indicator for a specified time.此代码在运行线性进度指示器指定时间后调用您的 function。
The script makes use of no external libraries该脚本不使用外部库

import 'dart:async';
import 'package:flutter/material.dart';


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

  @override
  _ProgressBarCallState createState() => _ProgressBarCallState();
}

class _ProgressBarCallState extends State<ProgressBarCall> {
  double _value = 0;
  @override
  Widget build(BuildContext context) {
    checkIndicator(delay: 2);
    return Scaffold(
      body: Column(
        children: [
          LinearProgressIndicator(
                      backgroundColor: Colors.grey,
                      color: Colors.green,
                      minHeight: 5,
                      value: _value,
                    ),
          Expanded(
            child: Container(child: Text("Perform function after loading"),),
            
          ),
        ],
      ),
    );
  }

  void checkIndicator({delay = 2}){
    new Timer.periodic(
        Duration(milliseconds: delay*100),
            (Timer timer){
          setState(() {
            if(_value == 1) {
              timer.cancel();
              performFunction();
            }
            else {
              _value = _value + 0.1;
            }
          });
        }
    );
  }

  void performFunction(){
    //call your function after the loading
  }
}


The performFunction() method can be used to load your data performFunction() 方法可用于加载数据
Set the duration of the linear progress indicator by setting the delay in the checkIndicator() method. 通过在 checkIndicator() 方法中设置延迟来设置线性进度指示器的持续时间。

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

相关问题 如何在加载recyclerview之前显示进度条? - How to show progress bar before loading recyclerview? 如何在使用数据加载列表视图之前在具有列表视图的活动中显示进度条(圆圈) - how to show progress bar(circle) in an activity having a listview before loading the listview with data 当片段视图加载是asynctask的一部分时,如何在片段加载之前显示进度条? - How to show progress bar before fragment loading when fragment view loading is part of asynctask? 如何在加载 ListView 时显示进度条? - How to show progress bar while loading ListView? 如何在WebView上显示加载图像或进度条 - How to show loading image or progress bar on WebView 如何在播放视频前显示进度条 - how to show the progress bar before playing the video 如何在加载列表视图之前显示加载栏? - How to show Loading bar before loading listview? 当无尽滚动加载更多数据时显示进度栏 - Show Progress Bar when Endless scroll is loading more data 在使用改造从服务器获取数据之前显示进度条 - show progress bar before fetching data from server using retrofit 在对话框中向列表视图显示数据之前无法显示进度条 - Unable to show progress bar before displaying data into listview inside Dialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM