Snackbar Refactor
"showSnackBar()" is now a feature of a Scaffold. To get to a Scaffold you either use a global key (`scaffoldKey.currentState.showSnackBar(...)`), or you use `Scaffold.of(context)`. Snack bars no longer have a route. They are entirely managed by the Scaffold. Fixes #432. Snack bars now queue up when you have several of them. Fixes #374. Snack bars now auto-size themselves around their contents. This is step one towards implementing multiline snack bars. Snack bars now self-dismiss after some per-snackbar configurable period. The self-dismissing pauses while a dialog is up above the snackbar (or anything that uses ModalRoute). To enable this, there's now a `ModalRoute.of(context)` API that returns the current ModalRoute, and you will be rebuilt if you asked for this and the route's "current" status changes. To implement this, the Navigator now rebuilds unconditionally any time it pushes or pops a route. Snack bars now use the curves that Android uses for snack bars. Snack bar contents now fade in.
This commit is contained in:
@@ -54,7 +54,6 @@ class FeedFragment extends StatefulComponent {
|
||||
}
|
||||
|
||||
class FeedFragmentState extends State<FeedFragment> {
|
||||
final GlobalKey<PlaceholderState> _snackBarPlaceholderKey = new GlobalKey<PlaceholderState>();
|
||||
FitnessMode _fitnessMode = FitnessMode.feed;
|
||||
|
||||
void _handleFitnessModeChange(FitnessMode value) {
|
||||
@@ -115,15 +114,14 @@ class FeedFragmentState extends State<FeedFragment> {
|
||||
|
||||
void _handleItemDismissed(FitnessItem item) {
|
||||
config.onItemDeleted(item);
|
||||
showSnackBar(
|
||||
context: context,
|
||||
placeholderKey: _snackBarPlaceholderKey,
|
||||
Scaffold.of(context).showSnackBar(new SnackBar(
|
||||
content: new Text("Item deleted."),
|
||||
actions: <SnackBarAction>[new SnackBarAction(label: "UNDO", onPressed: () {
|
||||
config.onItemCreated(item);
|
||||
Navigator.of(context).pop();
|
||||
})]
|
||||
);
|
||||
actions: <SnackBarAction>[
|
||||
new SnackBarAction(label: "UNDO", onPressed: () {
|
||||
config.onItemCreated(item);
|
||||
}),
|
||||
]
|
||||
));
|
||||
}
|
||||
|
||||
Widget buildChart() {
|
||||
@@ -212,7 +210,6 @@ class FeedFragmentState extends State<FeedFragment> {
|
||||
return new Scaffold(
|
||||
toolBar: buildToolBar(),
|
||||
body: buildBody(),
|
||||
snackBar: new Placeholder(key: _snackBarPlaceholderKey),
|
||||
floatingActionButton: buildFloatingActionButton()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -112,8 +112,6 @@ class MeasurementFragment extends StatefulComponent {
|
||||
}
|
||||
|
||||
class MeasurementFragmentState extends State<MeasurementFragment> {
|
||||
final GlobalKey<PlaceholderState> _snackBarPlaceholderKey = new GlobalKey<PlaceholderState>();
|
||||
|
||||
String _weight = "";
|
||||
DateTime _when = new DateTime.now();
|
||||
|
||||
@@ -123,11 +121,9 @@ class MeasurementFragmentState extends State<MeasurementFragment> {
|
||||
parsedWeight = double.parse(_weight);
|
||||
} on FormatException catch(e) {
|
||||
print("Exception $e");
|
||||
showSnackBar(
|
||||
context: context,
|
||||
placeholderKey: _snackBarPlaceholderKey,
|
||||
Scaffold.of(context).showSnackBar(new SnackBar(
|
||||
content: new Text('Save failed')
|
||||
);
|
||||
));
|
||||
}
|
||||
config.onCreated(new Measurement(when: _when, weight: parsedWeight));
|
||||
Navigator.of(context).pop();
|
||||
@@ -198,8 +194,7 @@ class MeasurementFragmentState extends State<MeasurementFragment> {
|
||||
Widget build(BuildContext context) {
|
||||
return new Scaffold(
|
||||
toolBar: buildToolBar(),
|
||||
body: buildBody(context),
|
||||
snackBar: new Placeholder(key: _snackBarPlaceholderKey)
|
||||
body: buildBody(context)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class StockHome extends StatefulComponent {
|
||||
|
||||
class StockHomeState extends State<StockHome> {
|
||||
|
||||
final GlobalKey<PlaceholderState> _snackBarPlaceholderKey = new GlobalKey<PlaceholderState>();
|
||||
final GlobalKey scaffoldKey = new GlobalKey();
|
||||
final GlobalKey<PlaceholderState> _bottomSheetPlaceholderKey = new GlobalKey<PlaceholderState>();
|
||||
bool _isSearching = false;
|
||||
String _searchQuery;
|
||||
@@ -179,19 +179,23 @@ class StockHomeState extends State<StockHome> {
|
||||
return stocks.where((Stock stock) => stock.symbol.contains(regexp));
|
||||
}
|
||||
|
||||
void _buyStock(Stock stock, Key arrowKey) {
|
||||
setState(() {
|
||||
stock.percentChange = 100.0 * (1.0 / stock.lastSale);
|
||||
stock.lastSale += 1.0;
|
||||
});
|
||||
scaffoldKey.currentState.showSnackBar(new SnackBar(
|
||||
content: new Text("Purchased ${stock.symbol} for ${stock.lastSale}"),
|
||||
actions: <SnackBarAction>[
|
||||
new SnackBarAction(label: "BUY MORE", onPressed: () { _buyStock(stock, arrowKey); })
|
||||
]
|
||||
));
|
||||
}
|
||||
|
||||
Widget buildStockList(BuildContext context, Iterable<Stock> stocks) {
|
||||
return new StockList(
|
||||
stocks: stocks.toList(),
|
||||
onAction: (Stock stock, Key arrowKey) {
|
||||
setState(() {
|
||||
stock.percentChange = 100.0 * (1.0 / stock.lastSale);
|
||||
stock.lastSale += 1.0;
|
||||
});
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
child: new StockSymbolBottomSheet(stock: stock)
|
||||
);
|
||||
},
|
||||
onAction: _buyStock,
|
||||
onOpen: (Stock stock, Key arrowKey) {
|
||||
Set<Key> mostValuableKeys = new Set<Key>();
|
||||
mostValuableKeys.add(arrowKey);
|
||||
@@ -229,6 +233,7 @@ class StockHomeState extends State<StockHome> {
|
||||
}
|
||||
|
||||
static GlobalKey searchFieldKey = new GlobalKey();
|
||||
static GlobalKey companyNameKey = new GlobalKey();
|
||||
|
||||
// TODO(abarth): Should we factor this into a SearchBar in the framework?
|
||||
Widget buildSearchBar() {
|
||||
@@ -247,18 +252,16 @@ class StockHomeState extends State<StockHome> {
|
||||
);
|
||||
}
|
||||
|
||||
void _handleUndo() {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
void _handleStockPurchased() {
|
||||
showSnackBar(
|
||||
void _handleCreateCompany() {
|
||||
showModalBottomSheet(
|
||||
// TODO(ianh): Fill this out.
|
||||
context: context,
|
||||
placeholderKey: _snackBarPlaceholderKey,
|
||||
content: new Text("Stock purchased!"),
|
||||
actions: <SnackBarAction>[
|
||||
new SnackBarAction(label: "UNDO", onPressed: _handleUndo)
|
||||
]
|
||||
child: new Column([
|
||||
new Input(
|
||||
key: companyNameKey,
|
||||
placeholder: 'Company Name'
|
||||
),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -266,15 +269,15 @@ class StockHomeState extends State<StockHome> {
|
||||
return new FloatingActionButton(
|
||||
child: new Icon(icon: 'content/add'),
|
||||
backgroundColor: Colors.redAccent[200],
|
||||
onPressed: _handleStockPurchased
|
||||
onPressed: _handleCreateCompany
|
||||
);
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return new Scaffold(
|
||||
key: scaffoldKey,
|
||||
toolBar: _isSearching ? buildSearchBar() : buildToolBar(),
|
||||
body: buildTabNavigator(),
|
||||
snackBar: new Placeholder(key: _snackBarPlaceholderKey),
|
||||
bottomSheet: new Placeholder(key: _bottomSheetPlaceholderKey),
|
||||
floatingActionButton: buildFloatingActionButton()
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user