async_button 2.0.0-beta.1+2 copy "async_button: ^2.0.0-beta.1+2" to clipboard
async_button: ^2.0.0-beta.1+2 copied to clipboard

Customized buttons for asyncronous onPressed function. Supports seamless animation between [idle], [loading], [success] and [failure] button states.

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool toFinishSuccessfully = true;

  AsyncBtnStatesController elevatedBtnController = AsyncBtnStatesController();
  AsyncBtnStatesController outlinedBtnController = AsyncBtnStatesController();
  AsyncBtnStatesController textBtnController = AsyncBtnStatesController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('async_button'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            AsyncElevatedBtn.withDefaultStyles(
              asyncBtnStatesController: elevatedBtnController,
              onPressed: () async {
                try {
                  elevatedBtnController.update(AsyncBtnState.loading);

                  // await for your API/async process here, in this example we are
                  // simply waiting for 2 seconds
                  await Future.delayed(const Duration(seconds: 2));
                  if (!toFinishSuccessfully) throw '';
                  elevatedBtnController.update(AsyncBtnState.success);
                } catch (e) {
                  elevatedBtnController.update(AsyncBtnState.failure);
                }
              },
              child: const Text('Execute'),
            ),
            AsyncTextBtn.withDefaultStyles(
              asyncBtnStatesController: textBtnController,
              onPressed: () async {
                try {
                  textBtnController.update(AsyncBtnState.loading);

                  // await for your API/async process here, in this example we are
                  // simply waiting for 2 seconds
                  await Future.delayed(const Duration(seconds: 2));
                  if (!toFinishSuccessfully) throw '';
                  textBtnController.update(AsyncBtnState.success);
                } catch (e) {
                  textBtnController.update(AsyncBtnState.failure);
                }
              },
              child: const Text('Execute'),
            ),
            AsyncOutlinedBtn.withDefaultStyles(
              asyncBtnStatesController: outlinedBtnController,
              onPressed: () async {
                try {
                  outlinedBtnController.update(AsyncBtnState.loading);

                  // await for your API/async process here, in this example we are
                  // simply waiting for 2 seconds
                  await Future.delayed(const Duration(seconds: 2));
                  if (!toFinishSuccessfully) throw '';
                  outlinedBtnController.update(AsyncBtnState.success);
                } catch (e) {
                  outlinedBtnController.update(AsyncBtnState.failure);
                }
                toFinishSuccessfully = !toFinishSuccessfully;
              },
              child: const Text('Execute'),
            ),
          ],
        ),
      ),
    );
  }
}
31
likes
150
pub points
81%
popularity

Publisher

unverified uploader

Customized buttons for asyncronous onPressed function. Supports seamless animation between [idle], [loading], [success] and [failure] button states.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on async_button