diff --git a/dev/bots/test.sh b/dev/bots/test.sh
index ebc054e4bb..d40cb3db74 100755
--- a/dev/bots/test.sh
+++ b/dev/bots/test.sh
@@ -19,10 +19,11 @@ flutter analyze --flutter-repo
(cd packages/flx; dart -c test/all.dart)
(cd dev/manual_tests; flutter test)
+(cd examples/calculator; flutter test)
(cd examples/hello_world; flutter test)
(cd examples/layers; flutter test)
-(cd examples/flutter_gallery; flutter test)
(cd examples/stocks; flutter test)
+(cd examples/flutter_gallery; flutter test)
# generate and analyze our large sample app
dart dev/tools/mega_gallery.dart
diff --git a/examples/calculator/.gitignore b/examples/calculator/.gitignore
new file mode 100644
index 0000000000..14c7d4c3f7
--- /dev/null
+++ b/examples/calculator/.gitignore
@@ -0,0 +1,9 @@
+.DS_Store
+.atom/
+.idea
+.packages
+.pub/
+build/
+ios/.generated/
+packages
+pubspec.lock
diff --git a/examples/calculator/README.md b/examples/calculator/README.md
new file mode 100644
index 0000000000..f247b5cf60
--- /dev/null
+++ b/examples/calculator/README.md
@@ -0,0 +1,8 @@
+# calculator
+
+A Material Design Calculator written in Flutter.
+
+## Getting Started
+
+For help getting started with Flutter, view our online
+[documentation](http://flutter.io/).
diff --git a/examples/calculator/android/AndroidManifest.xml b/examples/calculator/android/AndroidManifest.xml
new file mode 100644
index 0000000000..8a7cc4839e
--- /dev/null
+++ b/examples/calculator/android/AndroidManifest.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/calculator/android/res/mipmap-hdpi/ic_launcher.png b/examples/calculator/android/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 0000000000..f0a55cda78
Binary files /dev/null and b/examples/calculator/android/res/mipmap-hdpi/ic_launcher.png differ
diff --git a/examples/calculator/android/res/mipmap-mdpi/ic_launcher.png b/examples/calculator/android/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 0000000000..eb98dc451d
Binary files /dev/null and b/examples/calculator/android/res/mipmap-mdpi/ic_launcher.png differ
diff --git a/examples/calculator/android/res/mipmap-xhdpi/ic_launcher.png b/examples/calculator/android/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 0000000000..f1783dbbf3
Binary files /dev/null and b/examples/calculator/android/res/mipmap-xhdpi/ic_launcher.png differ
diff --git a/examples/calculator/android/res/mipmap-xxhdpi/ic_launcher.png b/examples/calculator/android/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000000..46828a290a
Binary files /dev/null and b/examples/calculator/android/res/mipmap-xxhdpi/ic_launcher.png differ
diff --git a/examples/calculator/android/res/mipmap-xxxhdpi/ic_launcher.png b/examples/calculator/android/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000000..a5276715d9
Binary files /dev/null and b/examples/calculator/android/res/mipmap-xxxhdpi/ic_launcher.png differ
diff --git a/examples/calculator/flutter.yaml b/examples/calculator/flutter.yaml
new file mode 100644
index 0000000000..f52ac623ce
--- /dev/null
+++ b/examples/calculator/flutter.yaml
@@ -0,0 +1,2 @@
+name: calculator
+uses-material-design: true
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Contents.json b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Contents.json
new file mode 100644
index 0000000000..363d1c4ce4
--- /dev/null
+++ b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Contents.json
@@ -0,0 +1,142 @@
+{
+ "images" : [
+ {
+ "size" : "29x29",
+ "idiom" : "iphone",
+ "filename" : "Icon-Small@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "29x29",
+ "idiom" : "iphone",
+ "filename" : "Icon-Small@3x.png",
+ "scale" : "3x"
+ },
+ {
+ "size" : "40x40",
+ "idiom" : "iphone",
+ "filename" : "Icon-Small-40@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "40x40",
+ "idiom" : "iphone",
+ "filename" : "Icon-Small-40@3x.png",
+ "scale" : "3x"
+ },
+ {
+ "size" : "60x60",
+ "idiom" : "iphone",
+ "filename" : "Icon-60@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "60x60",
+ "idiom" : "iphone",
+ "filename" : "Icon-60@3x.png",
+ "scale" : "3x"
+ },
+ {
+ "size" : "29x29",
+ "idiom" : "ipad",
+ "filename" : "Icon-Small.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "29x29",
+ "idiom" : "ipad",
+ "filename" : "Icon-Small@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "40x40",
+ "idiom" : "ipad",
+ "filename" : "Icon-Small-40.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "40x40",
+ "idiom" : "ipad",
+ "filename" : "Icon-Small-40@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "76x76",
+ "idiom" : "ipad",
+ "filename" : "Icon-76.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "76x76",
+ "idiom" : "ipad",
+ "filename" : "Icon-76@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "83.5x83.5",
+ "idiom" : "ipad",
+ "filename" : "Icon-83.5@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "16x16",
+ "idiom" : "mac",
+ "filename" : "icon_16x16.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "16x16",
+ "idiom" : "mac",
+ "filename" : "icon_16x16@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "32x32",
+ "idiom" : "mac",
+ "filename" : "icon_32x32.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "32x32",
+ "idiom" : "mac",
+ "filename" : "icon_32x32@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "128x128",
+ "idiom" : "mac",
+ "filename" : "icon_128x128.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "128x128",
+ "idiom" : "mac",
+ "filename" : "icon_128x128@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "256x256",
+ "idiom" : "mac",
+ "filename" : "icon_256x256.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "256x256",
+ "idiom" : "mac",
+ "filename" : "icon_256x256@2x.png",
+ "scale" : "2x"
+ },
+ {
+ "size" : "512x512",
+ "idiom" : "mac",
+ "filename" : "icon_512x512.png",
+ "scale" : "1x"
+ },
+ {
+ "size" : "512x512",
+ "idiom" : "mac",
+ "filename" : "icon_512x512@2x.png",
+ "scale" : "2x"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png
new file mode 100644
index 0000000000..9996f5e598
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png
new file mode 100644
index 0000000000..7a543edbdd
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-60@3x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-76.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-76.png
new file mode 100644
index 0000000000..05a8268104
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-76.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png
new file mode 100644
index 0000000000..bfbca28f5d
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-76@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png
new file mode 100644
index 0000000000..c924b8e497
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-83.5@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40.png
new file mode 100644
index 0000000000..3006f1d348
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@2x.png
new file mode 100644
index 0000000000..5f7d22d364
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@3x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@3x.png
new file mode 100644
index 0000000000..9996f5e598
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small-40@3x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small.png
new file mode 100644
index 0000000000..3433da1196
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png
new file mode 100644
index 0000000000..f7f9f16d1b
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@3x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@3x.png
new file mode 100644
index 0000000000..e9c23601cf
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/Icon-Small@3x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128.png
new file mode 100644
index 0000000000..dd5fccb0a9
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128@2x.png
new file mode 100644
index 0000000000..1909e27511
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_128x128@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16.png
new file mode 100644
index 0000000000..40a701b514
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16@2x.png
new file mode 100644
index 0000000000..06c1a80560
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_16x16@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256.png
new file mode 100644
index 0000000000..1909e27511
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256@2x.png
new file mode 100644
index 0000000000..69c96ef1e2
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_256x256@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32.png
new file mode 100644
index 0000000000..06c1a80560
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32@2x.png
new file mode 100644
index 0000000000..0f94371460
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_32x32@2x.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512.png
new file mode 100644
index 0000000000..69c96ef1e2
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512.png differ
diff --git a/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512@2x.png b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512@2x.png
new file mode 100644
index 0000000000..1d6355f58c
Binary files /dev/null and b/examples/calculator/ios/Assets.xcassets/AppIcon.appiconset/icon_512x512@2x.png differ
diff --git a/examples/calculator/ios/Info.plist b/examples/calculator/ios/Info.plist
new file mode 100644
index 0000000000..114564110e
--- /dev/null
+++ b/examples/calculator/ios/Info.plist
@@ -0,0 +1,47 @@
+
+
+
+
+ CFBundleDevelopmentRegion
+ en
+ CFBundleExecutable
+ Runner
+ CFBundleIdentifier
+ com.yourcompany.calculator
+ CFBundleInfoDictionaryVersion
+ 6.0
+ CFBundleName
+ calculator
+ CFBundlePackageType
+ APPL
+ CFBundleShortVersionString
+ 1.0
+ CFBundleSignature
+ ????
+ CFBundleVersion
+ 1
+ LSRequiresIPhoneOS
+
+ UILaunchStoryboardName
+ LaunchScreen
+ UIRequiredDeviceCapabilities
+
+ arm64
+
+ UISupportedInterfaceOrientations
+
+ UIInterfaceOrientationPortrait
+ UIInterfaceOrientationLandscapeLeft
+ UIInterfaceOrientationLandscapeRight
+
+ UISupportedInterfaceOrientations~ipad
+
+ UIInterfaceOrientationPortrait
+ UIInterfaceOrientationPortraitUpsideDown
+ UIInterfaceOrientationLandscapeLeft
+ UIInterfaceOrientationLandscapeRight
+
+ UIViewControllerBasedStatusBarAppearance
+
+
+
diff --git a/examples/calculator/ios/LaunchScreen.storyboard b/examples/calculator/ios/LaunchScreen.storyboard
new file mode 100644
index 0000000000..78686cd075
--- /dev/null
+++ b/examples/calculator/ios/LaunchScreen.storyboard
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/calculator/lib/calc_expression.dart b/examples/calculator/lib/calc_expression.dart
new file mode 100644
index 0000000000..f7adc8e847
--- /dev/null
+++ b/examples/calculator/lib/calc_expression.dart
@@ -0,0 +1,331 @@
+// 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.
+
+/// A token that composes an expression. There are several kinds of tokens
+/// that represent arithmetic operation symbols, numbers and pieces of numbers.
+/// We need to represent pieces of numbers because the user may have only
+/// entered a partial expression so far.
+class ExpressionToken {
+ ExpressionToken(this.stringRep);
+
+ final String stringRep;
+
+ @override
+ String toString() => stringRep;
+}
+
+/// A token that represents a number.
+class NumberToken extends ExpressionToken {
+ NumberToken(String stringRep, this.number) : super(stringRep);
+
+ NumberToken.fromNumber(num number) : this('$number', number);
+
+ final num number;
+}
+
+/// A token that represents an integer.
+class IntToken extends NumberToken {
+ IntToken(String stringRep) : super(stringRep, int.parse(stringRep));
+}
+
+/// A token that represents a floating point number.
+class FloatToken extends NumberToken {
+ FloatToken(String stringRep) : super(stringRep, _parse(stringRep));
+
+ static double _parse(String stringRep) {
+ String toParse = stringRep;
+ if (toParse.startsWith('.')) toParse = '0' + toParse;
+ if (toParse.endsWith('.')) toParse = toParse + '0';
+ return double.parse(toParse);
+ }
+}
+
+/// A token that represents a number that is the result of a computation.
+class ResultToken extends NumberToken {
+ ResultToken(num number) : super.fromNumber(round(number));
+
+ // rounds |number| to 14 digits of precision. A double precision
+ // floating point number is guaranteed to have at least this many
+ // decimal digits of precision.
+ static num round(num number) {
+ if (number is int) return number;
+ return double.parse(number.toStringAsPrecision(14));
+ }
+}
+
+/// A token that represents the unary minus prefix.
+class LeadingNegToken extends ExpressionToken {
+ LeadingNegToken() : super('-');
+}
+
+enum Operation { Addition, Subtraction, Multiplication, Division }
+
+/// A token that represents an arithmetic operation symbol.
+class OperationToken extends ExpressionToken {
+ OperationToken(Operation operation)
+ : this.operation = operation,
+ super(opString(operation));
+
+ Operation operation;
+
+ static String opString(Operation operation) {
+ switch (operation) {
+ case Operation.Addition:
+ return ' + ';
+ case Operation.Subtraction:
+ return ' - ';
+ case Operation.Multiplication:
+ return ' \u00D7 ';
+ case Operation.Division:
+ return ' \u00F7 ';
+ }
+ }
+}
+
+/// As the user taps different keys the current expression can be in one
+/// of several states.
+enum ExpressionState {
+ // The expression is empty or an operation symbol was just entered.
+ // A new number must be started now.
+ Start,
+ // A minus sign was entered as a leading negative prefix.
+ LeadingNeg,
+ // We are in the midst of a number without a point.
+ Number,
+ // A point was just entered.
+ Point,
+ // We are in the midst of a number with a point.
+ NumberWithPoint,
+ // A result is being displayed
+ Result,
+}
+
+/// An expression that can be displayed in a calculator. It is the result
+/// of a sequence of user entries. It is represented by a sequence of tokens.
+/// Note that the tokens are not in one to one correspondence with the
+/// key taps because we use one token per number, not one token per digit.
+/// A CalcExpression is immutable. The append* methods return a new
+/// CalcExpression that represents the appropriate expression when one
+/// additional key tap occurs.
+class CalcExpression {
+ CalcExpression(this._list, this.state);
+
+ CalcExpression.Empty()
+ : this(new List(), ExpressionState.Start);
+
+ CalcExpression.Result(FloatToken result)
+ : _list = new List(),
+ state = ExpressionState.Result {
+ _list.add(result);
+ }
+
+ // The tokens comprising the expression.
+ final List _list;
+ // The state of the expression.
+ final ExpressionState state;
+
+ // The string representation of the expression. This will be displayed
+ // in the calculator's display panel.
+ @override
+ String toString() {
+ StringBuffer buffer = new StringBuffer('');
+ buffer.writeAll(_list);
+ return buffer.toString();
+ }
+
+ /// Append a digit to the current expression and return a new expression
+ /// representing the result. Returns null to indicate that it is not legal
+ /// to append a digit in the current state.
+ CalcExpression appendDigit(int digit) {
+ ExpressionState newState = ExpressionState.Number;
+ ExpressionToken newToken;
+ final List outList = _list.toList();
+ switch (state) {
+ case ExpressionState.Start:
+ // Start a new number with digit.
+ newToken = new IntToken('$digit');
+ break;
+ case ExpressionState.LeadingNeg:
+ // Replace the leading neg with a negative number starting with digit.
+ outList.removeLast();
+ newToken = new IntToken('-$digit');
+ break;
+ case ExpressionState.Number:
+ ExpressionToken last = outList.removeLast();
+ newToken = new IntToken('${last.stringRep}$digit');
+ break;
+ case ExpressionState.Point:
+ case ExpressionState.NumberWithPoint:
+ ExpressionToken last = outList.removeLast();
+ newState = ExpressionState.NumberWithPoint;
+ newToken = new FloatToken('${last.stringRep}$digit');
+ break;
+ case ExpressionState.Result:
+ // Cannot enter a number now
+ return null;
+ }
+ outList.add(newToken);
+ return new CalcExpression(outList, newState);
+ }
+
+ /// Append a point to the current expression and return a new expression
+ /// representing the result. Returns null to indicate that it is not legal
+ /// to append a point in the current state.
+ CalcExpression appendPoint() {
+ ExpressionToken newToken;
+ final List outList = _list.toList();
+ switch (state) {
+ case ExpressionState.Start:
+ newToken = new FloatToken('.');
+ break;
+ case ExpressionState.LeadingNeg:
+ case ExpressionState.Number:
+ ExpressionToken last = outList.removeLast();
+ newToken = new FloatToken(last.stringRep + '.');
+ break;
+ case ExpressionState.Point:
+ case ExpressionState.NumberWithPoint:
+ case ExpressionState.Result:
+ // Cannot enter a point now
+ return null;
+ }
+ outList.add(newToken);
+ return new CalcExpression(outList, ExpressionState.Point);
+ }
+
+ /// Append an operation symbol to the current expression and return a new
+ /// expression representing the result. Returns null to indicate that it is not
+ /// legal to append an operation symbol in the current state.
+ CalcExpression appendOperation(Operation op) {
+ switch (state) {
+ case ExpressionState.Start:
+ case ExpressionState.LeadingNeg:
+ case ExpressionState.Point:
+ // Cannot enter operation now.
+ return null;
+ case ExpressionState.Number:
+ case ExpressionState.NumberWithPoint:
+ case ExpressionState.Result:
+ break;
+ }
+ final List outList = _list.toList();
+ outList.add(new OperationToken(op));
+ return new CalcExpression(outList, ExpressionState.Start);
+ }
+
+ /// Append a leading minus sign to the current expression and return a new
+ /// expression representing the result. Returns null to indicate that it is not
+ /// legal to append a leading minus sign in the current state.
+ CalcExpression appendLeadingNeg() {
+ switch (state) {
+ case ExpressionState.Start:
+ break;
+ case ExpressionState.LeadingNeg:
+ case ExpressionState.Point:
+ case ExpressionState.Number:
+ case ExpressionState.NumberWithPoint:
+ case ExpressionState.Result:
+ // Cannot enter leading neg now.
+ return null;
+ }
+ final List outList = _list.toList();
+ outList.add(new LeadingNegToken());
+ return new CalcExpression(outList, ExpressionState.LeadingNeg);
+ }
+
+ /// Append a minus sign to the current expression and return a new expression
+ /// representing the result. Returns null to indicate that it is not legal
+ /// to append a minus sign in the current state. Depending on the current
+ /// state the minus sign will be interpretted as either a leading negative
+ /// sign or a subtraction operation.
+ CalcExpression appendMinus() {
+ switch (state) {
+ case ExpressionState.Start:
+ return appendLeadingNeg();
+ case ExpressionState.LeadingNeg:
+ case ExpressionState.Point:
+ case ExpressionState.Number:
+ case ExpressionState.NumberWithPoint:
+ case ExpressionState.Result:
+ return appendOperation(Operation.Subtraction);
+ }
+ }
+
+ /// Computes the result of the current expression and returns a new
+ /// ResultExpression containing the result. Returns null to indicate that
+ /// it is not legal to compute a result in the current state.
+ CalcExpression computeResult() {
+ switch (state) {
+ case ExpressionState.Start:
+ case ExpressionState.LeadingNeg:
+ case ExpressionState.Point:
+ case ExpressionState.Result:
+ // Cannot compute result now.
+ return null;
+ case ExpressionState.Number:
+ case ExpressionState.NumberWithPoint:
+ break;
+ }
+
+ // We make a copy of _list because CalcExpressions are supposed to
+ // be immutable.
+ List list = _list.toList();
+ // We obey order-of-operations by computing the sum of the 'terms',
+ // where a "term" is defined to be a sequence of numbers separated by
+ // multiplcation or division symbols.
+ num currentTermValue = removeNextTerm(list);
+ while (list.length > 0) {
+ OperationToken opToken = list.removeAt(0);
+ num nextTermValue = removeNextTerm(list);
+ switch (opToken.operation) {
+ case Operation.Addition:
+ currentTermValue += nextTermValue;
+ break;
+ case Operation.Subtraction:
+ currentTermValue -= nextTermValue;
+ break;
+ case Operation.Multiplication:
+ case Operation.Division:
+ // Logic error.
+ assert(false);
+ }
+ }
+ final List outList = new List();
+ outList.add(new ResultToken(currentTermValue));
+ return new CalcExpression(outList, ExpressionState.Result);
+ }
+
+ // Removes the next "term" from |list| and returns its numeric value.
+ // A "term" is a sequence of number tokens separated by multiplication
+ // and division symbols.
+ static num removeNextTerm(List list) {
+ assert(list != null && list.length >= 1);
+ final NumberToken firstNumToken = list.removeAt(0);
+ num currentValue = firstNumToken.number;
+ while (list.length > 0) {
+ bool isDivision = false;
+ OperationToken nextOpToken = list.first;
+ switch (nextOpToken.operation) {
+ case Operation.Addition:
+ case Operation.Subtraction:
+ // We have reached the end of the current term
+ return currentValue;
+ case Operation.Multiplication:
+ break;
+ case Operation.Division:
+ isDivision = true;
+ }
+ // Remove the operation token.
+ list.removeAt(0);
+ // Remove the next number token.
+ final NumberToken nextNumToken = list.removeAt(0);
+ num nextNumber = nextNumToken.number;
+ if (isDivision)
+ currentValue /= nextNumber;
+ else
+ currentValue *= nextNumber;
+ }
+ return currentValue;
+ }
+}
diff --git a/examples/calculator/lib/main.dart b/examples/calculator/lib/main.dart
new file mode 100644
index 0000000000..21ee62fa99
--- /dev/null
+++ b/examples/calculator/lib/main.dart
@@ -0,0 +1,263 @@
+// 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:flutter/material.dart';
+
+import 'calc_expression.dart';
+
+// A calculator application.
+void main() {
+ runApp(new MaterialApp(
+ title: 'Calculator',
+ theme: new ThemeData(primarySwatch: Colors.blue),
+ home: new Calculator()));
+}
+
+class Calculator extends StatefulWidget {
+ Calculator({Key key}) : super(key: key);
+
+ @override
+ _CalculatorState createState() => new _CalculatorState();
+}
+
+class _CalculatorState extends State {
+ // As the user taps keys we update the current |_expression| and we also
+ // keep a stack of previous expressions so we can return to earlier states
+ // when the user hits the DEL key.
+ final List _expressionStack = [];
+ CalcExpression _expression = new CalcExpression.Empty();
+
+ // Make |expression| the current expression and push the previous current
+ // expression onto the stack.
+ void pushExpression(CalcExpression expression) {
+ _expressionStack.add(_expression);
+ _expression = expression;
+ }
+
+ // Pop the top expression off of the stack and make it the current expression.
+ void popCalcExpression() {
+ if (_expressionStack.length > 0) {
+ _expression = _expressionStack.removeLast();
+ } else {
+ _expression = new CalcExpression.Empty();
+ }
+ }
+
+ // Set |resultExpression| to the currrent expression and clear the stack.
+ void setResult(CalcExpression resultExpression) {
+ _expressionStack.clear();
+ _expression = resultExpression;
+ }
+
+ void handleNumberTap(int n) {
+ final CalcExpression expression = _expression.appendDigit(n);
+ if (expression != null) {
+ setState(() {
+ pushExpression(expression);
+ });
+ }
+ }
+
+ void handlePointTap() {
+ final CalcExpression expression = _expression.appendPoint();
+ if (expression != null) {
+ setState(() {
+ pushExpression(expression);
+ });
+ }
+ }
+
+ void handlePlusTap() {
+ final CalcExpression expression =
+ _expression.appendOperation(Operation.Addition);
+ if (expression != null) {
+ setState(() {
+ pushExpression(expression);
+ });
+ }
+ }
+
+ void handleMinusTap() {
+ final CalcExpression expression = _expression.appendMinus();
+ if (expression != null) {
+ setState(() {
+ pushExpression(expression);
+ });
+ }
+ }
+
+ void handleMultTap() {
+ final CalcExpression expression =
+ _expression.appendOperation(Operation.Multiplication);
+ if (expression != null) {
+ setState(() {
+ pushExpression(expression);
+ });
+ }
+ }
+
+ void handleDivTap() {
+ final CalcExpression expression =
+ _expression.appendOperation(Operation.Division);
+ if (expression != null) {
+ setState(() {
+ pushExpression(expression);
+ });
+ }
+ }
+
+ void handleEqualsTap() {
+ final CalcExpression resultExpression = _expression.computeResult();
+ if (resultExpression != null) {
+ setState(() {
+ setResult(resultExpression);
+ });
+ }
+ }
+
+ void handleDelTap() {
+ setState(() {
+ popCalcExpression();
+ });
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return new Scaffold(
+ appBar: new AppBar(title: new Text('Calculator')),
+ body: new Column(children: [
+ // Give the key-pad 3/5 of the vertical space and the display
+ // 2/5.
+ new CalcDisplay(2, _expression.toString()),
+ new KeyPad(3, calcState: this)
+ ]));
+ }
+}
+
+class CalcDisplay extends StatelessWidget {
+ CalcDisplay(this._flex, this._contents);
+
+ final int _flex;
+ final String _contents;
+
+ @override
+ Widget build(BuildContext context) {
+ return new Flexible(
+ flex: _flex,
+ child: new Center(child: new Text(_contents,
+ style: const TextStyle(color: Colors.black, fontSize: 24.0))));
+ }
+}
+
+class KeyPad extends StatelessWidget {
+ KeyPad(this._flex, {this.calcState});
+
+ final int _flex;
+ final _CalculatorState calcState;
+
+ @override
+ Widget build(BuildContext context) {
+ return new Flexible(
+ flex: _flex,
+ child: new Row(children: [
+ new MainKeyPad(calcState: calcState),
+ new OpKeyPad(calcState: calcState),
+ ]));
+ }
+}
+
+class MainKeyPad extends StatelessWidget {
+ MainKeyPad({this.calcState});
+
+ final _CalculatorState calcState;
+
+ @override
+ Widget build(BuildContext context) {
+ return new Flexible(
+ // We set flex equal to the number of columns so that the main keypad
+ // and the op keypad have sizes proportional to their number of
+ // columns.
+ flex: 3,
+ child: new Material(
+ type: MaterialType.canvas,
+ elevation: 12,
+ color: Colors.grey[800],
+ child: new Column(children: [
+ new KeyRow([
+ new NumberKey(7, calcState),
+ new NumberKey(8, calcState),
+ new NumberKey(9, calcState)
+ ]),
+ new KeyRow([
+ new NumberKey(4, calcState),
+ new NumberKey(5, calcState),
+ new NumberKey(6, calcState)
+ ]),
+ new KeyRow([
+ new NumberKey(1, calcState),
+ new NumberKey(2, calcState),
+ new NumberKey(3, calcState)
+ ]),
+ new KeyRow([
+ new CalcKey('.', calcState.handlePointTap),
+ new NumberKey(0, calcState),
+ new CalcKey('=', calcState.handleEqualsTap),
+ ])
+ ])));
+ }
+}
+
+class OpKeyPad extends StatelessWidget {
+ OpKeyPad({this.calcState});
+
+ final _CalculatorState calcState;
+
+ @override
+ Widget build(BuildContext context) {
+ return new Flexible(child: new Material(
+ type: MaterialType.canvas,
+ elevation: 24,
+ color: Colors.grey[700],
+ child: new Column(children: [
+ new CalcKey('DEL', calcState.handleDelTap),
+ new CalcKey('\u00F7', calcState.handleDivTap),
+ new CalcKey('\u00D7', calcState.handleMultTap),
+ new CalcKey('-', calcState.handleMinusTap),
+ new CalcKey('+', calcState.handlePlusTap)
+ ])));
+ }
+}
+
+class KeyRow extends StatelessWidget {
+ KeyRow(this.keys);
+
+ final List keys;
+
+ @override
+ Widget build(BuildContext context) {
+ return new Flexible(child: new Row(
+ mainAxisAlignment: MainAxisAlignment.center, children: this.keys));
+ }
+}
+
+class CalcKey extends StatelessWidget {
+ final String text;
+ final GestureTapCallback onTap;
+ CalcKey(this.text, this.onTap);
+
+ @override
+ Widget build(BuildContext context) {
+ return new Flexible(child: new Container(child: new InkResponse(
+ onTap: this.onTap,
+ child: new Center(child: new Text(this.text,
+ style: const TextStyle(color: Colors.white, fontSize: 32.0))))));
+ }
+}
+
+class NumberKey extends CalcKey {
+ NumberKey(int value, _CalculatorState calcState)
+ : super('$value', () {
+ calcState.handleNumberTap(value);
+ });
+}
diff --git a/examples/calculator/pubspec.yaml b/examples/calculator/pubspec.yaml
new file mode 100644
index 0000000000..cad2f30978
--- /dev/null
+++ b/examples/calculator/pubspec.yaml
@@ -0,0 +1,12 @@
+name: calculator
+description: A new flutter project.
+dependencies:
+ flutter:
+ path: ../../packages/flutter
+
+dev_dependencies:
+ test: any # flutter_test provides the version constraints
+ flutter_test:
+ path: ../../packages/flutter_test
+ flutter_driver:
+ path: ../../packages/flutter_driver
diff --git a/examples/calculator/test/calc_expression_test.dart b/examples/calculator/test/calc_expression_test.dart
new file mode 100644
index 0000000000..38f2ae4387
--- /dev/null
+++ b/examples/calculator/test/calc_expression_test.dart
@@ -0,0 +1,88 @@
+// 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:calculator/calc_expression.dart';
+import 'package:test/test.dart';
+
+void main() {
+ test('Test order of operations: 12 + 3 * 4 = 24', () {
+ CalcExpression expression = new CalcExpression.Empty();
+ expression = expression.appendDigit(1);
+ expression = expression.appendDigit(2);
+ expression = expression.appendOperation(Operation.Addition);
+ expression = expression.appendDigit(3);
+ expression = expression.appendOperation(Operation.Multiplication);
+ expression = expression.appendDigit(4);
+ expression = expression.computeResult();
+ expect(expression.state, equals(ExpressionState.Result));
+ expect(expression.toString(), equals("24"));
+ });
+
+ test('Test floating point 0.1 + 0.2 = 0.3', () {
+ CalcExpression expression = new CalcExpression.Empty();
+ expression = expression.appendDigit(0);
+ expression = expression.appendPoint();
+ expression = expression.appendDigit(1);
+ expression = expression.appendOperation(Operation.Addition);
+ expression = expression.appendDigit(0);
+ expression = expression.appendPoint();
+ expression = expression.appendDigit(2);
+ expression = expression.computeResult();
+ expect(expression.state, equals(ExpressionState.Result));
+ expect(expression.toString(), equals("0.3"));
+ });
+
+ test('Test floating point 1.0/10.0 = 0.1', () {
+ CalcExpression expression = new CalcExpression.Empty();
+ expression = expression.appendDigit(1);
+ expression = expression.appendPoint();
+ expression = expression.appendDigit(0);
+ expression = expression.appendOperation(Operation.Division);
+ expression = expression.appendDigit(1);
+ expression = expression.appendDigit(0);
+ expression = expression.appendPoint();
+ expression = expression.appendDigit(0);
+ expression = expression.computeResult();
+ expect(expression.state, equals(ExpressionState.Result));
+ expect(expression.toString(), equals("0.1"));
+ });
+
+ test('Test 1/0 = Infinity', () {
+ CalcExpression expression = new CalcExpression.Empty();
+ expression = expression.appendDigit(1);
+ expression = expression.appendOperation(Operation.Division);
+ expression = expression.appendDigit(0);
+ expression = expression.computeResult();
+ expect(expression.state, equals(ExpressionState.Result));
+ expect(expression.toString(), equals("Infinity"));
+ });
+
+ test('Test use result in next calculation: 1 + 1 = 2 + 1 = 3 + 1 = 4', () {
+ CalcExpression expression = new CalcExpression.Empty();
+ expression = expression.appendDigit(1);
+ expression = expression.appendOperation(Operation.Addition);
+ expression = expression.appendDigit(1);
+ expression = expression.computeResult();
+ expression = expression.appendOperation(Operation.Addition);
+ expression = expression.appendDigit(1);
+ expression = expression.computeResult();
+ expression = expression.appendOperation(Operation.Addition);
+ expression = expression.appendDigit(1);
+ expression = expression.computeResult();
+ expect(expression.state, equals(ExpressionState.Result));
+ expect(expression.toString(), equals("4"));
+ });
+
+ test('Test minus -3 - -2 = -1', () {
+ CalcExpression expression = new CalcExpression.Empty();
+ expression = expression.appendMinus();
+ expression = expression.appendDigit(3);
+ expression = expression.appendMinus();
+ expression = expression.appendMinus();
+ expression = expression.appendDigit(2);
+ expression = expression.computeResult();
+ expect(expression.state, equals(ExpressionState.Result));
+ expect(expression.toString(), equals("-1"));
+ });
+}
diff --git a/examples/calculator/test/smoke_test.dart b/examples/calculator/test/smoke_test.dart
new file mode 100644
index 0000000000..6f184a8496
--- /dev/null
+++ b/examples/calculator/test/smoke_test.dart
@@ -0,0 +1,36 @@
+// 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:calculator/main.dart' as calculator;
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
+ if (binding is LiveTestWidgetsFlutterBinding)
+ binding.allowAllFrames = true;
+
+ // We press the "1" and the "2" buttons and check that the display
+ // reads "12".
+ testWidgets('Flutter calculator app smoke test', (WidgetTester tester) async {
+ calculator.main(); // builds the app and schedules a frame but doesn't trigger one
+ await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
+ await tester.pump(); // triggers a frame
+
+ Finder oneButton = find.widgetWithText(InkResponse, '1');
+ expect(oneButton, findsOneWidget);
+
+ Finder twoButton = find.widgetWithText(InkResponse, '2');
+ expect(twoButton, findsOneWidget);
+
+ await tester.tap(oneButton);
+ await tester.pump();
+ await tester.tap(twoButton);
+ await tester.pump();
+ await tester.pump(const Duration(seconds: 1)); // Wait until it has finished.
+
+ Finder display = find.widgetWithText(Flexible, '12');
+ expect(display, findsOneWidget);
+ });
+}
diff --git a/examples/flutter_gallery/test/smoke_test.dart b/examples/flutter_gallery/test/smoke_test.dart
index 0998f7685e..8be5022b6d 100644
--- a/examples/flutter_gallery/test/smoke_test.dart
+++ b/examples/flutter_gallery/test/smoke_test.dart
@@ -9,12 +9,16 @@ import 'package:flutter_gallery/gallery/item.dart' as flutter_gallery_item;
import 'package:flutter_gallery/main.dart' as flutter_gallery_main;
// Warning: the following strings must be kept in sync with GalleryHome.
-const List demoCategories = const ['Demos', 'Components', 'Style'];
+const List demoCategories = const [
+ 'Demos',
+ 'Components',
+ 'Style'
+];
Finder findGalleryItemByRouteName(WidgetTester tester, String routeName) {
return find.byWidgetPredicate((Widget widget) {
- return widget is flutter_gallery_item.GalleryItem
- && widget.routeName == routeName;
+ return widget is flutter_gallery_item.GalleryItem &&
+ widget.routeName == routeName;
});
}
@@ -24,7 +28,8 @@ Finder byTooltip(WidgetTester tester, String message) {
});
}
-Finder findNavigationMenuButton(WidgetTester tester) => byTooltip(tester, 'Open navigation menu');
+Finder findNavigationMenuButton(WidgetTester tester) =>
+ byTooltip(tester, 'Open navigation menu');
Finder findBackButton(WidgetTester tester) => byTooltip(tester, 'Back');
@@ -38,7 +43,8 @@ Future smokeDemo(WidgetTester tester, String routeName) async {
await tester.tap(menuItem);
await tester.pump(); // Launch the demo.
- await tester.pump(const Duration(seconds: 1)); // Wait until the demo has opened.
+ await tester
+ .pump(const Duration(seconds: 1)); // Wait until the demo has opened.
// Go back
Finder backButton = findBackButton(tester);
@@ -50,12 +56,13 @@ Future smokeDemo(WidgetTester tester, String routeName) async {
}
void main() {
- TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
- if (binding is LiveTestWidgetsFlutterBinding)
- binding.allowAllFrames = true;
+ TestWidgetsFlutterBinding binding =
+ TestWidgetsFlutterBinding.ensureInitialized();
+ if (binding is LiveTestWidgetsFlutterBinding) binding.allowAllFrames = true;
testWidgets('Flutter Gallery app smoke test', (WidgetTester tester) async {
- flutter_gallery_main.main(); // builds the app and schedules a frame but doesn't trigger one
+ flutter_gallery_main
+ .main(); // builds the app and schedules a frame but doesn't trigger one
await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
await tester.pump(); // triggers a frame
@@ -63,14 +70,16 @@ void main() {
for (String category in demoCategories.reversed) {
await tester.tap(find.text(category));
await tester.pump();
- await tester.pump(const Duration(seconds: 1)); // Wait until the menu has expanded.
+ await tester.pump(
+ const Duration(seconds: 1)); // Wait until the menu has expanded.
}
final List scrollDeltas = new List();
double previousY = tester.getTopRight(find.text(demoCategories[0])).y;
final List routeNames = flutter_gallery_app.kRoutes.keys.toList();
for (String routeName in routeNames) {
- final double y = tester.getTopRight(findGalleryItemByRouteName(tester, routeName)).y;
+ final double y =
+ tester.getTopRight(findGalleryItemByRouteName(tester, routeName)).y;
scrollDeltas.add(previousY - y);
previousY = y;
}
@@ -79,7 +88,8 @@ void main() {
for (int i = 0; i < routeNames.length; i += 1) {
final String routeName = routeNames[i];
await smokeDemo(tester, routeName);
- await tester.scroll(findGalleryItemByRouteName(tester, routeName), new Offset(0.0, scrollDeltas[i]));
+ await tester.scroll(findGalleryItemByRouteName(tester, routeName),
+ new Offset(0.0, scrollDeltas[i]));
await tester.pump();
}
@@ -87,7 +97,8 @@ void main() {
expect(navigationMenuButton, findsOneWidget);
await tester.tap(navigationMenuButton);
await tester.pump(); // Start opening drawer.
- await tester.pump(const Duration(seconds: 1)); // Wait until it's really opened.
+ await tester
+ .pump(const Duration(seconds: 1)); // Wait until it's really opened.
// switch theme
await tester.tap(find.text('Dark'));