|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:ui'; |
| 6 | +import 'package:dual_screen/dual_screen.dart'; |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import '../../gallery_localizations.dart'; |
| 9 | + |
| 10 | +// BEGIN twoPaneDemo |
| 11 | + |
| 12 | +enum TwoPaneDemoType { |
| 13 | + foldable, |
| 14 | + tablet, |
| 15 | + smallScreen, |
| 16 | +} |
| 17 | + |
| 18 | +class TwoPaneDemo extends StatefulWidget { |
| 19 | + const TwoPaneDemo({ |
| 20 | + super.key, |
| 21 | + required this.restorationId, |
| 22 | + required this.type, |
| 23 | + }); |
| 24 | + |
| 25 | + final String restorationId; |
| 26 | + final TwoPaneDemoType type; |
| 27 | + |
| 28 | + @override |
| 29 | + TwoPaneDemoState createState() => TwoPaneDemoState(); |
| 30 | +} |
| 31 | + |
| 32 | +class TwoPaneDemoState extends State<TwoPaneDemo> with RestorationMixin { |
| 33 | + final RestorableInt _currentIndex = RestorableInt(-1); |
| 34 | + |
| 35 | + @override |
| 36 | + String get restorationId => widget.restorationId; |
| 37 | + |
| 38 | + @override |
| 39 | + void restoreState(RestorationBucket? oldBucket, bool initialRestore) { |
| 40 | + registerForRestoration(_currentIndex, 'two_pane_selected_item'); |
| 41 | + } |
| 42 | + |
| 43 | + @override |
| 44 | + void dispose() { |
| 45 | + _currentIndex.dispose(); |
| 46 | + super.dispose(); |
| 47 | + } |
| 48 | + |
| 49 | + @override |
| 50 | + Widget build(BuildContext context) { |
| 51 | + TwoPanePriority panePriority = TwoPanePriority.both; |
| 52 | + if (widget.type == TwoPaneDemoType.smallScreen) { |
| 53 | + panePriority = _currentIndex.value == -1 |
| 54 | + ? TwoPanePriority.start |
| 55 | + : TwoPanePriority.end; |
| 56 | + } |
| 57 | + return SimulateScreen( |
| 58 | + type: widget.type, |
| 59 | + child: TwoPane( |
| 60 | + paneProportion: 0.3, |
| 61 | + panePriority: panePriority, |
| 62 | + startPane: ListPane( |
| 63 | + selectedIndex: _currentIndex.value, |
| 64 | + onSelect: (int index) { |
| 65 | + setState(() { |
| 66 | + _currentIndex.value = index; |
| 67 | + }); |
| 68 | + }, |
| 69 | + ), |
| 70 | + endPane: DetailsPane( |
| 71 | + selectedIndex: _currentIndex.value, |
| 72 | + onClose: switch (widget.type) { |
| 73 | + TwoPaneDemoType.smallScreen => () => setState(() { _currentIndex.value = -1; }), |
| 74 | + TwoPaneDemoType.foldable || TwoPaneDemoType.tablet => null, |
| 75 | + }, |
| 76 | + ), |
| 77 | + ), |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +class ListPane extends StatelessWidget { |
| 83 | + |
| 84 | + const ListPane({ |
| 85 | + super.key, |
| 86 | + required this.onSelect, |
| 87 | + required this.selectedIndex, |
| 88 | + }); |
| 89 | + final ValueChanged<int> onSelect; |
| 90 | + final int selectedIndex; |
| 91 | + |
| 92 | + @override |
| 93 | + Widget build(BuildContext context) { |
| 94 | + return Scaffold( |
| 95 | + appBar: AppBar( |
| 96 | + automaticallyImplyLeading: false, |
| 97 | + title: Text(GalleryLocalizations.of(context)!.demoTwoPaneList), |
| 98 | + ), |
| 99 | + body: Scrollbar( |
| 100 | + child: ListView( |
| 101 | + restorationId: 'list_demo_list_view', |
| 102 | + padding: const EdgeInsets.symmetric(vertical: 8), |
| 103 | + children: <Widget>[ |
| 104 | + for (int index = 1; index < 21; index++) |
| 105 | + ListTile( |
| 106 | + onTap: () { |
| 107 | + onSelect(index); |
| 108 | + }, |
| 109 | + selected: selectedIndex == index, |
| 110 | + leading: ExcludeSemantics( |
| 111 | + child: CircleAvatar(child: Text('$index')), |
| 112 | + ), |
| 113 | + title: Text( |
| 114 | + GalleryLocalizations.of(context)!.demoTwoPaneItem(index), |
| 115 | + ), |
| 116 | + ), |
| 117 | + ], |
| 118 | + ), |
| 119 | + ), |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +class DetailsPane extends StatelessWidget { |
| 125 | + |
| 126 | + const DetailsPane({ |
| 127 | + super.key, |
| 128 | + required this.selectedIndex, |
| 129 | + this.onClose, |
| 130 | + }); |
| 131 | + final VoidCallback? onClose; |
| 132 | + final int selectedIndex; |
| 133 | + |
| 134 | + @override |
| 135 | + Widget build(BuildContext context) { |
| 136 | + return Scaffold( |
| 137 | + appBar: AppBar( |
| 138 | + automaticallyImplyLeading: false, |
| 139 | + leading: onClose == null |
| 140 | + ? null |
| 141 | + : IconButton(icon: const Icon(Icons.close), onPressed: onClose), |
| 142 | + title: Text( |
| 143 | + GalleryLocalizations.of(context)!.demoTwoPaneDetails, |
| 144 | + ), |
| 145 | + ), |
| 146 | + body: ColoredBox( |
| 147 | + color: const Color(0xfffafafa), |
| 148 | + child: Center( |
| 149 | + child: Text( |
| 150 | + selectedIndex == -1 |
| 151 | + ? GalleryLocalizations.of(context)!.demoTwoPaneSelectItem |
| 152 | + : GalleryLocalizations.of(context)! |
| 153 | + .demoTwoPaneItemDetails(selectedIndex), |
| 154 | + ), |
| 155 | + ), |
| 156 | + ), |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +class SimulateScreen extends StatelessWidget { |
| 162 | + const SimulateScreen({ |
| 163 | + super.key, |
| 164 | + required this.type, |
| 165 | + required this.child, |
| 166 | + }); |
| 167 | + |
| 168 | + final TwoPaneDemoType type; |
| 169 | + final TwoPane child; |
| 170 | + |
| 171 | + // An approximation of a real foldable |
| 172 | + static const double foldableAspectRatio = 20 / 18; |
| 173 | + // 16x9 candy bar phone |
| 174 | + static const double singleScreenAspectRatio = 9 / 16; |
| 175 | + // Taller desktop / tablet |
| 176 | + static const double tabletAspectRatio = 4 / 3; |
| 177 | + // How wide should the hinge be, as a proportion of total width |
| 178 | + static const double hingeProportion = 1 / 35; |
| 179 | + |
| 180 | + @override |
| 181 | + Widget build(BuildContext context) { |
| 182 | + return Center( |
| 183 | + child: Container( |
| 184 | + decoration: BoxDecoration( |
| 185 | + color: Colors.black, |
| 186 | + borderRadius: BorderRadius.circular(16), |
| 187 | + ), |
| 188 | + padding: const EdgeInsets.all(14), |
| 189 | + child: AspectRatio( |
| 190 | + aspectRatio: switch (type) { |
| 191 | + TwoPaneDemoType.foldable => foldableAspectRatio, |
| 192 | + TwoPaneDemoType.tablet => tabletAspectRatio, |
| 193 | + TwoPaneDemoType.smallScreen => singleScreenAspectRatio, |
| 194 | + }, |
| 195 | + child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) { |
| 196 | + final Size size = Size(constraints.maxWidth, constraints.maxHeight); |
| 197 | + final Size hingeSize = Size(size.width * hingeProportion, size.height); |
| 198 | + // Position the hinge in the middle of the display |
| 199 | + final Rect hingeBounds = Rect.fromLTWH( |
| 200 | + (size.width - hingeSize.width) / 2, |
| 201 | + 0, |
| 202 | + hingeSize.width, |
| 203 | + hingeSize.height, |
| 204 | + ); |
| 205 | + return MediaQuery( |
| 206 | + data: MediaQueryData( |
| 207 | + size: size, |
| 208 | + displayFeatures: <DisplayFeature>[ |
| 209 | + if (type == TwoPaneDemoType.foldable) |
| 210 | + DisplayFeature( |
| 211 | + bounds: hingeBounds, |
| 212 | + type: DisplayFeatureType.hinge, |
| 213 | + state: DisplayFeatureState.postureFlat, |
| 214 | + ), |
| 215 | + ], |
| 216 | + ), |
| 217 | + child: child, |
| 218 | + ); |
| 219 | + }), |
| 220 | + ), |
| 221 | + ), |
| 222 | + ); |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +// END |
0 commit comments