Show menu

main.dart

  void main() => runApp(DemoApp());

  class DemoApp extends StatelessWidget {
    const DemoApp();

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("SVG Download"),
          ),
          body: const Center(
            child: SvgFromWeb(),
          )
        ),
      );
    }
  }
  

downloader.dart

  class Downloader {
    final String url;
    const Downloader({ required this.url });

    static final _opt = BaseOptions(
        baseUrl: "https://fluttercompletereference.com/",
        connectTimeout: 3000,
        responseType: ResponseType.bytes
    );

    static final _client = Dio(_opt);

    Future<List<int>> start() async {
      final request = await _client.get<List<int>>(url);
      return request.data;
    }
  }
  

svg_widget.dart

  class SvgFromWeb extends StatefulWidget {
    const SvgFromWeb();

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

  class _SvgFromWebState extends State<SvgFromWeb> {
    late final Downloader downloader;
    late final Future<List<int>> svgImage;

    @override
    void initState() {
      super.initState();
      downloader = const Downloader(url: "/demoimages/firefox.svg");
      svgImage = downloader.start();
    }

    @override
    Widget build(BuildContext context) {
      return FutureBuilder<List<int>>(
        future: svgImage,
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return const ErrorWidget();
          }

          if (snapshot.hasData) {
            if (snapshot.data != null) {
              return SvgPicture.memory(
                Uint8List.fromList(snapshot.data!),
                placeholderBuilder: (_) => const DecoderLoader(),
              );
            } else {
              return const ErrorWidget();
            }
          }

          return const NetworkLoader();
        },
      );
    }
  }
  

network_loader.dart

  class NetworkLoader extends StatelessWidget {
    const NetworkLoader();

    @override
    Widget build(BuildContext context) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: const [
          CircularProgressIndicator(),

          Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text("Downloading..."),
          )
        ],
      );
    }

  }
  

decoder_loader.dart

  class DecoderLoader extends StatelessWidget {
    const DecoderLoader();

    @override
    Widget build(BuildContext context) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: const [
          CircularProgressIndicator(),

          Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text("Decoding SVG..."),
          )
        ],
      );
    }

  }
  

error_widget.dart

  class ErrorWidget extends StatelessWidget {
    const ErrorWidget();

    @override
    Widget build(BuildContext context) {
      return const Icon(
        Icons.error,
        size: 25,
        color: Colors.red,
      );
    }

  }
  

This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X

"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"