Código

mail@pastecode.io avatar
unknown
dart
2 years ago
11 kB
2
Indexable
Never
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:t2_market/src/Model/ClientePrevisaoEntrega/ClientePrevisaoEntrega.dart';
import 'package:t2_market/src/Templates/Components/currencyFormat.dart';
import 'package:t2_market/src/Templates/Components/dateFormater.dart';
import 'package:t2_market/src/core/AppTextStyles.dart';
import 'package:t2_market/src/provider/FinalizeData.dart';

// ignore: must_be_immutable
class ShippingDialog extends StatelessWidget {
  ClientePrevisaoEntrega frete;

  ShippingDialog(this.frete, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    var receberAntesCutsto = Provider.of<FinalizeData>(context).getValorFrete;

    var data = Provider.of<FinalizeData>(context).getDate;

    var elevatedWidth = 300.0;

    var hoje = DateTime.now();

    var amanha = hoje.add(const Duration(days: 1));

    var dataEntrega;

    if (amanha.weekday == 6) {
      dataEntrega = amanha.add(const Duration(days: 2));
    } else if (amanha.weekday == 7) {
      dataEntrega = amanha.add(const Duration(days: 1));
    } else {
      dataEntrega = amanha;
    }

    dataEntrega = DateFormat('yyyy-MM-ddThh:mm:ss').format(dataEntrega);

    return AlertDialog(
      content: frete.previsaoEntregaList!.isNotEmpty
          ? Container(
              width: 310,
              height: 300,
              child: Column(
                children: [
                  Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "Escolha a data disponível",
                          style: TextStyle(fontSize: 12),
                        ),
                        Row(
                          children: [
                            Text(
                              "Receba gratuitamente em",
                              style: TextStyle(
                                  fontSize: 18, fontWeight: FontWeight.w300),
                            )
                          ],
                        ),
                        Container(
                            child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: frete.previsaoEntregaList!.length,
                          itemBuilder: (BuildContext context, int index) {
                            return Container(
                                padding: EdgeInsets.all(4),
                                child: receberAntesCutsto == 0
                                    ? data ==
                                            frete.previsaoEntregaList![index]
                                                .previsaoEntrega
                                                .toString()
                                        ? ElevatedButton(
                                            style: ElevatedButton.styleFrom(
                                                shape: RoundedRectangleBorder(
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            8)),
                                                primary: AppStyles.topGrey),
                                            onPressed: () {
                                              Provider.of<FinalizeData>(context,
                                                      listen: false)
                                                  .setDate('');
                                              Provider.of<FinalizeData>(context,
                                                      listen: false)
                                                  .setValorFrete(0);
                                            },
                                            child: Text(dateFormater(frete
                                                .previsaoEntregaList![index]
                                                .previsaoEntrega)))
                                        : ElevatedButton(
                                            style: ElevatedButton.styleFrom(
                                                shape: RoundedRectangleBorder(
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            8)),
                                                primary:
                                                    AppStyles.topGreyligth),
                                            onPressed: () {
                                              Provider.of<FinalizeData>(context,
                                                      listen: false)
                                                  .setDate(frete
                                                      .previsaoEntregaList![
                                                          index]
                                                      .previsaoEntrega
                                                      .toString());
                                            },
                                            child: Text(
                                                dateFormater(frete.previsaoEntregaList![index].previsaoEntrega)))
                                    : ElevatedButton(
                                        style: ElevatedButton.styleFrom(
                                            shape: RoundedRectangleBorder(
                                                borderRadius:
                                                    BorderRadius.circular(8)),
                                            primary: AppStyles.topGreyligth),
                                        child: Text(dateFormater(frete
                                            .previsaoEntregaList![index]
                                            .previsaoEntrega)),
                                        onPressed: null,
                                      ));
                          },
                        )),
                      ]),
                  frete.valorFrete != 0
                      ? Container(
                          child: Column(
                            children: [
                              Text("ou"),
                              Container(
                                  padding: EdgeInsets.only(top: 4),
                                  child: receberAntesCutsto == 0
                                      ? ElevatedButton(
                                          style: ElevatedButton.styleFrom(
                                              primary: AppStyles.topRed,
                                              minimumSize:
                                                  Size(elevatedWidth, 40),
                                              shape: RoundedRectangleBorder(
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          8.0))),
                                          onPressed: () {
                                            Provider.of<FinalizeData>(context,
                                                    listen: false)
                                                .setDate(
                                                    dataEntrega.toString());

                                            Provider.of<FinalizeData>(context,
                                                    listen: false)
                                                .setValorFrete(double.parse(
                                                    frete.valorFrete
                                                        .toString()));
                                          },
                                          child: Text(
                                              "Receba antes por ${currencyFormat.format(frete.valorFrete)}"))
                                      : ElevatedButton(
                                          style: ElevatedButton.styleFrom(
                                              primary: AppStyles.topRed,
                                              minimumSize:
                                                  Size(elevatedWidth, 40),
                                              shape: RoundedRectangleBorder(
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          8.0))),
                                          onPressed: () {
                                            Provider.of<FinalizeData>(context,
                                                    listen: false)
                                                .setDate('');
                                            Provider.of<FinalizeData>(context,
                                                    listen: false)
                                                .setValorFrete(0);
                                          },
                                          child:
                                              Text("Não desejo receber antes")))
                            ],
                          ),
                        )
                      : Container()
                ],
              ))
          : Container(
              child: Text(frete.mensagem.toString()),
            ),
      actions: [
        TextButton(
          style: TextButton.styleFrom(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(7.5)),
            backgroundColor: Colors.grey,
          ),
          onPressed: () async {
            Navigator.pop(context);
          },
          child: Container(
              padding: AppStyles.buttonPadding,
              child: Text('Ok', style: TextStyle(color: Colors.white))),
        ),
        TextButton(
          style: TextButton.styleFrom(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(7.5)),
            backgroundColor: Colors.red[700],
          ),
          onPressed: () async {
            Provider.of<FinalizeData>(context, listen: false).setDate('');
            Provider.of<FinalizeData>(context, listen: false).setValorFrete(0);
            Navigator.of(context).pop();
          },
          child: Container(
              padding: AppStyles.buttonPadding,
              child: Text('Cancelar', style: TextStyle(color: Colors.white))),
        )
      ],
    );
  }
}