onAction static method

void onAction(
  1. BuildContext context,
  2. ItemSelect? itemSelect,
  3. int? index,
  4. ActionSelect acao,
  5. Map? data,
  6. Function reloadData,
  7. DataSource? dataSource,
)

Implementation

static void onAction(
    BuildContext context,
    ItemSelect? itemSelect,
    int? index,
    ActionSelect acao,
    Map? data,
    Function reloadData,
    DataSource? dataSource) async {
  if (acao.function != null) {
    if (acao.closePage) {
      Navigator.pop(context);
    }
    acao.function!(
        DataFunction(data: itemSelect, index: index, context: context));
  }
  if (acao.functionUpd != null) {
    if (acao.closePage) {
      Navigator.pop(context);
    }

    var res = await acao.functionUpd!(
        DataFunction(data: itemSelect, index: index, context: context));
    if (res == true) {
      reloadData();
    }
  } else if (acao.route != null || acao.page != null) {
    Map<String, dynamic> dados = Map();
    if (acao.keys?.entries != null) {
      for (MapEntry dado in acao.keys!.entries) {
        if (itemSelect != null &&
            (itemSelect.object as Map).containsKey(dado.key)) {
          dados.addAll({dado.value: itemSelect.object[dado.key]});
        } else if (data != null && data.containsKey(dado.key)) {
          dados.addAll({dado.value: data[dado.key]});
        }
      }
    }

    RouteSettings settings = (itemSelect != null || dados.isNotEmpty)
        ? RouteSettings(arguments: {
            'cod_obj': itemSelect?.id,
            'obj': itemSelect?.object,
            'data': dados,
            'dataSource': dataSource
          })
        : RouteSettings();

    var res = await Navigator.of(context).push(acao.route != null
        ? acao.route!
        : new MaterialPageRoute(
            builder: (_) => acao.page!(), settings: settings));

    if (res != null && res != false) {
      if (acao.closePage) {
        if (res is Map &&
            res['dados'] != null &&
            res['dados'] is Map &&
            res['dados'].isNotEmpty) {
          Navigator.pop(context, res['dados']);
        } else if (res is Map &&
            res['data'] != null &&
            res['data'] is Map &&
            res['data'].isNotEmpty) {
          Navigator.pop(context, res['data']);
        } else {
          Navigator.pop(context, res);
        }
      } else {
        reloadData();
      }
    }
  }
}