Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ class ExampleApp extends StatelessWidget {
),
),
),
SizedBox(
width: buttonWidth,
child: ElevatedButton(
onPressed: () {
_openYearDatePicker(context);
},
child: Text('Year Picker', textAlign: TextAlign.center),
),
),
SizedBox(
width: buttonWidth,
child: ElevatedButton(
Expand Down Expand Up @@ -311,6 +320,39 @@ class ExampleApp extends StatelessWidget {
).show(context);
}

void _openYearDatePicker(BuildContext context) {
BottomPicker.year(
headerBuilder: (context) {
return Row(
children: [
Text(
'Set your Birthday Year',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
color: Colors.blue,
),
),
],
);
},
initialDateTime: DateTime(1996),
maxDateTime: DateTime(1998),
minDateTime: DateTime(1980),
onChange: (index) {
print(index);
},
onSubmit: (index) {
print(index);
Navigator.pop(context);
},
onDismiss: (p0) {
print(p0);
},
bottomPickerTheme: BottomPickerTheme.plumPlate,
).show(context);
}

void _openMonthYearPicker(BuildContext context) {
BottomPicker.monthYear(
headerBuilder: (context) {
Expand Down
89 changes: 68 additions & 21 deletions lib/bottom_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:bottom_picker/widgets/date_picker.dart';
import 'package:bottom_picker/widgets/range_picker.dart';
import 'package:bottom_picker/widgets/simple_picker.dart';
import 'package:bottom_picker/widgets/time_picker.dart';
import 'package:bottom_picker/widgets/year_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -139,6 +140,39 @@ class BottomPicker extends StatefulWidget {
assertInitialValues();
}

/// Creates a bottom picker for selecting a year.
BottomPicker.year({
super.key,
this.dismissable = false,
this.onChange,
this.onSubmit,
this.onDismiss,
this.bottomPickerTheme = BottomPickerTheme.blue,
this.gradientColors,
this.initialDateTime,
this.minDateTime,
this.maxDateTime,
this.buttonPadding,
this.buttonWidth,
this.buttonSingleColor,
this.backgroundColor = Colors.white,
this.pickerThemeData,
this.buttonAlignment = MainAxisAlignment.center,
this.height,
this.displaySubmitButton = true,
this.buttonContent,
this.buttonStyle,
this.itemExtent = 35.0,
this.headerBuilder,
}) {
datePickerMode = CupertinoDatePickerMode.date;
bottomPickerType = BottomPickerType.year;
use24hFormat = false;
onRangeDateSubmitPressed = null;
displayCloseIcon = false;
assertInitialValues();
}

BottomPicker.monthYear({
super.key,
@Deprecated('should use headerBuilder instead') this.pickerTitle,
Expand Down Expand Up @@ -487,21 +521,21 @@ class BottomPicker extends StatefulWidget {
}

/// Bottom picker title widget
final Widget? pickerTitle;
Widget? pickerTitle;

/// Renders the header component of the bottom picker
final Widget Function(BuildContext context)? headerBuilder;

///Bottom picker description widget
final Widget? pickerDescription;
Widget? pickerDescription;

///The padding applied on the title
///by default it is set with zero values
final EdgeInsetsGeometry titlePadding;
EdgeInsetsGeometry? titlePadding;

///Title widget alignment inside the stack
///by default the title will be aligned left/right depends on `layoutOrientation`
final Alignment? titleAlignment;
Alignment? titleAlignment;

///defines whether the bottom picker is dismissable or not
///by default it's set to false
Expand Down Expand Up @@ -532,7 +566,7 @@ class BottomPicker extends StatefulWidget {

///Invoked when clicking on the close button
///
final Function? onCloseButtonPressed;
Function? onCloseButtonPressed;

///set the theme of the bottom picker (the button theme)
///possible values
Expand Down Expand Up @@ -617,11 +651,11 @@ class BottomPicker extends StatefulWidget {

///date order applied on date picker or date time picker
///by default it's YYYY/MM/DD
late DatePickerDateOrder? dateOrder;
DatePickerDateOrder? dateOrder;

///the picker text style applied on all types of bottom picker
///by default `TextStyle(fontSize: 14)`
final TextStyle pickerTextStyle;
TextStyle? pickerTextStyle;

/// The picker theme data
final CupertinoTextThemeData? pickerThemeData;
Expand All @@ -632,29 +666,29 @@ class BottomPicker extends StatefulWidget {

///indicate whether the close icon will be rendred or not
/// by default `displayCloseIcon = true`
final bool displayCloseIcon;
bool? displayCloseIcon;

/// Renders the close widget if it's null and [displayCloseIcon] is true
/// the default close icon is rendered.
/// Note if closeWidget is provided onClosePressed won't be triggered
/// since you need to handle all actions on the provided widget.
final Widget? closeWidget;
Widget? closeWidget;

///the close icon color
///by default `closeIconColor = Colors.black`
final Color closeIconColor;
Color? closeIconColor;

///the close icon size
///by default `closeIconSize = 20`
final double closeIconSize;
double? closeIconSize;

///the layout orientation of the bottom picker
///by default the orientation is set to LTR
///```
///LAYOUT_ORIENTATION.ltr,
///LAYOUT_ORIENTATION.rtl
///```
final TextDirection layoutOrientation;
TextDirection? layoutOrientation;

///THe alignment of the bottom picker button
///by default it's `MainAxisAlignment.center`
Expand Down Expand Up @@ -760,10 +794,10 @@ class BottomPicker extends StatefulWidget {
/// when the submit button is pressed.
///
/// By default closeOnSubmit = true.
final bool closeOnSubmit;
bool? closeOnSubmit;

/// The datepicker calendar type
final List<int> calendarDays;
List<int> calendarDays = CupertinoDatePickerWidget.fullWeek;

///display the bottom picker popup
///[context] the app context to display the popup
Expand Down Expand Up @@ -873,13 +907,13 @@ class BottomPickerState extends State<BottomPicker> {
horizontal: 20,
),
child: Directionality(
textDirection: widget.layoutOrientation,
textDirection: widget.layoutOrientation ?? TextDirection.ltr,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: widget.headerBuilder == null
? widget.titlePadding
? widget.titlePadding ?? const EdgeInsets.all(0)
: EdgeInsetsGeometry.zero,
child: Row(
children: [
Expand All @@ -893,7 +927,7 @@ class BottomPickerState extends State<BottomPicker> {
widget.pickerTitle != null)
Expanded(child: widget.pickerTitle!),
if (widget.headerBuilder == null &&
widget.displayCloseIcon)
(widget.displayCloseIcon ?? true))
widget.closeWidget ??
CloseIcon(
onPress: _closeBottomPicker,
Expand Down Expand Up @@ -974,8 +1008,20 @@ class BottomPickerState extends State<BottomPicker> {
showTimeSeparator: widget.showTimeSeparator,
pickerThemeData: widget.pickerThemeData,
)
: widget.bottomPickerType ==
BottomPickerType.rangeTime
: widget.bottomPickerType == BottomPickerType.year
? BottomYearDatePicker(
initialDateTime: widget.initialDateTime,
maxDateTime: widget.maxDateTime,
minDateTime: widget.minDateTime,
onDateChanged: (DateTime date) {
selectedDateTime = date;
widget.onChange?.call(date);
},
itemExtent: widget.itemExtent,
pickerThemeData: widget.pickerThemeData,
)
: widget.bottomPickerType ==
BottomPickerType.rangeTime
? RangePicker(
mode: CupertinoDatePickerMode.time,
use24hFormat: widget.use24hFormat,
Expand Down Expand Up @@ -1050,7 +1096,8 @@ class BottomPickerState extends State<BottomPicker> {
);
} else if (widget.bottomPickerType ==
BottomPickerType.dateTime ||
widget.bottomPickerType == BottomPickerType.time) {
widget.bottomPickerType == BottomPickerType.time ||
widget.bottomPickerType == BottomPickerType.year) {
widget.onSubmit?.call(selectedDateTime);
} else if (widget.bottomPickerType ==
BottomPickerType.timer) {
Expand All @@ -1059,7 +1106,7 @@ class BottomPickerState extends State<BottomPicker> {
widget.onSubmit?.call(selectedItemIndex);
}

if (widget.closeOnSubmit) {
if (widget.closeOnSubmit ?? false) {
Navigator.pop(context);
}
},
Expand Down
1 change: 1 addition & 0 deletions lib/resources/arrays.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enum BottomPickerType {
time,
timer,
dateTime,
year,
rangeDate,
rangeTime,
}
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/close_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:flutter/material.dart';

class CloseIcon extends StatelessWidget {
final Function() onPress;
final Color iconColor;
final double closeIconSize;
final Color? iconColor;
final double? closeIconSize;

const CloseIcon({
super.key,
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DatePicker extends StatelessWidget {
final int minuteInterval;
final bool use24hFormat;
final DatePickerDateOrder? dateOrder;
final TextStyle textStyle;
final TextStyle? textStyle;
final double? itemExtent;
final bool showTimeSeparator;
final List<int> calendarDays;
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/range_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RangePicker extends StatefulWidget {
final DateTime? minFirstDateTime;
final DateTime? minSecondDateTime;
final DatePickerDateOrder? dateOrder;
final TextStyle textStyle;
final TextStyle? textStyle;
final CupertinoDatePickerMode mode;
final bool use24hFormat;
final int? minuteInterval;
Expand Down
11 changes: 6 additions & 5 deletions lib/widgets/simple_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SimplePicker extends StatelessWidget {
final int selectedItemIndex;
final Function(int)? onChange;
final List<Widget> items;
final TextStyle textStyle;
final TextStyle? textStyle;
final double itemExtent;
final Widget? selectionOverlay;
final CupertinoTextThemeData? pickerThemeData;
Expand All @@ -17,7 +17,7 @@ class SimplePicker extends StatelessWidget {
required this.items,
required this.onChange,
required this.selectedItemIndex,
required this.textStyle,
this.textStyle,
required this.itemExtent,
this.selectionOverlay,
this.pickerThemeData,
Expand All @@ -28,9 +28,10 @@ class SimplePicker extends StatelessWidget {
if (!kIsWeb && (Platform.isIOS || Platform.isAndroid)) {
return CupertinoTheme(
data: CupertinoThemeData(
textTheme: pickerThemeData ?? CupertinoTextThemeData(
pickerTextStyle: textStyle,
),
textTheme: pickerThemeData ??
CupertinoTextThemeData(
pickerTextStyle: textStyle,
),
),
child: CupertinoPicker(
offAxisFraction: 2.0,
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/time_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
class TimePicker extends StatelessWidget {
final CupertinoTimerPickerMode mode;
final int minuteInterval;
final TextStyle textStyle;
final TextStyle? textStyle;
final double itemExtent;
final Function(Duration) onChange;
final Duration? initialDuration;
Expand Down
Loading