base_project
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class ReusableDatePickerField extends StatelessWidget {
|
||||
final String label;
|
||||
final String? initialDate;
|
||||
final TextEditingController controller;
|
||||
final Function(String?)? onSaved;
|
||||
|
||||
const ReusableDatePickerField({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.controller,
|
||||
this.initialDate,
|
||||
required this.onSaved,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextFormField(
|
||||
initialValue: initialDate,
|
||||
controller: controller,
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
border: const OutlineInputBorder(),
|
||||
contentPadding:
|
||||
const EdgeInsets.symmetric(vertical: 16.0, horizontal: 12.0),
|
||||
suffixIcon: const Icon(Icons.calendar_today),
|
||||
),
|
||||
readOnly: true,
|
||||
onTap: () async {
|
||||
DateTime? selectedDate = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: DateTime.now(),
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: DateTime(2101),
|
||||
);
|
||||
controller.text = selectedDate != null
|
||||
? DateFormat('yyyy-MM-dd').format(selectedDate!)
|
||||
: '';
|
||||
},
|
||||
onSaved: onSaved,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class ReusableDateTimePickerField extends StatelessWidget {
|
||||
final String label;
|
||||
final String? initialDateTime;
|
||||
final TextEditingController controller;
|
||||
final Function(String?)? onSaved;
|
||||
|
||||
const ReusableDateTimePickerField({super.key,
|
||||
required this.label,
|
||||
required this.controller,
|
||||
this.initialDateTime,
|
||||
required this.onSaved,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: TextFormField(
|
||||
initialValue: initialDateTime,
|
||||
controller: controller,
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
border: const OutlineInputBorder(),
|
||||
contentPadding:
|
||||
const EdgeInsets.symmetric(vertical: 16.0, horizontal: 12.0),
|
||||
suffixIcon: const Icon(Icons.event),
|
||||
),
|
||||
readOnly: true,
|
||||
onTap: () async {
|
||||
DateTime? selectedDateTime = await showDateTimePicker(
|
||||
context: context,
|
||||
initialDateTime: DateTime.now(),
|
||||
);
|
||||
if (selectedDateTime != null) {
|
||||
controller.text =
|
||||
DateFormat('yyyy-MM-dd HH:mm').format(selectedDateTime);
|
||||
}
|
||||
},
|
||||
onSaved: onSaved,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<DateTime?> showDateTimePicker({
|
||||
required BuildContext context,
|
||||
required DateTime initialDateTime,
|
||||
}) async {
|
||||
// Custom date-time picker implementation or package usage
|
||||
// This is a placeholder for demonstration purposes.
|
||||
return await showDatePicker(
|
||||
context: context,
|
||||
initialDate: initialDateTime,
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: DateTime(2101),
|
||||
).then((date) {
|
||||
if (date != null) {
|
||||
return showTimePicker(
|
||||
context: context,
|
||||
initialTime: TimeOfDay.fromDateTime(initialDateTime),
|
||||
).then((time) {
|
||||
if (time != null) {
|
||||
return DateTime(
|
||||
date.year, date.month, date.day, time.hour, time.minute);
|
||||
}
|
||||
return date;
|
||||
});
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ReusableDropdownField extends StatelessWidget {
|
||||
final String label;
|
||||
final List<Map<String, dynamic>> options;
|
||||
final String? value;
|
||||
final String valueField; // ID key (dynamic)
|
||||
final String uiField; // Name key (dynamic)
|
||||
final void Function(String?)? onChanged;
|
||||
final void Function(String?)? onSaved;
|
||||
|
||||
const ReusableDropdownField({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.options,
|
||||
required this.valueField, // Dynamic ID field
|
||||
required this.uiField, // Dynamic Name field
|
||||
this.value,
|
||||
this.onChanged,
|
||||
this.onSaved,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: DropdownButtonFormField<String>(
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
labelStyle: const TextStyle(color: Colors.deepPurple),
|
||||
contentPadding:
|
||||
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
borderSide: const BorderSide(color: Colors.deepPurple, width: 2),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
borderSide: const BorderSide(color: Colors.deepPurple, width: 2),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
borderSide:
|
||||
const BorderSide(color: Colors.deepPurpleAccent, width: 2),
|
||||
),
|
||||
),
|
||||
value: (value != null && value!.isNotEmpty) ? value : null,
|
||||
items: [
|
||||
const DropdownMenuItem<String>(
|
||||
value: '',
|
||||
child: Text('Select an option'),
|
||||
),
|
||||
...options.map<DropdownMenuItem<String>>(
|
||||
(item) => DropdownMenuItem<String>(
|
||||
value: item[valueField].toString(),
|
||||
child: Text(
|
||||
item[uiField].toString(),
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
onChanged: onChanged,
|
||||
onSaved: onSaved,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please select a $label';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class ReusableTextField extends StatelessWidget {
|
||||
final String label;
|
||||
final TextEditingController? controller;
|
||||
final TextInputType keyboardType;
|
||||
final bool obscureText;
|
||||
final int maxLines;
|
||||
final List<TextInputFormatter>? inputFormatters;
|
||||
final FormFieldValidator<String>? validator;
|
||||
final Function(DateTime?)? onDateTimeSelected; // Callback for DateTime picker
|
||||
final Function(String?)? onSaved;
|
||||
final Function(String?)? onChanged;
|
||||
|
||||
final String? initialValue;
|
||||
|
||||
const ReusableTextField({
|
||||
super.key,
|
||||
required this.label,
|
||||
this.controller,
|
||||
this.keyboardType = TextInputType.text,
|
||||
this.obscureText = false,
|
||||
this.maxLines = 1,
|
||||
this.inputFormatters,
|
||||
this.validator,
|
||||
this.onDateTimeSelected,
|
||||
this.onSaved,
|
||||
this.onChanged,
|
||||
this.initialValue, // Added callback for DateTime picker
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: TextFormField(
|
||||
initialValue: initialValue,
|
||||
onSaved: onSaved,
|
||||
onChanged: onChanged,
|
||||
controller: controller,
|
||||
keyboardType: keyboardType,
|
||||
obscureText: obscureText,
|
||||
maxLines: maxLines,
|
||||
inputFormatters: inputFormatters,
|
||||
validator: validator,
|
||||
onTap: () async {
|
||||
if (keyboardType == TextInputType.datetime &&
|
||||
onDateTimeSelected != null) {
|
||||
DateTime? dateTime = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: DateTime.now(),
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: DateTime(2100),
|
||||
);
|
||||
// Agar user cancel kare, to current date set ho jaye
|
||||
dateTime ??= DateTime.now();
|
||||
|
||||
TimeOfDay? timeOfDay = await showTimePicker(
|
||||
context: context,
|
||||
initialTime: TimeOfDay.now(),
|
||||
);
|
||||
if (timeOfDay != null) {
|
||||
dateTime = DateTime(dateTime.year, dateTime.month, dateTime.day,
|
||||
timeOfDay.hour, timeOfDay.minute);
|
||||
onDateTimeSelected!(dateTime);
|
||||
}
|
||||
}
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user