Free to copy ยท Oxygoreon

Flutter starter snippets

Small widgets and helpers we reuse in client apps. Copy, paste, adjust. Written for current Flutter with Material 3. These examples are uncompiled; test and adapt them before using in an app.

Loading button that blocks double taps

class BusyButton extends StatefulWidget {
  const BusyButton({super.key, required this.label, required this.onPressed});
  final String label;
  final Future<void> Function() onPressed;
  @override
  State<BusyButton> createState() => _BusyButtonState();
}

class _BusyButtonState extends State<BusyButton> {
  bool _busy = false;

  Future<void> _tap() async {
    setState(() => _busy = true);
    try {
      await widget.onPressed();
    } finally {
      if (mounted) setState(() => _busy = false);
    }
  }

  @override
  Widget build(BuildContext context) => FilledButton(
        onPressed: _busy ? null : _tap,
        child: _busy
            ? const SizedBox(
                width: 18, height: 18,
                child: CircularProgressIndicator(strokeWidth: 2))
            : Text(widget.label),
      );
}

Empty-state widget

class EmptyState extends StatelessWidget {
  const EmptyState({super.key, required this.message, this.action});
  final String message;
  final Widget? action;

  @override
  Widget build(BuildContext context) => Center(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(mainAxisSize: MainAxisSize.min, children: [
            const Icon(Icons.inbox_outlined, size: 48),
            const SizedBox(height: 12),
            Text(message, textAlign: TextAlign.center),
            if (action != null) ...[const SizedBox(height: 16), action!],
          ]),
        ),
      );
}

Debounced search field

class DebouncedSearch extends StatefulWidget {
  const DebouncedSearch({super.key, required this.onChanged});
  final ValueChanged<String> onChanged;
  @override
  State<DebouncedSearch> createState() => _DebouncedSearchState();
}

class _DebouncedSearchState extends State<DebouncedSearch> {
  Timer? _t; // import 'dart:async';

  @override
  void dispose() {
    _t?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => TextField(
        decoration: const InputDecoration(prefixIcon: Icon(Icons.search)),
        onChanged: (v) {
          _t?.cancel();
          _t = Timer(const Duration(milliseconds: 400), () => widget.onChanged(v));
        },
      );
}

Safe retry for network calls

Future<T> retry<T>(Future<T> Function() run, {int tries = 3}) async {
  var delay = const Duration(milliseconds: 500);
  for (var i = 1;; i++) {
    try {
      return await run();
    } catch (_) {
      if (i >= tries) rethrow;
      await Future.delayed(delay);
      delay *= 2;
    }
  }
}

Show a snackbar from anywhere safely

void toast(BuildContext context, String msg) {
  final m = ScaffoldMessenger.of(context);
  m.hideCurrentSnackBar();
  m.showSnackBar(SnackBar(content: Text(msg)));
}