layout_settings.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:math' as math;
  2. import 'package:flutter/rendering.dart';
  3. import 'package:flutter/widgets.dart';
  4. // layout defaults
  5. const Duration _kPageScrollDuration = Duration(milliseconds: 200);
  6. const double _kDayPickerRowHeight = 42.0;
  7. const int _kMaxDayPickerRowCount = 6; // A 31 day month that starts on Saturday.
  8. const double _kMonthPickerPortraitWidth = 330.0;
  9. const EdgeInsetsGeometry _kContentPadding =
  10. EdgeInsets.symmetric(horizontal: 8.0);
  11. /// Settings for the layout of the [DayPicker], [WeekPicker], [RangePicker]
  12. /// and [MonthPicker].
  13. class DatePickerLayoutSettings {
  14. /// Duration for scroll to previous or next page.
  15. final Duration pagesScrollDuration;
  16. /// Determines the scroll physics of a date picker widget.
  17. ///
  18. /// Can be null. In this case default physics for [ScrollView] will be used.
  19. final ScrollPhysics? scrollPhysics;
  20. /// Height of the one row in picker including headers.
  21. ///
  22. /// Default is [_kDayPickerRowHeight].
  23. final double dayPickerRowHeight;
  24. /// Width of the day based pickers.
  25. final double monthPickerPortraitWidth;
  26. /// Width of the day based pickers.
  27. final double yearPickerPortraitWidth;
  28. ///
  29. final int maxDayPickerRowCount;
  30. /// Padding for the entire picker.
  31. final EdgeInsetsGeometry contentPadding;
  32. /// If the first dates from the next month should be shown
  33. /// to complete last week of the selected month.
  34. ///
  35. /// false by default.
  36. final bool showNextMonthStart;
  37. /// If the last dates from the previous month should be shown
  38. /// to complete first week of the selected month.
  39. ///
  40. /// false by default.
  41. final bool showPrevMonthEnd;
  42. /// Hide Month navigation row
  43. /// false by default.
  44. final bool hideMonthNavigationRow;
  45. /// Grid delegate for the picker according to [dayPickerRowHeight] and
  46. /// [maxDayPickerRowCount].
  47. SliverGridDelegate get dayPickerGridDelegate =>
  48. _DayPickerGridDelegate(dayPickerRowHeight, maxDayPickerRowCount);
  49. /// Maximum height of the day based picker according to [dayPickerRowHeight]
  50. /// and [maxDayPickerRowCount].
  51. ///
  52. /// Two extra rows:
  53. /// one for the day-of-week header and one for the month header.
  54. double get maxDayPickerHeight =>
  55. dayPickerRowHeight * (maxDayPickerRowCount + 2);
  56. /// Creates layout settings for the date picker.
  57. ///
  58. /// Usually used in [DayPicker], [WeekPicker], [RangePicker]
  59. /// and [MonthPicker].
  60. const DatePickerLayoutSettings({
  61. this.pagesScrollDuration = _kPageScrollDuration,
  62. this.dayPickerRowHeight = _kDayPickerRowHeight,
  63. this.monthPickerPortraitWidth = _kMonthPickerPortraitWidth,
  64. this.yearPickerPortraitWidth = _kMonthPickerPortraitWidth,
  65. this.maxDayPickerRowCount = _kMaxDayPickerRowCount,
  66. this.contentPadding = _kContentPadding,
  67. this.showNextMonthStart = false,
  68. this.showPrevMonthEnd = false,
  69. this.hideMonthNavigationRow = false,
  70. this.scrollPhysics,
  71. });
  72. }
  73. class _DayPickerGridDelegate extends SliverGridDelegate {
  74. final double _dayPickerRowHeight;
  75. final int _maxDayPickerRowCount;
  76. const _DayPickerGridDelegate(
  77. this._dayPickerRowHeight, this._maxDayPickerRowCount);
  78. @override
  79. SliverGridLayout getLayout(SliverConstraints constraints) {
  80. const int columnCount = DateTime.daysPerWeek;
  81. final double tileWidth = constraints.crossAxisExtent / columnCount;
  82. final double tileHeight = math.min(_dayPickerRowHeight,
  83. constraints.viewportMainAxisExtent / (_maxDayPickerRowCount + 1));
  84. return SliverGridRegularTileLayout(
  85. crossAxisCount: columnCount,
  86. mainAxisStride: tileHeight,
  87. crossAxisStride: tileWidth,
  88. childMainAxisExtent: tileHeight,
  89. childCrossAxisExtent: tileWidth,
  90. reverseCrossAxis: axisDirectionIsReversed(constraints.crossAxisDirection),
  91. );
  92. }
  93. @override
  94. bool shouldRelayout(SliverGridDelegate oldDelegate) => false;
  95. }