Commit df7fdb5b authored by Hixie's avatar Hixie

pushDialog(navigator, (navigator) => new Dialog(...)).then((result) => process(result));

parent 82f5f14b
...@@ -68,7 +68,7 @@ class AddressBookApp extends App { ...@@ -68,7 +68,7 @@ class AddressBookApp extends App {
child: new Icon(type: 'image/photo_camera', size: 24), child: new Icon(type: 'image/photo_camera', size: 24),
backgroundColor: Theme.of(this).accentColor, backgroundColor: Theme.of(this).accentColor,
onPressed: () { onPressed: () {
navigator.push(new DialogRoute(builder: (navigator, route) { showDialog(navigator, (navigator) {
return new Dialog( return new Dialog(
title: new Text("Describe your picture"), title: new Text("Describe your picture"),
content: new ScrollableBlock([ content: new ScrollableBlock([
...@@ -79,9 +79,7 @@ class AddressBookApp extends App { ...@@ -79,9 +79,7 @@ class AddressBookApp extends App {
actions: [ actions: [
new FlatButton( new FlatButton(
child: new Text('DISCARD'), child: new Text('DISCARD'),
onPressed: () { onPressed: navigator.pop
navigator.pop();
}
), ),
new FlatButton( new FlatButton(
child: new Text('SAVE'), child: new Text('SAVE'),
...@@ -91,7 +89,7 @@ class AddressBookApp extends App { ...@@ -91,7 +89,7 @@ class AddressBookApp extends App {
), ),
] ]
); );
})); });
} }
); );
} }
......
...@@ -214,10 +214,8 @@ class FeedFragment extends StatefulComponent { ...@@ -214,10 +214,8 @@ class FeedFragment extends StatefulComponent {
} }
void _handleActionButtonPressed() { void _handleActionButtonPressed() {
setState(() { showDialog(navigator, (navigator) => new AddItemDialog(navigator)).then((route) {
navigator.push(new DialogRoute(builder: (navigator, route) { navigator.pushNamed(route);
return new AddItemDialog(navigator);
}));
}); });
} }
...@@ -286,8 +284,7 @@ class AddItemDialog extends StatefulComponent { ...@@ -286,8 +284,7 @@ class AddItemDialog extends StatefulComponent {
new FlatButton( new FlatButton(
child: new Text('ADD'), child: new Text('ADD'),
onPressed: () { onPressed: () {
navigator.pop(); navigator.pop(_addItemRoute);
navigator.pushNamed(_addItemRoute);
} }
), ),
] ]
......
...@@ -45,26 +45,29 @@ class StockSettings extends StatefulComponent { ...@@ -45,26 +45,29 @@ class StockSettings extends StatefulComponent {
_handleOptimismChanged(false); _handleOptimismChanged(false);
break; break;
case StockMode.pessimistic: case StockMode.pessimistic:
navigator.push(new DialogRoute(builder: (navigator, route) { showDialog(navigator, (navigator) {
return new Dialog( return new Dialog(
title: new Text("Change mode?"), title: new Text("Change mode?"),
content: new Text("Optimistic mode means everything is awesome. Are you sure you can handle that?"), content: new Text("Optimistic mode means everything is awesome. Are you sure you can handle that?"),
onDismiss: navigator.pop, onDismiss: () {
navigator.pop(false);
},
actions: [ actions: [
new FlatButton( new FlatButton(
child: new Text('NO THANKS'), child: new Text('NO THANKS'),
onPressed: navigator.pop onPressed: () {
navigator.pop(false);
}
), ),
new FlatButton( new FlatButton(
child: new Text('AGREE'), child: new Text('AGREE'),
onPressed: () { onPressed: () {
_handleOptimismChanged(true); navigator.pop(true);
navigator.pop();
} }
), ),
] ]
); );
})); }).then(_handleOptimismChanged);
break; break;
} }
} }
......
...@@ -2,13 +2,18 @@ ...@@ -2,13 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async';
import 'package:sky/theme/colors.dart' as colors; import 'package:sky/theme/colors.dart' as colors;
import 'package:sky/widgets/basic.dart'; import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/default_text_style.dart'; import 'package:sky/widgets/default_text_style.dart';
import 'package:sky/widgets/material.dart'; import 'package:sky/widgets/material.dart';
import 'package:sky/widgets/navigator.dart';
import 'package:sky/widgets/scrollable_viewport.dart'; import 'package:sky/widgets/scrollable_viewport.dart';
import 'package:sky/widgets/theme.dart'; import 'package:sky/widgets/theme.dart';
typedef Widget DialogBuilder(Navigator navigator);
/// A material design dialog /// A material design dialog
/// ///
/// <https://www.google.com/design/spec/components/dialogs.html> /// <https://www.google.com/design/spec/components/dialogs.html>
...@@ -100,3 +105,14 @@ class Dialog extends Component { ...@@ -100,3 +105,14 @@ class Dialog extends Component {
} }
} }
Future<dynamic> showDialog(Navigator navigator, DialogBuilder builder) {
Completer completer = new Completer();
navigator.push(new DialogRoute(
completer: completer,
builder: (navigator, route) {
return builder(navigator);
}
));
return completer.future;
}
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async';
import 'package:sky/animation/animated_value.dart'; import 'package:sky/animation/animated_value.dart';
import 'package:sky/animation/animation_performance.dart'; import 'package:sky/animation/animation_performance.dart';
import 'package:sky/animation/curves.dart'; import 'package:sky/animation/curves.dart';
...@@ -14,7 +16,7 @@ typedef Widget RouteBuilder(Navigator navigator, RouteBase route); ...@@ -14,7 +16,7 @@ typedef Widget RouteBuilder(Navigator navigator, RouteBase route);
abstract class RouteBase { abstract class RouteBase {
Widget build(Navigator navigator, RouteBase route); Widget build(Navigator navigator, RouteBase route);
bool get isOpaque; bool get isOpaque;
void popState() { } void popState([dynamic result]) { assert(result == null); }
} }
class Route extends RouteBase { class Route extends RouteBase {
...@@ -28,17 +30,16 @@ class Route extends RouteBase { ...@@ -28,17 +30,16 @@ class Route extends RouteBase {
} }
class DialogRoute extends RouteBase { class DialogRoute extends RouteBase {
DialogRoute({ this.builder, this.callback }); DialogRoute({ this.completer, this.builder });
final Completer completer;
final RouteBuilder builder; final RouteBuilder builder;
Function callback;
Widget build(Navigator navigator, RouteBase route) => builder(navigator, route); Widget build(Navigator navigator, RouteBase route) => builder(navigator, route);
bool get isOpaque => false; bool get isOpaque => false;
void popState() { void popState([dynamic result]) {
if (callback != null) completer.complete(result);
callback(this);
} }
} }
...@@ -52,7 +53,8 @@ class RouteState extends RouteBase { ...@@ -52,7 +53,8 @@ class RouteState extends RouteBase {
Widget build(Navigator navigator, RouteBase route) => null; Widget build(Navigator navigator, RouteBase route) => null;
bool get isOpaque => false; bool get isOpaque => false;
void popState() { void popState([dynamic result]) {
assert(result == null);
if (callback != null) if (callback != null)
callback(this); callback(this);
} }
...@@ -197,10 +199,10 @@ class NavigationState { ...@@ -197,10 +199,10 @@ class NavigationState {
historyIndex++; historyIndex++;
} }
void pop() { void pop([dynamic result]) {
if (historyIndex > 0) { if (historyIndex > 0) {
HistoryEntry entry = history[historyIndex]; HistoryEntry entry = history[historyIndex];
entry.route.popState(); entry.route.popState(result);
entry.fullyOpaque = false; entry.fullyOpaque = false;
historyIndex--; historyIndex--;
} }
...@@ -240,9 +242,9 @@ class Navigator extends StatefulComponent { ...@@ -240,9 +242,9 @@ class Navigator extends StatefulComponent {
}); });
} }
void pop() { void pop([dynamic result]) {
setState(() { setState(() {
state.pop(); state.pop(result);
}); });
} }
......
...@@ -26,7 +26,6 @@ abstract class Key { ...@@ -26,7 +26,6 @@ abstract class Key {
factory Key(String value) => new StringKey(value); factory Key(String value) => new StringKey(value);
factory Key.stringify(Object value) => new StringKey(value.toString()); factory Key.stringify(Object value) => new StringKey(value.toString());
factory Key.fromObjectIdentity(Object value) => new ObjectKey(value); factory Key.fromObjectIdentity(Object value) => new ObjectKey(value);
factory Key.unique() => new UniqueKey();
} }
class StringKey extends Key { class StringKey extends Key {
...@@ -45,14 +44,25 @@ class ObjectKey extends Key { ...@@ -45,14 +44,25 @@ class ObjectKey extends Key {
int get hashCode => identityHashCode(value); int get hashCode => identityHashCode(value);
} }
class UniqueKey extends Key { abstract class GlobalKey extends Key {
UniqueKey() : super.constructor(); GlobalKey.constructor() : super.constructor(); // so that subclasses can call us, since the Key() factory constructor shadows the implicit constructor
String toString() => '[$hashCode]'; factory GlobalKey({ String label }) => new LabeledGlobalKey(label);
factory GlobalKey.fromObjectIdentity(Object value) => new GlobalObjectKey(value);
} }
class GlobalKey extends Key { class LabeledGlobalKey extends GlobalKey {
GlobalKey() : super.constructor(); // the label is purely for documentary purposes and does not affect the key
String toString() => '[Global Key $hashCode]'; LabeledGlobalKey(this._label) : super.constructor();
final String _label;
String toString() => '[GlobalKey ${_label != null ? _label : hashCode}]';
}
class GlobalObjectKey extends GlobalKey {
GlobalObjectKey(this.value) : super.constructor();
final Object value;
String toString() => '[GlobalKey ${value.runtimeType}(${value.hashCode})]';
bool operator==(other) => other is GlobalObjectKey && identical(other.value, value);
int get hashCode => identityHashCode(value);
} }
/// A base class for elements of the widget tree /// A base class for elements of the widget tree
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment