Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to add custom fields or widgets in dialog #76

Closed
ebelevics opened this issue May 9, 2022 · 2 comments
Closed

Option to add custom fields or widgets in dialog #76

ebelevics opened this issue May 9, 2022 · 2 comments

Comments

@ebelevics
Copy link

ebelevics commented May 9, 2022

https://images.squarespace-cdn.com/content/v1/5cfdee013dbb080001afc0ff/1600237264823-BOX7W8U7X1MBJDPEGMFU/Prompt+to+allow+precise+location+in+iOS+14?format=2500w

So far it seems it is just limited to predefined set of widgets to use dialogs. For example it seems is not possible to add something like in image above or just adding some other fields like my own pin code field. I hoped that builder(context, child): would allow to do this thing, but it seems like it's not currently available.

@mono0926
Copy link
Owner

mono0926 commented May 9, 2022

This package doesn't support that kind of flexibility as described here: #19 (comment)

You can implement it by using CupertinoAlertDialog class - cupertino library - Dart API etc.

@mono0926 mono0926 closed this as completed May 9, 2022
@ebelevics
Copy link
Author

ebelevics commented May 9, 2022

So I have copied the code and added content parameter, so that it is now possible to add other components, for those who are interested

import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:animations/animations.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// Show alert dialog, whose appearance is adaptive according to platform
///
/// [useActionSheetForCupertino] (default: false) only works for
/// cupertino style. If it is set to true, [showModalActionSheet] is called
/// instead.
/// [actionsOverflowDirection] works only for Material style currently.
Future<T?> showMyAlertDialog<T>({
  required BuildContext context,
  String? title,
  String? message,
  Widget? content, //added possibility to add content
  List<AlertDialogAction<T>> actions = const [],
  bool barrierDismissible = true,
  AdaptiveStyle style = AdaptiveStyle.adaptive,
  bool useActionSheetForCupertino = false,
  bool useRootNavigator = true,
  VerticalDirection actionsOverflowDirection = VerticalDirection.up,
  bool fullyCapitalizedForMaterial = true,
  WillPopCallback? onWillPop,
  AdaptiveDialogBuilder? builder,
}) {
  void pop(T? key) => Navigator.of(
        context,
        rootNavigator: useRootNavigator,
      ).pop(key);
  final theme = Theme.of(context);
  final colorScheme = theme.colorScheme;
  final isCupertinoStyle = style.isCupertinoStyle(theme);
  if (isCupertinoStyle && useActionSheetForCupertino) {
    return showModalActionSheet(
      context: context,
      title: title,
      message: message,
      cancelLabel: actions.findCancelLabel(),
      actions: actions.convertToSheetActions(),
      style: style,
      useRootNavigator: useRootNavigator,
      onWillPop: onWillPop,
      builder: builder,
    );
  }
  final titleText = title == null ? null : Text(title);
  final messageText = message == null ? null : Text(message);
  final contentWidget = content == null
      ? messageText
      : Column(children: [
          if (messageText != null) messageText,
          Material(color: Colors.transparent, child: content),
        ]); // if content is present then material is used as container as not all widgets support cupertino
  return style.isCupertinoStyle(theme)
      ? showCupertinoDialog(
          context: context,
          useRootNavigator: useRootNavigator,
          builder: (context) {
            final dialog = WillPopScope(
              onWillPop: onWillPop,
              child: CupertinoAlertDialog(
                title: titleText,
                content: contentWidget, // replace
                actions: actions.convertToCupertinoDialogActions(
                  onPressed: pop,
                ),
                // TODO(mono): Set actionsOverflowDirection if available
                // https://twitter.com/_mono/status/1261122914218160128
              ),
            );
            return builder == null ? dialog : builder(context, dialog);
          },
        )
      : showModal(
          context: context,
          useRootNavigator: useRootNavigator,
          configuration: FadeScaleTransitionConfiguration(
            barrierDismissible: barrierDismissible,
          ),
          builder: (context) {
            final dialog = WillPopScope(
              onWillPop: onWillPop,
              child: AlertDialog(
                title: titleText,
                content: contentWidget, // replace
                actions: actions.convertToMaterialDialogActions(
                  onPressed: pop,
                  destructiveColor: colorScheme.error,
                  fullyCapitalized: fullyCapitalizedForMaterial,
                ),
                actionsOverflowDirection: actionsOverflowDirection,
                scrollable: true,
              ),
            );
            return builder == null ? dialog : builder(context, dialog);
          },
        );
}

// Used to specify [showOkCancelAlertDialog]'s [defaultType]
enum OkCancelAlertDefaultType {
  ok,
  cancel,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants