Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Foldable support (#677)
Browse files Browse the repository at this point in the history
* Keeping a few of the foldable experiments

* Second screen for splash and study wrapper routes

* TwoPane Demo

* Dialog on screen 1 and panePriority naming

* Update to latest TwoPane and foldable api

* Upgrade to dual_screen 1.0.3

* Fix analyze issues

* Fix formatting issues

* Update code segments

* Add TwoPane sample labels to l10n

* Update links to actual TwoPane documentation on pub.dev

* Add strings to l10n

* Add demo for selecting the screen for dialogs

* Make route path parameter named

* Reuse code for showing the flutter logo

* Move magic numbers to descriptive constants

* Better description for l10n label

* Update to dart 2.17 with support for super parameters

* Update goldens

* Remove foldable dialog type

* Change texts to say small screen instead of single screen

* Update goldens after copy change

* Change shrine golden tests initial route to avoid double instantiation of Shrine app
  • Loading branch information
andreidiaconu authored May 17, 2022
1 parent d1a9685 commit 221023a
Show file tree
Hide file tree
Showing 15 changed files with 1,797 additions and 80 deletions.
1,174 changes: 1,174 additions & 0 deletions lib/codeviewer/code_segments.dart

Large diffs are not rendered by default.

58 changes: 57 additions & 1 deletion lib/data/demos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'dart:collection';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
Expand All @@ -11,7 +12,6 @@ import 'package:flutter_gen/gen_l10n/gallery_localizations_en.dart'
import 'package:gallery/codeviewer/code_displayer.dart';
import 'package:gallery/codeviewer/code_segments.dart';
import 'package:gallery/data/icons.dart';

import 'package:gallery/deferred_widget.dart';
import 'package:gallery/demos/cupertino/cupertino_demos.dart'
deferred as cupertino_demos;
Expand All @@ -30,6 +30,8 @@ import 'package:gallery/demos/reference/motion_demo_shared_y_axis_transition.dar
import 'package:gallery/demos/reference/motion_demo_shared_z_axis_transition.dart';
import 'package:gallery/demos/reference/transformations_demo.dart'
deferred as transformations_demo;
import 'package:gallery/demos/reference/two_pane_demo.dart'
deferred as twopane_demo;
import 'package:gallery/demos/reference/typography_demo.dart'
deferred as typography;

Expand Down Expand Up @@ -1215,6 +1217,60 @@ List<GalleryDemo> cupertinoDemos(GalleryLocalizations localizations) {

List<GalleryDemo> otherDemos(GalleryLocalizations localizations) {
return [
GalleryDemo(
title: localizations.demoTwoPaneTitle,
icon: GalleryIcons.bottomSheetPersistent,
slug: 'two-pane',
subtitle: localizations.demoTwoPaneSubtitle,
configurations: [
GalleryDemoConfiguration(
title: localizations.demoTwoPaneFoldableLabel,
description: localizations.demoTwoPaneFoldableDescription,
documentationUrl:
'https://pub.dev/documentation/dual_screen/latest/dual_screen/TwoPane-class.html',
buildRoute: (_) => DeferredWidget(
twopane_demo.loadLibrary,
// ignore: prefer_const_constructors
() => twopane_demo.TwoPaneDemo(
type: twopane_demo.TwoPaneDemoType.foldable,
restorationId: 'two_pane_foldable',
),
),
code: CodeSegments.twoPaneDemo,
),
GalleryDemoConfiguration(
title: localizations.demoTwoPaneTabletLabel,
description: localizations.demoTwoPaneTabletDescription,
documentationUrl:
'https://pub.dev/documentation/dual_screen/latest/dual_screen/TwoPane-class.html',
buildRoute: (_) => DeferredWidget(
twopane_demo.loadLibrary,
// ignore: prefer_const_constructors
() => twopane_demo.TwoPaneDemo(
type: twopane_demo.TwoPaneDemoType.tablet,
restorationId: 'two_pane_tablet',
),
),
code: CodeSegments.twoPaneDemo,
),
GalleryDemoConfiguration(
title: localizations.demoTwoPaneSmallScreenLabel,
description: localizations.demoTwoPaneSmallScreenDescription,
documentationUrl:
'https://pub.dev/documentation/dual_screen/latest/dual_screen/TwoPane-class.html',
buildRoute: (_) => DeferredWidget(
twopane_demo.loadLibrary,
// ignore: prefer_const_constructors
() => twopane_demo.TwoPaneDemo(
type: twopane_demo.TwoPaneDemoType.smallScreen,
restorationId: 'two_pane_single',
),
),
code: CodeSegments.twoPaneDemo,
),
],
category: GalleryDemoCategory.other,
),
GalleryDemo(
title: localizations.demoMotionTitle,
icon: GalleryIcons.animation,
Expand Down
229 changes: 229 additions & 0 deletions lib/demos/reference/two_pane_demo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui';
import 'package:dual_screen/dual_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

// BEGIN twoPaneDemo

enum TwoPaneDemoType {
foldable,
tablet,
smallScreen,
}

class TwoPaneDemo extends StatefulWidget {
const TwoPaneDemo({
super.key,
required this.restorationId,
required this.type,
});

final String restorationId;
final TwoPaneDemoType type;

@override
TwoPaneDemoState createState() => TwoPaneDemoState();
}

class TwoPaneDemoState extends State<TwoPaneDemo> with RestorationMixin {
final RestorableInt _currentIndex = RestorableInt(-1);

@override
String get restorationId => widget.restorationId;

@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(_currentIndex, 'two_pane_selected_item');
}

@override
void dispose() {
_currentIndex.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
var panePriority = TwoPanePriority.both;
if (widget.type == TwoPaneDemoType.smallScreen) {
panePriority = _currentIndex.value == -1
? TwoPanePriority.start
: TwoPanePriority.end;
}
return SimulateScreen(
type: widget.type,
child: TwoPane(
paneProportion: 0.3,
panePriority: panePriority,
startPane: ListPane(
selectedIndex: _currentIndex.value,
onSelect: (index) {
setState(() {
_currentIndex.value = index;
});
},
),
endPane: DetailsPane(
selectedIndex: _currentIndex.value,
onClose: widget.type == TwoPaneDemoType.smallScreen
? () {
setState(() {
_currentIndex.value = -1;
});
}
: null,
),
),
);
}
}

class ListPane extends StatelessWidget {
final ValueChanged<int> onSelect;
final int selectedIndex;

const ListPane({
super.key,
required this.onSelect,
required this.selectedIndex,
});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(GalleryLocalizations.of(context)!.demoTwoPaneList),
),
body: Scrollbar(
child: ListView(
restorationId: 'list_demo_list_view',
padding: const EdgeInsets.symmetric(vertical: 8),
children: [
for (int index = 1; index < 21; index++)
ListTile(
onTap: () {
onSelect(index);
},
selected: selectedIndex == index,
leading: ExcludeSemantics(
child: CircleAvatar(child: Text('$index')),
),
title: Text(
GalleryLocalizations.of(context)!.demoTwoPaneItem(index),
),
),
],
),
),
);
}
}

class DetailsPane extends StatelessWidget {
final VoidCallback? onClose;
final int selectedIndex;

const DetailsPane({
super.key,
required this.selectedIndex,
this.onClose,
});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
leading: onClose == null
? null
: IconButton(icon: const Icon(Icons.close), onPressed: onClose),
title: Text(
GalleryLocalizations.of(context)!.demoTwoPaneDetails,
),
),
body: Container(
color: const Color(0xfffafafa),
child: Center(
child: Text(
selectedIndex == -1
? GalleryLocalizations.of(context)!.demoTwoPaneSelectItem
: GalleryLocalizations.of(context)!
.demoTwoPaneItemDetails(selectedIndex),
),
),
),
);
}
}

class SimulateScreen extends StatelessWidget {
const SimulateScreen({
super.key,
required this.type,
required this.child,
});

final TwoPaneDemoType type;
final TwoPane child;

// An approximation of a real foldable
static const double foldableAspectRatio = 20 / 18;
// 16x9 candy bar phone
static const double singleScreenAspectRatio = 9 / 16;
// Taller desktop / tablet
static const double tabletAspectRatio = 4 / 3;
// How wide should the hinge be, as a proportion of total width
static const double hingeProportion = 1 / 35;

@override
Widget build(BuildContext context) {
return Center(
child: Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(16),
),
padding: const EdgeInsets.all(14),
child: AspectRatio(
aspectRatio: type == TwoPaneDemoType.foldable
? foldableAspectRatio
: type == TwoPaneDemoType.tablet
? tabletAspectRatio
: singleScreenAspectRatio,
child: LayoutBuilder(builder: (context, constraints) {
final size = Size(constraints.maxWidth, constraints.maxHeight);
final hingeSize = Size(size.width * hingeProportion, size.height);
// Position the hinge in the middle of the display
final hingeBounds = Rect.fromLTWH(
(size.width - hingeSize.width) / 2,
0,
hingeSize.width,
hingeSize.height,
);
return MediaQuery(
data: MediaQueryData(
size: size,
displayFeatures: [
if (type == TwoPaneDemoType.foldable)
DisplayFeature(
bounds: hingeBounds,
type: DisplayFeatureType.hinge,
state: DisplayFeatureState.postureFlat,
),
],
),
child: child,
);
}),
),
),
);
}
}

// END
66 changes: 66 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -3296,5 +3296,71 @@
"replyDraftsLabel": "Drafts",
"@replyDraftsLabel": {
"description": "Text label for Drafts destination."
},
"demoTwoPaneFoldableLabel": "Foldable",
"@demoTwoPaneFoldableLabel": {
"description": "Option title for TwoPane demo on foldable devices."
},
"demoTwoPaneFoldableDescription": "This is how TwoPane behaves on a foldable device.",
"@demoTwoPaneFoldableDescription": {
"description": "Description for the foldable option configuration on the TwoPane demo."
},
"demoTwoPaneSmallScreenLabel": "Small Screen",
"@demoTwoPaneSmallScreenLabel": {
"description": "Option title for TwoPane demo in small screen mode. Counterpart of the foldable option."
},
"demoTwoPaneSmallScreenDescription": "This is how TwoPane behaves on a small screen device.",
"@demoTwoPaneSmallScreenDescription": {
"description": "Description for the small screen option configuration on the TwoPane demo."
},
"demoTwoPaneTabletLabel": "Tablet / Desktop",
"@demoTwoPaneTabletLabel": {
"description": "Option title for TwoPane demo in tablet or desktop mode."
},
"demoTwoPaneTabletDescription": "This is how TwoPane behaves on a larger screen like a tablet or desktop.",
"@demoTwoPaneTabletDescription": {
"description": "Description for the tablet / desktop option configuration on the TwoPane demo."
},
"demoTwoPaneTitle": "TwoPane",
"@demoTwoPaneTitle": {
"description": "Title for the TwoPane widget demo."
},
"demoTwoPaneSubtitle": "Responsive layouts on foldable, large, and small screens",
"@demoTwoPaneSubtitle": {
"description": "Subtitle for the TwoPane widget demo."
},
"splashSelectDemo": "Select a demo",
"@splashSelectDemo": {
"description": "Tip for user, visible on the right side of the splash screen when Gallery runs on a foldable device."
},
"demoTwoPaneList": "List",
"@demoTwoPaneList": {
"description": "Title of one of the panes in the TwoPane demo. It sits on top of a list of items."
},
"demoTwoPaneDetails": "Details",
"@demoTwoPaneDetails": {
"description": "Title of one of the panes in the TwoPane demo, which shows details of the currently selected item."
},
"demoTwoPaneSelectItem": "Select an item",
"@demoTwoPaneSelectItem": {
"description": "Tip for user, visible on the right side of the TwoPane widget demo in the foldable configuration."
},
"demoTwoPaneItem": "Item {value}",
"@demoTwoPaneItem": {
"description": "Generic item placeholder visible in the TwoPane widget demo.",
"placeholders": {
"value": {
"example": "1"
}
}
},
"demoTwoPaneItemDetails": "Item {value} details",
"@demoTwoPaneItemDetails": {
"description": "Generic item description or details visible in the TwoPane widget demo.",
"placeholders": {
"value": {
"example": "1"
}
}
}
}
Loading

0 comments on commit 221023a

Please sign in to comment.