import 'package:flutter/material.dart';
import 'package:test_sa/views/app_style/sizing.dart';

class ADatePicker extends StatelessWidget {
  final DateTime date;
  final DateTime from;
  final DateTime to;
  final Function(DateTime) onDatePicker;

  const ADatePicker({Key key, this.date, this.onDatePicker, this.from, this.to}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white,
        textStyle: Theme.of(context).textTheme.subtitle2,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
                12 * AppStyle.getScaleFactor(context)
            ),
        ),
      ),
      child: Text(
        date == null ? "Pick Date" :
        date.toString().split(" ").first,
        textScaleFactor: AppStyle.getScaleFactor(context),
      ),
      onPressed: () async {
        DateTime picked = await showDatePicker(
            context: context,
            initialDate: DateTime.now(),
            firstDate: from ?? DateTime.now(),
            lastDate: to ?? DateTime.now().add(Duration(days: 365))
        );
        onDatePicker(picked);
      },
    );
  }
}
