image.loading_builder.0.dart 1.45 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7
/// Flutter code sample for [Image.loadingBuilder].
8

9
void main() => runApp(const LoadingBuilderExampleApp());
10

11 12
class LoadingBuilderExampleApp extends StatelessWidget {
  const LoadingBuilderExampleApp({super.key});
13 14 15 16

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
17
      home: LoadingBuilderExample(),
18 19 20 21
    );
  }
}

22 23
class LoadingBuilderExample extends StatelessWidget {
  const LoadingBuilderExample({super.key});
24 25 26 27 28 29 30 31 32 33

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(),
        borderRadius: BorderRadius.circular(20),
      ),
      child: Image.network(
34
        'https://flutter.github.io/assets-for-api-docs/assets/widgets/falcon.jpg',
35
        loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) {
36 37 38 39 40 41
          if (loadingProgress == null) {
            return child;
          }
          return Center(
            child: CircularProgressIndicator(
              value: loadingProgress.expectedTotalBytes != null
42
                  ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes!
43 44 45 46 47 48 49 50
                  : null,
            ),
          );
        },
      ),
    );
  }
}