加载GridView时如何在Scaffold中显示CircularProgressIndicator

时间:2020-06-08 18:39:58

标签: flutter dart flutter-layout

我想在容器中心显示一个圆形的进度指示器,直到所有图像从互联网上加载为止。但这是行不通的。我尝试了很多方法。

class Category extends StatefulWidget {
  static String screenid = "Category_screen";
  @override
  _CategoryState createState() => _CategoryState();
}

class _CategoryState extends State<Category> {
  @override
  Widget build(BuildContext context) {
    if(CategoryGridItems == null){
      return Center(
        child: CircularProgressIndicator(),
      );
    }
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                  colors: [Color(0xff1f4037), Colors.white],
                  begin: Alignment.topCenter,
                  end: Alignment.center),
            ),
            height: 300.0,
          ),
          SingleChildScrollView(
            child: Container(
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 60.0,
                  ),
                  // Add Horizontal scroll here
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 5.0),
                    height: 70.0,
                    color: Colors.redAccent,
                  ),
                  Container(
                    child: GridView.count(
                      shrinkWrap: true,
                      physics: ClampingScrollPhysics(),
                      crossAxisCount: 2,
                      crossAxisSpacing: 4,
                      mainAxisSpacing: 8,
                      childAspectRatio: 1.8,
                      padding:
                          EdgeInsets.symmetric(horizontal: 8.0, vertical: 25.0),
                      children: <Widget>[
                        CategoryGridItems(
                          image: 'https://i.ibb.co/fxfWjyn/custom-1-2x.jpg',
                          ontapped: () {
                            Navigator.push(context,
                                MaterialPageRoute(builder: (context) {
                              return CategoryGrid(
                                label: 'nature',
                                colour: Color(0xffba2828),
                                displaytext: 'Nature',
                              );
                            }));
                          },
                        ),
                        CategoryGridItems(
                          image: 'https://i.ibb.co/JsLpD25/custom-2-2x.jpg',
                          ontapped: () {
                            Navigator.push(context,
                                MaterialPageRoute(builder: (context) {
                              return CategoryGrid(
                                label: 'dark_collection',
                                colour: Color(0xffba2828),
                                displaytext: 'Dark',
                              );
                            }));
                          },
                        ),
                        CategoryGridItems(
                          image: 'https://i.ibb.co/GvrzW7R/custom-8-2x.jpg',
                          ontapped: () {
                            Navigator.push(context,
                                MaterialPageRoute(builder: (context) {
                              return CategoryGrid(
                                label: 'abstract_collection',
                                colour: Color(0xff646def),
                                displaytext: 'Abstract',
                              );
                            }));
                          },
                        ),
                        CategoryGridItems(
                          image: 'https://i.ibb.co/3mJ6c0J/custom-3-2x.jpg',
                          ontapped: () {
                            Navigator.push(context,
                                MaterialPageRoute(builder: (context) {
                              return CategoryGrid(
                                label: 'food_collection',
                                colour: Color(0xff646def),
                                displaytext: 'Food',
                              );
                            }));
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}

class CategoryGridItems extends StatelessWidget {
  final String image;
  final Function ontapped;

  CategoryGridItems({
    @required this.image,
    @required this.ontapped,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: GestureDetector(
          onTap: ontapped,
          child: ClipRRect(
            borderRadius: BorderRadius.all(
              Radius.circular(8.0),
            ),
            child: Image.network(
              image,
            ),
          ),
        ),
      ),
    );
  }
}

我在容器内给出了Loading,但它显示在容器内。我不想显示空白屏幕,而是显示循环进度指示器。

1 个答案:

答案 0 :(得分:0)

这不是CircularProgressIndicator,但是请考虑使用FadeInImage小部件:

FadeInImage.memoryNetwork(
  placeholder: kTransparentImage,
  image: 'https://picsum.photos/250?image=9',
);

要显示网络映像的CircularProgressIndicator,您将需要处理HTTP请求。 IMO,您应该节省您的时间并使用上面的代码(您有时可能会遇到4xx错误,但是大多数情况下最终应该加载图像。如果不能,则仍然可以,因为它不会完全崩溃您的应用程序)。