|
1 | | -# header-customization-date-range-picker-flutter |
2 | | -How to customize the header view of the flutter date range picker? |
| 1 | +## How to customize the header view of the Flutter date range picker? |
| 2 | + |
| 3 | +In the flutter date range picker, you can add your custom header, and it can be achieved by hiding the default header and placing your custom header in the date range picker. |
| 4 | + |
| 5 | +## Step 1: |
| 6 | +Set the `headerHeight` property value to 0 to hide the default header. Please find the following image for date picker without header. |
| 7 | + |
| 8 | +```xml |
| 9 | +body: Column( |
| 10 | + children: <Widget>[ |
| 11 | + Card( |
| 12 | + margin: const EdgeInsets.fromLTRB(50, 0, 50, 50), |
| 13 | + child: SfDateRangePicker( |
| 14 | + controller: _controller, |
| 15 | + view: DateRangePickerView.month, |
| 16 | + headerHeight: 0, |
| 17 | + ) |
| 18 | + ], |
| 19 | +); |
| 20 | +``` |
| 21 | +<img alt="header height" src="http://www.syncfusion.com/uploads/user/kb/flut/flut-864/flut-864_img1.png" width="230" height="250" />| |
| 22 | + |
| 23 | +## Step 2: |
| 24 | +For design own custom header using the Row widget inside the Column widget for the header customization. |
| 25 | + |
| 26 | +```xml |
| 27 | +Row( |
| 28 | + children: <Widget>[ |
| 29 | + Container( |
| 30 | + height: cellWidth, |
| 31 | + width: cellWidth + 10, |
| 32 | + ), |
| 33 | + Container( |
| 34 | + width: cellWidth, |
| 35 | + height: cellWidth, |
| 36 | + color: Color(0xFFfa697c), |
| 37 | + child: IconButton( |
| 38 | + icon: Icon(Icons.arrow_left), |
| 39 | + color: Colors.white, |
| 40 | + iconSize: 20, |
| 41 | + highlightColor: Colors.lightGreen, |
| 42 | + onPressed: () { |
| 43 | + setState(() { |
| 44 | + _controller.backward(); |
| 45 | + }); |
| 46 | + }, |
| 47 | + )), |
| 48 | + Container( |
| 49 | + color: Color(0xFFfa697c), |
| 50 | + height: cellWidth, |
| 51 | + width: cellWidth * 4.5, |
| 52 | + child: Text(headerString, |
| 53 | + textAlign: TextAlign.center, |
| 54 | + style: TextStyle( |
| 55 | + fontSize: 25, color: Colors.white, height: 1.4)), |
| 56 | + ), |
| 57 | + Container( |
| 58 | + width: cellWidth, |
| 59 | + height: cellWidth, |
| 60 | + color: Color(0xFFfa697c), |
| 61 | + child: IconButton( |
| 62 | + icon: Icon(Icons.arrow_right), |
| 63 | + color: Colors.white, |
| 64 | + highlightColor: Colors.lightGreen, |
| 65 | + onPressed: () { |
| 66 | + setState(() { |
| 67 | + _controller.forward(); |
| 68 | + }); |
| 69 | + }, |
| 70 | + )), |
| 71 | + Container( |
| 72 | + height: cellWidth, |
| 73 | + width: cellWidth, |
| 74 | + ) |
| 75 | + ], |
| 76 | +), |
| 77 | +``` |
| 78 | +## Step 3: |
| 79 | +Then, add the custom header in the date range picker. |
| 80 | + |
| 81 | +```xml |
| 82 | +Card( |
| 83 | + margin: const EdgeInsets.fromLTRB(50, 0, 50, 50), |
| 84 | + child: SfDateRangePicker( |
| 85 | + controller: _controller, |
| 86 | + view: DateRangePickerView.month, |
| 87 | + headerHeight: 0, |
| 88 | + onViewChanged: viewChanged, |
| 89 | + monthViewSettings: DateRangePickerMonthViewSettings( |
| 90 | + showTrailingAndLeadingDates: true, |
| 91 | + viewHeaderStyle: DateRangePickerViewHeaderStyle( |
| 92 | + backgroundColor: Color(0xFFfcc169))), |
| 93 | + monthCellStyle: DateRangePickerMonthCellStyle( |
| 94 | + cellDecoration: BoxDecoration(color: Color(0xFF6fb98f)), |
| 95 | + leadingDatesDecoration: |
| 96 | + BoxDecoration(color: Color(0xFF6fb98f)), |
| 97 | + trailingDatesDecoration: |
| 98 | + BoxDecoration(color: Color(0xFF6fb98f)))), |
| 99 | +) |
| 100 | +``` |
| 101 | + |
| 102 | +## Step 4: |
| 103 | +Using the `onViewChanged` callback of the date picker, you can get the mid date of the visible date range and assign it to the header string. |
| 104 | + |
| 105 | +```xml |
| 106 | +void viewChanged(DateRangePickerViewChangedArgs args) { |
| 107 | + _startDate = (args.visibleDateRange.startDate |
| 108 | + .difference(args.visibleDateRange.endDate) |
| 109 | + .inDays); |
| 110 | + var middleDate = (_startDate ~/ 2).toInt(); |
| 111 | + midDate = args.visibleDateRange.startDate.add(Duration(days: middleDate)); |
| 112 | + headerString = DateFormat('MMMM yyyy').format(midDate).toString(); |
| 113 | + SchedulerBinding.instance.addPostFrameCallback((duration) { |
| 114 | + setState(() {}); |
| 115 | + }); |
| 116 | +} |
| 117 | +``` |
| 118 | +**[View document in Syncfusion Flutter Knowledge base](https://www.syncfusion.com/kb/11427/how-to-customize-the-header-view-of-the-flutter-date-range-picker)** |
| 119 | + |
| 120 | +**Screenshot** |
| 121 | + |
| 122 | +<img alt="Custom header" src="http://www.syncfusion.com/uploads/user/kb/flut/flut-864/flut-864_img2.png" width="230" height="250" />| |
0 commit comments