From 036f7ef59dc7991f6b2532b003a7589d6ece1d66 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 27 Sep 2016 17:19:55 -0700 Subject: [PATCH] Add support for landscape date pickers Fixes #5141 --- .../android/AndroidManifest.xml | 2 +- .../flutter/lib/src/material/date_picker.dart | 154 +++++++++++++----- .../lib/src/widgets/orientation_builder.dart | 41 +++++ packages/flutter/lib/widgets.dart | 1 + 4 files changed, 153 insertions(+), 45 deletions(-) create mode 100644 packages/flutter/lib/src/widgets/orientation_builder.dart diff --git a/examples/flutter_gallery/android/AndroidManifest.xml b/examples/flutter_gallery/android/AndroidManifest.xml index 9dd7f31d9a..dc60068d84 100644 --- a/examples/flutter_gallery/android/AndroidManifest.xml +++ b/examples/flutter_gallery/android/AndroidManifest.xml @@ -9,7 +9,7 @@ - + diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 5a70cae8a9..1ce1cefc25 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:math' as math; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; @@ -25,14 +26,20 @@ import 'typography.dart'; enum _DatePickerMode { day, year } -const double _kDatePickerHeaderHeight = 100.0; +const double _kDatePickerHeaderPortraitHeight = 100.0; +const double _kDatePickerHeaderLandscapeWidth = 168.0; const Duration _kMonthScrollDuration = const Duration(milliseconds: 200); const double _kDayPickerRowHeight = 42.0; const int _kMaxDayPickerRowCount = 6; // A 31 day month that starts on Saturday. // Two extra rows: one for the day-of-week header and one for the month header. const double _kMaxDayPickerHeight = _kDayPickerRowHeight * (_kMaxDayPickerRowCount + 2); -const double _kMonthPickerWidth = 330.0; + +const double _kMonthPickerPortraitWidth = 330.0; +const double _kMonthPickerLandscapeWidth = 344.0; + +const double _kDialogActionBarHeight = 52.0; +const double _kDatePickerLandscapeHeight = _kMaxDayPickerHeight + _kDialogActionBarHeight; // Shows the selected date in large font and toggles between year and day mode class _DatePickerHeader extends StatelessWidget { @@ -40,15 +47,18 @@ class _DatePickerHeader extends StatelessWidget { Key key, @required this.selectedDate, @required this.mode, - @required this.onModeChanged + @required this.onModeChanged, + @required this.orientation, }) : super(key: key) { assert(selectedDate != null); assert(mode != null); + assert(orientation != null); } final DateTime selectedDate; final _DatePickerMode mode; final ValueChanged<_DatePickerMode> onModeChanged; + final Orientation orientation; void _handleChangeMode(_DatePickerMode value) { if (value != mode) @@ -84,12 +94,30 @@ class _DatePickerHeader extends StatelessWidget { break; } + double height; + double width; + EdgeInsets padding; + MainAxisAlignment mainAxisAlignment; + switch (orientation) { + case Orientation.portrait: + height = _kDatePickerHeaderPortraitHeight; + padding = const EdgeInsets.symmetric(horizontal: 24.0); + mainAxisAlignment = MainAxisAlignment.center; + break; + case Orientation.landscape: + width = _kDatePickerHeaderLandscapeWidth; + padding = const EdgeInsets.all(16.0); + mainAxisAlignment = MainAxisAlignment.start; + break; + } + return new Container( - height: _kDatePickerHeaderHeight, - padding: const EdgeInsets.symmetric(horizontal: 24.0), + width: width, + height: height, + padding: padding, decoration: new BoxDecoration(backgroundColor: backgroundColor), child: new Column( - mainAxisAlignment: MainAxisAlignment.center, + mainAxisAlignment: mainAxisAlignment, crossAxisAlignment: CrossAxisAlignment.start, children: [ new GestureDetector( @@ -98,7 +126,7 @@ class _DatePickerHeader extends StatelessWidget { ), new GestureDetector( onTap: () => _handleChangeMode(_DatePickerMode.day), - child: new Text(new DateFormat('MMMEd').format(selectedDate), style: dayStyle) + child: new Text(new DateFormat('E, MMM\u00a0d').format(selectedDate), style: dayStyle) ), ] ) @@ -112,7 +140,7 @@ class _DayPickerGridDelegate extends GridDelegateWithInOrderChildPlacement { final int columnCount = DateTime.DAYS_PER_WEEK; return new GridSpecification.fromRegularTiles( tileWidth: constraints.maxWidth / columnCount, - tileHeight: _kDayPickerRowHeight, + tileHeight: math.min(_kDayPickerRowHeight, constraints.maxHeight / (_kMaxDayPickerRowCount + 1)), columnCount: columnCount, rowCount: (childCount / columnCount).ceil() ); @@ -236,9 +264,12 @@ class DayPicker extends StatelessWidget { ) ) ), - new CustomGrid( - delegate: _kDayPickerGridDelegate, - children: labels + new Flexible( + fit: FlexFit.loose, + child: new CustomGrid( + delegate: _kDayPickerGridDelegate, + children: labels + ) ) ] ) @@ -357,7 +388,7 @@ class _MonthPickerState extends State { @override Widget build(BuildContext context) { return new SizedBox( - width: _kMonthPickerWidth, + width: _kMonthPickerPortraitWidth, height: _kMaxDayPickerHeight, child: new Stack( children: [ @@ -507,6 +538,7 @@ class _DatePickerDialogState extends State<_DatePickerDialog> { DateTime _selectedDate; _DatePickerMode _mode = _DatePickerMode.day; + GlobalKey _pickerKey = new GlobalKey(); void _handleModeChanged(_DatePickerMode mode) { HapticFeedback.vibrate(); @@ -543,6 +575,7 @@ class _DatePickerDialogState extends State<_DatePickerDialog> { switch (_mode) { case _DatePickerMode.day: return new MonthPicker( + key: _pickerKey, selectedDate: _selectedDate, onChanged: _handleDayChanged, firstDate: config.firstDate, @@ -550,6 +583,7 @@ class _DatePickerDialogState extends State<_DatePickerDialog> { ); case _DatePickerMode.year: return new YearPicker( + key: _pickerKey, selectedDate: _selectedDate, onChanged: _handleYearChanged, firstDate: config.firstDate, @@ -561,39 +595,71 @@ class _DatePickerDialogState extends State<_DatePickerDialog> { @override Widget build(BuildContext context) { - return new Dialog( + Widget picker = new Flexible( + fit: FlexFit.loose, child: new SizedBox( - width: _kMonthPickerWidth, - child: new Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - new _DatePickerHeader( - selectedDate: _selectedDate, - mode: _mode, - onModeChanged: _handleModeChanged - ), - new Container( - height: _kMaxDayPickerHeight, - child: _buildPicker() - ), - new ButtonTheme.bar( - child: new ButtonBar( - alignment: MainAxisAlignment.end, - children: [ - new FlatButton( - child: new Text('CANCEL'), - onPressed: _handleCancel - ), - new FlatButton( - child: new Text('OK'), - onPressed: _handleOk - ), - ] - ) - ) - ] - ) + height: _kMaxDayPickerHeight, + child: _buildPicker(), + ) + ); + Widget actions = new ButtonTheme.bar( + child: new ButtonBar( + alignment: MainAxisAlignment.end, + children: [ + new FlatButton( + child: new Text('CANCEL'), + onPressed: _handleCancel + ), + new FlatButton( + child: new Text('OK'), + onPressed: _handleOk + ), + ] + ) + ); + + return new Dialog( + child: new OrientationBuilder( + builder: (BuildContext context, Orientation orientation) { + Widget header = new _DatePickerHeader( + selectedDate: _selectedDate, + mode: _mode, + onModeChanged: _handleModeChanged, + orientation: orientation + ); + assert(orientation != null); + switch (orientation) { + case Orientation.portrait: + return new SizedBox( + width: _kMonthPickerPortraitWidth, + child: new Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [header, picker, actions] + ) + ); + case Orientation.landscape: + return new SizedBox( + height: _kDatePickerLandscapeHeight, + child: new Row( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + header, + new SizedBox( + width: _kMonthPickerLandscapeWidth, + child: new Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [picker, actions] + ) + ) + ] + ) + ); + } + return null; + } ) ); } diff --git a/packages/flutter/lib/src/widgets/orientation_builder.dart b/packages/flutter/lib/src/widgets/orientation_builder.dart new file mode 100644 index 0000000000..81f747a336 --- /dev/null +++ b/packages/flutter/lib/src/widgets/orientation_builder.dart @@ -0,0 +1,41 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:meta/meta.dart'; + +import 'basic.dart'; +import 'framework.dart'; +import 'layout_builder.dart'; +import 'media_query.dart'; + +/// Signature for a function that builds a widget given an [Orientation]. +typedef Widget OrientationWidgetBuilder(BuildContext context, Orientation orientation); + +class OrientationBuilder extends StatelessWidget { + /// Creates an orientation builder. + /// + /// The [builder] argument must not be null. + OrientationBuilder({ + Key key, + @required this.builder + }) : super(key: key) { + assert(builder != null); + } + + /// Builds the widgets below this widget given this widget's orientation. + final OrientationWidgetBuilder builder; + + Widget _buildWithConstraints(BuildContext context, BoxConstraints constraints) { + // If the constraints are fully unbounded (i.e., maxWidth and maxHeight are + // both infinite), we prefer Orientation.portrait because its more common to + // scroll vertially than horizontally. + final Orientation orientation = constraints.maxWidth > constraints.maxHeight ? Orientation.landscape : Orientation.portrait; + return builder(context, orientation); + } + + @override + Widget build(BuildContext context) { + return new LayoutBuilder(builder: _buildWithConstraints); + } +} diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index 9c4b7d1664..48a880cbfd 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -38,6 +38,7 @@ export 'src/widgets/mimic.dart'; export 'src/widgets/modal_barrier.dart'; export 'src/widgets/navigator.dart'; export 'src/widgets/notification_listener.dart'; +export 'src/widgets/orientation_builder.dart'; export 'src/widgets/overlay.dart'; export 'src/widgets/page_storage.dart'; export 'src/widgets/pageable_list.dart';