multiselect method

Widget multiselect({
  1. dynamic component,
  2. required double parentWidth,
})

Implementation

Widget multiselect({var component, required double parentWidth}) {
  List<String> _items = component["options"] != null
      ? component['options']
          .map<String>((innerMap) => innerMap['value'] as String)
          .toList()
      : ['Select'];
  MultiselectController _multiselectController = MultiselectController();
  ScrollController _multigetxController = ScrollController();

  return SizedBox(
    width: (component["cssClass"] != null && component["cssClass"] != "")
        ? SwitchCase().componentWidthSC(
            component["cssClass"],
            {
              "layout_1by8_col": parentWidth * 0.1,
              "layout_1_col": parentWidth * 0.2,
              "layout_2_col": parentWidth * 0.4,
              "layout_3_col": parentWidth * 0.68,
              "layout_4_col": parentWidth,
            },
            parentWidth * 0.2)
        : parentWidth * 0.2,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AltComponent().altComponentLabel(
            component: component,
            label: (component["displayLabel"] != null &&
                    component["displayLabel"] != "")
                ? component["displayLabel"]
                : (component["security"] != null &&
                        component["security"]["fieldLabel"] != null &&
                        component["security"]["fieldLabel"] != "")
                    ? component["security"]["fieldLabel"]
                    : component["label"]),
        Container(
          height: 85,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(componentBorderRadius),
              border: Border.all(color: Palette.componentBorderColor)),
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 5),
            child: MultiselectScope<String>(
              dataSource: _items,
              controller: _multiselectController,
              clearSelectionOnPop: true,
              keepSelectedItemsBetweenUpdates: true,
              initialSelectedIndexes: const [],
              onSelectionChanged: (indexes, items) {
                debugPrint(
                    'Custom listener invoked! Indexes: $indexes Items: $items');
                return;
              },
              child: Scrollbar(
                //isAlwaysShown: true,
                controller: _multigetxController,
                child: ListView.builder(
                    controller: _multigetxController,
                    itemCount: _items.length,
                    itemBuilder: (context, index) {
                      final controller =
                          MultiselectScope.controllerOf(context);

                      final itemIsSelected = controller.isSelected(index);

                      return InkWell(
                        onLongPress: () {
                          if (!controller.selectionAttached) {
                            controller.select(index);
                          }
                        },
                        onTap: () {
                          debugPrint('Item is selected: $itemIsSelected');
                          print(controller.getSelectedItems().toString());
                          controller.select(index);
                        },
                        child: Container(
                          color: itemIsSelected
                              ? Palette.multiselectSelectedColor
                              : null,
                          child: Padding(
                            padding: const EdgeInsets.symmetric(
                                vertical: 2, horizontal: 2),
                            child: Text(
                              _items[index],
                              style: componentTxtStyle.copyWith(
                                  color: (controller
                                          .getSelectedItems()
                                          .contains(_items[index])
                                      ? Colors.white
                                      : Palette.textfieldTxtColor)),
                            ),
                          ),
                        ),
                      );
                    }),
              ),
            ),
          ),
        ),
        AltComponent().altComponentInstruction(component: component)
      ],
    ),
  );
}