selection<T> static method

Future<T?> selection<T>(
  1. BuildContext context, {
  2. String title = "Confirmation",
  3. String? content,
  4. bool barrierDismisable = true,
  5. required List<PopupWidgetOptions<T>> data,
  6. required List<T> selectedData,
  7. required dynamic onOk(
    1. List<T> selectedData
    ),
  8. String okText = "Confirm",
  9. Color color = Colors.blue,
})

Implementation

static Future<T?> selection<T>(
  BuildContext context, {
  String title = "Confirmation",
  String? content,
  bool barrierDismisable = true,
  required List<PopupWidgetOptions<T>> data,
  required List<T> selectedData,
  required Function(List<T> selectedData) onOk,
  String okText = "Confirm",
  Color color = Colors.blue,
}) async {
  var size = MediaQuery.of(context).size;
  List<T> selected = selectedData;
  var dialog = Dialog(
    backgroundColor: Colors.white,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(10),
    ),
    child: StatefulBuilder(builder: (context, setState) {
      return Container(
        width: _isLargeDevice(size) ? 350 : size.width - 40,
        constraints: BoxConstraints(
          maxHeight: size.height - 80,
        ),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: Colors.white,
        ),
        child: SingleChildScrollView(
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const SizedBox(height: 10),
                Text(
                  title,
                  style: Theme.of(context).textTheme.bodyText1!.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                ),
                if (content != null) const Divider(),
                if (content != null) Text(content),
                const Divider(),
                ...data.map((e) {
                  bool isSelected = selected.contains(e.value);
                  return Container(
                    width: double.infinity,
                    padding: const EdgeInsets.only(bottom: 10),
                    child: ButtonWidget(
                      onPressed: () {
                        if (!isSelected) {
                          if (e.value != null) selected.add(e.value as T);
                        } else {
                          selected.remove(e.value);
                        }
                        setState(() {});
                      },
                      text: e.text,
                      background:
                          isSelected ? color.withOpacity(0.2) : Colors.white,
                      radius: 10,
                      textStyle: Theme.of(context)
                          .textTheme
                          .bodyText1!
                          .copyWith(color: color),
                      borderColor: color,
                    ),
                  );
                }).toList(),
                const Divider(),
                SizedBox(
                  width: double.infinity,
                  child: ButtonWidget(
                    onPressed: () {
                      onOk(selected);
                    },
                    text: okText,
                    borderColor: color,
                    background: color,
                  ),
                ),
                const SizedBox(height: 10),
              ],
            ),
          ),
        ),
      );
    }),
  );
  return showDialog(
      context: context,
      barrierDismissible: barrierDismisable,
      builder: (context) {
        return dialog;
      });
}