You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/views/widgets/date_and_time/date_picker.dart

129 lines
5.7 KiB
Dart

3 years ago
import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
3 years ago
class ADatePicker extends StatelessWidget {
final DateTime? date; // Nullable
final DateTime? from; // Nullable
final DateTime? to; // Nullable
final String label;
1 year ago
final bool hideShadow;
final String? hint; // Nullable
final Function(DateTime)? onDatePicker; // Nullable
final bool enable;
final bool withBorder;
final bool withIcon;
final Color? backgroundColor; // Nullable
final bool formatDateWithTime;
final double? height; // Nullable
3 years ago
const ADatePicker({
Key? key,
required this.label,
this.withBorder = true,
1 year ago
this.height,
this.backgroundColor,
this.hideShadow = false,
1 year ago
this.hint,
this.date,
this.withIcon = true,
this.formatDateWithTime = false,
this.onDatePicker,
this.from,
this.to,
this.enable = true,
}) : super(key: key);
3 years ago
@override
Widget build(BuildContext context) {
return Container(
height: height,
decoration: BoxDecoration(
color: backgroundColor ??
(context.isDark && !enable
? AppColor.neutral50
: !enable
? AppColor.neutral40
: AppColor.background(context)),
borderRadius: BorderRadius.circular(10),
border: withBorder ? Border.all(width: 1, color: Theme.of(context).scaffoldBackgroundColor) : const Border(),
boxShadow: hideShadow ? null : [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)],
3 years ago
),
padding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth, vertical: 8.toScreenHeight),
child: Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
label.tinyFont(context),
(date == null
? hint ?? context.translation.pickADate
: (formatDateWithTime
? date?.toIso8601String().toFirstActionFormat // Use null-aware operator
: (date?.toIso8601String().split("T").first ?? context.translation.pickADate)))!
.bodyText(context)
.custom(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
],
).expanded,
// enable ? 16.width : const Spacer(),
withIcon ? "calender".toSvgAsset(width: 20, color: context.isDark ? AppColor.neutral10 : null) : const SizedBox(),
],
3 years ago
),
).onPress(enable
? () async {
final DateTime? picked = await showDatePicker(
// Nullable DateTime
context: context,
initialDate: DateTime.now(),
firstDate: from ?? DateTime.now(),
lastDate: to ?? DateTime.now(),
builder: (BuildContext context, Widget? child) {
final ThemeData currentTheme = Theme.of(context);
return Theme(
data: currentTheme.copyWith(
datePickerTheme: DatePickerThemeData(
headerBackgroundColor: AppColor.primary10,
headerForegroundColor: Colors.white,
backgroundColor: currentTheme.colorScheme.surface,
dayForegroundColor: MaterialStateColor.resolveWith((states) {
if (states.contains(MaterialState.selected)) return Colors.white;
if (states.contains(MaterialState.disabled)) return Colors.grey.withOpacity(0.6);
return currentTheme.colorScheme.onSurface;
}),
dayBackgroundColor: MaterialStateColor.resolveWith((states) => states.contains(MaterialState.selected) ? AppColor.primary10 : Colors.transparent),
yearForegroundColor: MaterialStateColor.resolveWith((states) {
if (states.contains(MaterialState.selected)) return Colors.white;
if (states.contains(MaterialState.disabled)) return Colors.grey.withOpacity(0.6);
return currentTheme.colorScheme.onSurface;
}),
yearBackgroundColor: MaterialStateColor.resolveWith((states) => states.contains(MaterialState.selected) ? AppColor.primary10 : Colors.transparent),
// todayForegroundColor: MaterialStateColor.resolveWith((states) => AppColor.primary10),
// todayBorder: BorderSide(color: AppColor.primary10.withOpacity(0.7)),
dividerColor: Colors.grey.withOpacity(0.2),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
inputDecorationTheme: const InputDecorationTheme(
focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: AppColor.primary10, width: 2)),
),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(foregroundColor: AppColor.primary10),
),
iconTheme: IconThemeData(color: AppColor.primary10.withOpacity(0.7)),
),
child: child!,
);
},
);
if (picked != null && onDatePicker != null) {
onDatePicker!(picked); // Use null-aware operator
}
}
: null);
3 years ago
}
}