progress_builder 1.1.0 copy "progress_builder: ^1.1.0" to clipboard
progress_builder: ^1.1.0 copied to clipboard

outdated

A simple wrapper for asynchronous actions with progress/loading indicators and error handling.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:progress_builder/progress_builder.dart';
import 'package:widget_with_codeview/widget_with_codeview.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp(
        title: 'Progress Builder Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const Scaffold(
          body: WidgetWithCodeView(
            sourceFilePath: 'lib/main.dart',
            codeLinkPrefix:
                'https://github.com/apexlabs-ai/progress_builder/blob/master/example',
            child: MyHomePage(),
          ),
        ),
      );
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  bool _fail = true;
  String _message = '';
  late ActionController _controller;

  void _showMessage(String message) {
    setState(() {
      _message = message;
    });
  }

  void _showSnackbar(String message) => ScaffoldMessenger.of(context)
      .showSnackBar(SnackBar(content: Text(message)));

  @override
  void initState() {
    _controller = ActionController();
    super.initState();
  }

  @override
  void dispose() {
    _controller.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          LinearProgressBuilder(
            builder: (context, action, error) => Column(
              children: [
                ElevatedButton(
                  onPressed: action,
                  child: const Text(
                    '50/50 chance of success',
                  ),
                ),
                if (error != null)
                  Container(
                    color: Colors.red,
                    padding: const EdgeInsets.all(16.0),
                    child: Text(error.toString()),
                  )
              ],
            ),
            action: (onProgress) async {
              _fail = !_fail;
              _showMessage('loading undetermined');
              await Future.delayed(const Duration(seconds: 1));
              for (final progress in [10, 20, 30, 40, 50, 60, 70, 80, 90]) {
                _showMessage('loaded $progress%');
                onProgress(progress / 100);
                await Future.delayed(const Duration(milliseconds: 200));
              }
              if (_fail) {
                throw Exception('Loading failed');
              }
              _showMessage('loaded');
            },
            onDone: () => _showMessage('done'),
            onError: (error) => _showSnackbar('error $error'),
            onSuccess: () => _showSnackbar('success'),
            controller: _controller
          ),
          Text(_message),
          ElevatedButton(
              onPressed: () => _controller.add(ActionType.start),
              child: const Text('Trigger action via controller'))
        ],
        // This trailing comma makes auto-formatting nicer for build methods.
      );
}
5
likes
0
points
80
downloads

Publisher

verified publisherapexive.com

Weekly Downloads

A simple wrapper for asynchronous actions with progress/loading indicators and error handling.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on progress_builder