Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 9031085

Browse files
authored
Merge pull request #339 from IIzzaya/dev
[Clean] Picker Widget
2 parents 67d0957 + 1586e1d commit 9031085

File tree

4 files changed

+40
-178
lines changed

4 files changed

+40
-178
lines changed

Runtime/cupertino/date_picker.cs

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@ public static List<Widget> listGenerate(int count, listGenerateDelegate func) {
3838

3939
public class _DatePickerLayoutDelegate : MultiChildLayoutDelegate {
4040
public _DatePickerLayoutDelegate(
41-
List<float> columnWidths,
42-
int textDirectionFactor
41+
List<float> columnWidths
4342
) {
4443
D.assert(columnWidths != null);
4544
this.columnWidths = columnWidths;
46-
this.textDirectionFactor = textDirectionFactor;
4745
}
4846

4947
public readonly List<float> columnWidths;
50-
public readonly int textDirectionFactor;
5148

5249
public override void performLayout(Size size) {
5350
float remainingWidth = size.width;
@@ -57,21 +54,19 @@ public override void performLayout(Size size) {
5754

5855
float currentHorizontalOffset = 0.0f;
5956
for (int i = 0; i < this.columnWidths.Count; i++) {
60-
int index = this.textDirectionFactor == 1 ? i : this.columnWidths.Count - i - 1;
61-
float childWidth = this.columnWidths[index] + CupertinoDatePickerUtils._kDatePickerPadSize * 2;
62-
if (index == 0 || index == this.columnWidths.Count - 1) {
57+
float childWidth = this.columnWidths[i] + CupertinoDatePickerUtils._kDatePickerPadSize * 2;
58+
if (i == 0 || i == this.columnWidths.Count - 1) {
6359
childWidth += remainingWidth / 2;
6460
}
6561

66-
this.layoutChild(index, BoxConstraints.tight(new Size(childWidth, size.height)));
67-
this.positionChild(index, new Offset(currentHorizontalOffset, 0.0f));
62+
this.layoutChild(i, BoxConstraints.tight(new Size(childWidth, size.height)));
63+
this.positionChild(i, new Offset(currentHorizontalOffset, 0.0f));
6864
currentHorizontalOffset += childWidth;
6965
}
7066
}
7167

7268
public override bool shouldRelayout(MultiChildLayoutDelegate oldDelegate) {
73-
return this.columnWidths != ((_DatePickerLayoutDelegate) oldDelegate).columnWidths
74-
|| this.textDirectionFactor != ((_DatePickerLayoutDelegate) oldDelegate).textDirectionFactor;
69+
return this.columnWidths != ((_DatePickerLayoutDelegate) oldDelegate).columnWidths;
7570
}
7671
}
7772

@@ -231,8 +226,7 @@ BuildContext context
231226
text: new TextSpan(
232227
style: DefaultTextStyle.of(context).style,
233228
text: longestText
234-
),
235-
textDirection: Directionality.of(context)
229+
)
236230
);
237231

238232

@@ -244,7 +238,6 @@ BuildContext context
244238
delegate Widget _ColumnBuilder(float offAxisFraction, TransitionBuilder itemPositioningBuilder);
245239

246240
class _CupertinoDatePickerDateTimeState : State<CupertinoDatePicker> {
247-
public int textDirectionFactor;
248241
public CupertinoLocalizations localizations;
249242
public Alignment alignCenterLeft;
250243
public Alignment alignCenterRight;
@@ -288,11 +281,10 @@ public override void didUpdateWidget(StatefulWidget oldWidget) {
288281

289282
public override void didChangeDependencies() {
290283
base.didChangeDependencies();
291-
this.textDirectionFactor = Directionality.of(this.context) == TextDirection.ltr ? 1 : -1;
292284

293285
this.localizations = CupertinoLocalizations.of(this.context);
294-
this.alignCenterLeft = this.textDirectionFactor == 1 ? Alignment.centerLeft : Alignment.centerRight;
295-
this.alignCenterRight = this.textDirectionFactor == 1 ? Alignment.centerRight : Alignment.centerLeft;
286+
this.alignCenterLeft = Alignment.centerLeft;
287+
this.alignCenterRight = Alignment.centerRight;
296288
estimatedColumnWidths.Clear();
297289
}
298290

@@ -486,21 +478,17 @@ public override Widget build(BuildContext context) {
486478
var _i = i;
487479
float offAxisFraction = 0.0f;
488480
if (_i == 0) {
489-
offAxisFraction = -0.5f * this.textDirectionFactor;
481+
offAxisFraction = -0.5f;
490482
}
491483
else if (_i >= 2 || columnWidths.Count == 2) {
492-
offAxisFraction = 0.5f * this.textDirectionFactor;
484+
offAxisFraction = 0.5f;
493485
}
494486

495487
EdgeInsets padding = EdgeInsets.only(right: CupertinoDatePickerUtils._kDatePickerPadSize);
496488
if (_i == columnWidths.Count - 1) {
497489
padding = padding.flipped;
498490
}
499491

500-
if (this.textDirectionFactor == -1) {
501-
padding = padding.flipped;
502-
}
503-
504492
pickers.Add(new LayoutId(
505493
id: _i,
506494
child: pickerBuilders[_i](
@@ -532,8 +520,7 @@ public override Widget build(BuildContext context) {
532520
style: CupertinoDatePickerUtils._kDefaultPickerTextStyle,
533521
child: new CustomMultiChildLayout(
534522
layoutDelegate: new _DatePickerLayoutDelegate(
535-
columnWidths: columnWidths,
536-
textDirectionFactor: this.textDirectionFactor
523+
columnWidths: columnWidths
537524
),
538525
children: pickers
539526
)
@@ -543,7 +530,6 @@ public override Widget build(BuildContext context) {
543530
}
544531

545532
class _CupertinoDatePickerDateState : State<CupertinoDatePicker> {
546-
int textDirectionFactor;
547533
CupertinoLocalizations localizations;
548534

549535
Alignment alignCenterLeft;
@@ -566,10 +552,9 @@ public override void initState() {
566552

567553
public override void didChangeDependencies() {
568554
base.didChangeDependencies();
569-
this.textDirectionFactor = Directionality.of(this.context) == TextDirection.ltr ? 1 : -1;
570555
this.localizations = CupertinoLocalizations.of(this.context);
571-
this.alignCenterLeft = this.textDirectionFactor == 1 ? Alignment.centerLeft : Alignment.centerRight;
572-
this.alignCenterRight = this.textDirectionFactor == 1 ? Alignment.centerRight : Alignment.centerLeft;
556+
this.alignCenterLeft = Alignment.centerLeft;
557+
this.alignCenterRight = Alignment.centerRight;
573558
this.estimatedColumnWidths[(int) _PickerColumnType.dayOfMonth] = CupertinoDatePicker._getColumnWidth(
574559
_PickerColumnType.dayOfMonth, this.localizations, this.context);
575560

@@ -747,11 +732,8 @@ public override Widget build(BuildContext context) {
747732
List<Widget> pickers = new List<Widget>();
748733
for (int i = 0; i < columnWidths.Count; i++) {
749734
var _i = i;
750-
float offAxisFraction = (_i - 1) * 0.3f * this.textDirectionFactor;
735+
float offAxisFraction = (_i - 1) * 0.3f;
751736
EdgeInsets padding = EdgeInsets.only(right: CupertinoDatePickerUtils._kDatePickerPadSize);
752-
if (this.textDirectionFactor == -1) {
753-
padding = EdgeInsets.only(left: CupertinoDatePickerUtils._kDatePickerPadSize);
754-
}
755737

756738
pickers.Add(new LayoutId(
757739
id: _i,
@@ -782,8 +764,7 @@ public override Widget build(BuildContext context) {
782764
style: CupertinoDatePickerUtils._kDefaultPickerTextStyle,
783765
child: new CustomMultiChildLayout(
784766
layoutDelegate: new _DatePickerLayoutDelegate(
785-
columnWidths: columnWidths,
786-
textDirectionFactor: this.textDirectionFactor
767+
columnWidths: columnWidths
787768
),
788769
children:
789770
pickers
@@ -834,7 +815,6 @@ public override State createState() {
834815
}
835816

836817
class _CupertinoTimerPickerState : State<CupertinoTimerPicker> {
837-
int textDirectionFactor;
838818
CupertinoLocalizations localizations;
839819
Alignment alignCenterLeft;
840820
Alignment alignCenterRight;
@@ -864,16 +844,15 @@ Widget _buildLabel(string text) {
864844

865845
public override void didChangeDependencies() {
866846
base.didChangeDependencies();
867-
this.textDirectionFactor = Directionality.of(this.context) == TextDirection.ltr ? 1 : -1;
868847
this.localizations = CupertinoLocalizations.of(this.context);
869-
this.alignCenterLeft = this.textDirectionFactor == 1 ? Alignment.centerLeft : Alignment.centerRight;
870-
this.alignCenterRight = this.textDirectionFactor == 1 ? Alignment.centerRight : Alignment.centerLeft;
848+
this.alignCenterLeft = Alignment.centerLeft;
849+
this.alignCenterRight = Alignment.centerRight;
871850
}
872851

873852
Widget _buildHourPicker() {
874853
return new CupertinoPicker(
875854
scrollController: new FixedExtentScrollController(initialItem: this.selectedHour),
876-
offAxisFraction: -0.5f * this.textDirectionFactor,
855+
offAxisFraction: -0.5f,
877856
itemExtent: CupertinoDatePickerUtils._kItemExtent,
878857
backgroundColor: CupertinoDatePickerUtils._kBackgroundColor,
879858
onSelectedItemChanged: (int index) => {
@@ -890,14 +869,9 @@ Widget _buildHourPicker() {
890869
float hourLabelWidth = this.widget.mode == CupertinoTimerPickerMode.hm
891870
? CupertinoDatePickerUtils._kPickerWidth / 4
892871
: CupertinoDatePickerUtils._kPickerWidth / 6;
893-
string semanticsLabel = this.textDirectionFactor == 1
894-
? this.localizations.timerPickerHour(index) + this.localizations.timerPickerHourLabel(index)
895-
: this.localizations.timerPickerHourLabel(index) + this.localizations.timerPickerHour(index);
896872
return new Container(
897873
alignment: this.alignCenterRight,
898-
padding: this.textDirectionFactor == 1
899-
? EdgeInsets.only(right: hourLabelWidth)
900-
: EdgeInsets.only(left: hourLabelWidth),
874+
padding: EdgeInsets.only(right: hourLabelWidth),
901875
child: new Container(
902876
alignment: this.alignCenterRight,
903877
padding: EdgeInsets.symmetric(horizontal: 2.0f),
@@ -933,13 +907,13 @@ Widget _buildHourColumn() {
933907
Widget _buildMinutePicker() {
934908
float offAxisFraction;
935909
if (this.widget.mode == CupertinoTimerPickerMode.hm) {
936-
offAxisFraction = 0.5f * this.textDirectionFactor;
910+
offAxisFraction = 0.5f;
937911
}
938912
else if (this.widget.mode == CupertinoTimerPickerMode.hms) {
939913
offAxisFraction = 0.0f;
940914
}
941915
else {
942-
offAxisFraction = -0.5f * this.textDirectionFactor;
916+
offAxisFraction = -0.5f;
943917
}
944918

945919
return new CupertinoPicker(
@@ -961,17 +935,10 @@ Widget _buildMinutePicker() {
961935
},
962936
children: CupertinoDatePickerUtils.listGenerate(60 / this.widget.minuteInterval, (int index) => {
963937
int minute = index * this.widget.minuteInterval;
964-
string semanticsLabel = this.textDirectionFactor == 1
965-
? this.localizations.timerPickerMinute(minute) +
966-
this.localizations.timerPickerMinuteLabel(minute)
967-
: this.localizations.timerPickerMinuteLabel(minute) +
968-
this.localizations.timerPickerMinute(minute);
969938
if (this.widget.mode == CupertinoTimerPickerMode.ms) {
970939
return new Container(
971940
alignment: this.alignCenterRight,
972-
padding: this.textDirectionFactor == 1
973-
? EdgeInsets.only(right: CupertinoDatePickerUtils._kPickerWidth / 4)
974-
: EdgeInsets.only(left: CupertinoDatePickerUtils._kPickerWidth / 4),
941+
padding: EdgeInsets.only(right: CupertinoDatePickerUtils._kPickerWidth / 4),
975942
child: new Container(
976943
alignment: this.alignCenterRight,
977944
padding: EdgeInsets.symmetric(horizontal: 2.0f),
@@ -1003,9 +970,7 @@ Widget _buildMinuteColumn() {
1003970
minuteLabel = new IgnorePointer(
1004971
child: new Container(
1005972
alignment: this.alignCenterLeft,
1006-
padding: this.textDirectionFactor == 1
1007-
? EdgeInsets.only(left: CupertinoDatePickerUtils._kPickerWidth / 10)
1008-
: EdgeInsets.only(right: CupertinoDatePickerUtils._kPickerWidth / 10),
973+
padding: EdgeInsets.only(left: CupertinoDatePickerUtils._kPickerWidth / 10),
1009974
child: new Container(
1010975
alignment: this.alignCenterLeft,
1011976
padding: EdgeInsets.symmetric(horizontal: 2.0f),
@@ -1039,7 +1004,7 @@ Widget _buildMinuteColumn() {
10391004
}
10401005

10411006
Widget _buildSecondPicker() {
1042-
float offAxisFraction = 0.5f * this.textDirectionFactor;
1007+
float offAxisFraction = 0.5f;
10431008
float secondPickerWidth = this.widget.mode == CupertinoTimerPickerMode.ms
10441009
? CupertinoDatePickerUtils._kPickerWidth / 10
10451010
: CupertinoDatePickerUtils._kPickerWidth / 6;
@@ -1062,11 +1027,6 @@ Widget _buildSecondPicker() {
10621027
},
10631028
children: CupertinoDatePickerUtils.listGenerate(60 / this.widget.secondInterval, (int index) => {
10641029
int second = index * this.widget.secondInterval;
1065-
string semanticsLabel = this.textDirectionFactor == 1
1066-
? this.localizations.timerPickerSecond(second) +
1067-
this.localizations.timerPickerSecondLabel(second)
1068-
: this.localizations.timerPickerSecondLabel(second) +
1069-
this.localizations.timerPickerSecond(second);
10701030
return new Container(
10711031
alignment: this.alignCenterLeft,
10721032
child: new Container(
@@ -1087,9 +1047,7 @@ Widget _buildSecondColumn() {
10871047
Widget secondLabel = new IgnorePointer(
10881048
child: new Container(
10891049
alignment: this.alignCenterLeft,
1090-
padding: this.textDirectionFactor == 1
1091-
? EdgeInsets.only(left: secondPickerWidth)
1092-
: EdgeInsets.only(right: secondPickerWidth),
1050+
padding: EdgeInsets.only(left: secondPickerWidth),
10931051
child: new Container(
10941052
alignment: this.alignCenterLeft,
10951053
padding: EdgeInsets.symmetric(horizontal: 2.0f),

Runtime/cupertino/localization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public override string datePickerMinuteSemanticsLabel(int minute) {
169169
public override string datePickerMediumDate(DateTime date) {
170170
var day = _shortWeekdays[((int) date.DayOfWeek + 6) % 7];
171171
var month = _shortMonths[date.Month - 1];
172-
return $"{day}, {month} {date.Day.ToString().PadRight(2)} ";
172+
return $"{day} {month} {date.Day.ToString().PadRight(2)} ";
173173
}
174174

175175
public override DatePickerDateOrder datePickerDateOrder {

0 commit comments

Comments
 (0)