简体   繁体   English

如何使用 FutureBuilder Flutter 叠加和居中圆形进度指示器

[英]How to Overlay and Center Circular Progress Indicator with FutureBuilder Flutter

I have a FutureBuilder that shows a CircularProgressIndicator while data is being fetched.我有一个 FutureBuilder,它在获取数据时显示 CircularProgressIndicator。 Currently, the CircularProgressIndicator shows up at the top left corner of my screen and when it shows, nothing else is on the screen behind it.目前,CircularProgressIndicator 显示在我屏幕的左上角,当它显示时,它后面的屏幕上没有其他东西。 I would like to have the indicator centered and have it as an overlay in front of my other widgets when data is being fetched.我想让指示器居中,并在获取数据时将其作为我其他小部件前面的覆盖物。

class PriceScreen extends StatefulWidget {
  @override
  PriceScreenState createState() => PriceScreenState();
}

class PriceScreenState extends State<PriceScreen> {
  Future<Map> futureData;

  Future<Map> getData() async {
    ...
  }

  @override
  void initState() {
    super.initState();
    futureData = getData();
  }

  @override
  Widget build(BuildContext context) {
    //get object of the provided class dataProvider

    return Scaffold(
  appBar: AppBar(
    title: Text('My app'),
  ),
  body: FutureBuilder<Object>(
      future: futureData,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        } else
          return Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
              .
              .
              .
              .
              .
              
);
  }
}

To center it, use the Center widget To overlay it above another widget use the Stack widget and place it after the part you want it to go over in the children list要将其居中,请使用 Center 小部件 要将其覆盖在另一个小部件上方,请使用 Stack 小部件并将其放置在您希望它在子列表中经过的部分之后

Edit: to make the overlaying widget take the size of the underlaying widget use Positioned.fill around the overlaying widget.编辑:要使覆盖小部件采用底层小部件的大小,请在覆盖小部件周围使用 Positioned.fill。

To center the CircularProgressIndicator() simply wrap it with Center widget.要使 CircularProgressIndicator() 居中,只需将其与 Center 小部件包装起来。

  return Center(child: CircularProgressIndicator()) ;

There are packages available on pub.dev like modal_progress_hud ( https://pub.dev/packages/modal_progress_hud ) and loading_overlay ( https://pub.dev/packages/loading_overlay ) that you can use to easily show a modal progress indicator. pub.dev 上有可用的包,如 modal_progress_hud ( https://pub.dev/packages/modal_progress_hud ) 和 loading_overlay ( https://pub.dev/packages/loading_overlay ),您可以使用它们轻松显示模态进度指示器。 However you are using FutureBuilder to fetch data, so you'll have to make sure that widgets can be displayed before the data arrives.但是,您正在使用 FutureBuilder 来获取数据,因此您必须确保可以在数据到达之前显示小部件。

wrap your CircularProgressIndicator() in a center.将您的 CircularProgressIndicator() 包裹在一个中心。 Example:例子:

Center(child:CircularProgressIndicator());

You could also wrap the whole thing in a center like this:你也可以像这样将整个东西包裹在一个中心:

body: Center(
      child: FutureBuilder<Object>(
      future: futureData,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        } else
          return Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
              .
              .
              .
              .
              .
              
));

I'm not sure what you mean by overlay, since in your code example there is nothing other than the futurebuilder.我不确定您所说的覆盖是什么意思,因为在您的代码示例中,除了 futurebuilder 之外别无他物。

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

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