在Flutter中从Firebase加载图像时使用CircularProgressIndicator小部件

时间:2020-04-20 11:45:09

标签: firebase flutter spinner

我想知道如何为我从firebase获得的图像添加circleprogressindicator。当我加载数据“文本”时,首先是图像,这似乎并不优雅。这是我的代码。请查看我的代码,并帮助我实现这一目标。预先感谢。

class MyApp extends StatefulWidget {
  static const String id = 'Myapp_screen';
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(0.0),
        child: StreamBuilder(
          stream: Firestore.instance.collection('news').limit(20).snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Center(
                  child: CircularProgressIndicator(
                value: null,
                strokeWidth: 2.0,
              ));
            } else {
              return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) {
                  DocumentSnapshot documentSnapshot =
                  snapshot.data.documents[index];
                  return Container(
                    child: Card(
                      child: Column(children: <Widget>[
                        Image.network(documentSnapshot['images']),
                        Text(documentSnapshot['title'])
                      ]),
                    ),
                  );
                },
              );
            }
          },
        ));
  }
}

2 个答案:

答案 0 :(得分:3)

您可以使用loading builder属性来实现。

以下代码清除了您的想法。

Image.network(
          'https://previews.123rf.com/images/blueringmedia/blueringmedia1701/blueringmedia170100692/69125003-colorful-kite-flying-in-blue-sky-illustration.jpg',
          loadingBuilder: (BuildContext context, Widget child,
              ImageChunkEvent loadingProgress) {
            if (loadingProgress == null) return child;
            return Center(
              child: CircularProgressIndicator(
                value: loadingProgress.expectedTotalBytes != null
                    ? loadingProgress.cumulativeBytesLoaded /
                        loadingProgress.expectedTotalBytes
                    : null,
              ),
            );
          },
        ),

答案 1 :(得分:1)

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Fade in images';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Stack(
          children: <Widget>[
            Center(child: CircularProgressIndicator()),
            Center(
              child: FadeInImage.memoryNetwork(
                placeholder: kTransparentImage,
                image: 'https://picsum.photos/250?image=9',
              ),
            ),
          ],
        ),
      ),
    );
  }
}