button function

Widget button({
  1. String? title,
  2. Widget? titleWidget,
  3. VoidCallback? onTap,
  4. IconData? icon,
  5. double? width,
  6. double? height,
  7. TextStyle? textStyle,
  8. Color? backgroundColor,
  9. ButtonType buttonType = ButtonType.elevated,
  10. EdgeInsets? padding,
  11. PageState state = PageState.initial,
  12. int countDownSeconds = 120,
})

Implementation

Widget button({
  final String? title,
  final Widget? titleWidget,
  final VoidCallback? onTap,
  final IconData? icon,
  final double? width,
  final double? height,
  final TextStyle? textStyle,
  final Color? backgroundColor,
  final ButtonType buttonType = ButtonType.elevated,
  final EdgeInsets? padding,
  final PageState state = PageState.initial,
  final int countDownSeconds = 120,
}) {
  final Rx<PageState> buttonState = state.obs;
  if (buttonType == ButtonType.elevated)
    return Obx(
      () {
        if (buttonState.value == PageState.initial)
          return ElevatedButton(
            style: ButtonStyle(
              textStyle: textStyle == null ? null : WidgetStatePropertyAll<TextStyle>(textStyle),
              backgroundColor: WidgetStateProperty.all(backgroundColor),
              padding: WidgetStateProperty.all(padding),
            ),
            onPressed: onTap,
            child: SizedBox(
              height: height ?? 20,
              width: width ?? MediaQuery.sizeOf(navigatorKey.currentContext!).width,
              child: Center(
                child: titleWidget ?? Text(title ?? '', textAlign: TextAlign.center),
              ),
            ),
          );
        else if (buttonState.value == PageState.loading)
          return const CircularProgressIndicator().alignAtCenter();
        else if (buttonState.value == PageState.paging)
          return SlideCountdown(
            separatorStyle: TextStyle(color: Theme.of(navigatorKey.currentContext!).colorScheme.onSurface),
            decoration: const BoxDecoration(),
            style: Theme.of(navigatorKey.currentContext!).textTheme.bodyMedium!,
            duration: Duration(seconds: countDownSeconds),
            onDone: () => buttonState(PageState.initial),
          ).alignAtCenter();
        else
          return const SizedBox();
      },
    );
  if (buttonType == ButtonType.outlined)
    return OutlinedButton(
      style: ButtonStyle(
        textStyle: textStyle == null ? null : WidgetStatePropertyAll<TextStyle>(textStyle),
        backgroundColor: WidgetStateProperty.all(backgroundColor),
        padding: WidgetStateProperty.all(padding),
      ),
      onPressed: onTap,
      child: SizedBox(
        height: height ?? 20,
        width: width ?? MediaQuery.sizeOf(navigatorKey.currentContext!).width,
        child: Center(
          child: titleWidget ?? Text(title ?? '', textAlign: TextAlign.center),
        ),
      ),
    );
  if (buttonType == ButtonType.text)
    return TextButton(
      style: ButtonStyle(
        textStyle: textStyle == null ? null : WidgetStatePropertyAll<TextStyle>(textStyle),
        backgroundColor: WidgetStateProperty.all(backgroundColor),
        padding: WidgetStateProperty.all(padding),
      ),
      onPressed: onTap,
      child: SizedBox(
        height: height ?? 20,
        width: width ?? MediaQuery.sizeOf(navigatorKey.currentContext!).width,
        child: Center(
          child: titleWidget ?? Text(title ?? '', textAlign: TextAlign.center),
        ),
      ),
    );
  return const SizedBox();
}