Commit e158527d authored by Hans Muller's avatar Hans Muller

Restore stock_arrow element of the stocks2 demo

This version doesn't add any visual punch.

R=abarth@chromium.org

Review URL: https://codereview.chromium.org/1172513002.
parent b7e81c79
...@@ -2,91 +2,61 @@ ...@@ -2,91 +2,61 @@
// 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:math';
import 'package:sky/framework/fn2.dart'; import 'package:sky/framework/fn2.dart';
import 'package:vector_math/vector_math.dart';
import 'package:sky/framework/rendering/box.dart';
import 'package:sky/framework/rendering/object.dart';
import 'package:sky/framework/theme2/colors.dart' as colors;
class StockArrow extends Component { import 'dart:math' as math;
static final Style _style = new Style(''' import 'dart:sky' as sky;
width: 40px;
height: 40px;
align-items: center;
justify-content: center;
border-radius: 40px;
margin-right: 16px;
border: 1px solid transparent;'''
);
static final Style _upStyle = new Style('''
width: 0;
height: 0;
border-left: 9px solid transparent;
border-right: 9px solid transparent;
margin-bottom: 3px;
border-bottom: 9px solid white;'''
);
static final Style _downStyle = new Style('''
width: 0;
height: 0;
border-left: 9px solid transparent;
border-right: 9px solid transparent;
margin-top: 3px;
border-top: 9px solid white'''
);
class StockArrow extends Component {
double percentChange; double percentChange;
StockArrow({ Object key, this.percentChange }) : super(key: key); StockArrow({ Object key, this.percentChange }) : super(key: key);
// TODO(abarth): These should use sky/framework/theme/colors.dart.
final List<String> _kRedColors = [
'#E57373',
'#EF5350',
'#F44336',
'#E53935',
'#D32F2F',
'#C62828',
'#B71C1C',
];
// TODO(abarth): These should use sky/framework/theme/colors.dart.
final List<String> _kGreenColors = [
'#81C784',
'#66BB6A',
'#4CAF50',
'#43A047',
'#388E3C',
'#2E7D32',
'#1B5E20',
];
int _colorIndexForPercentChange(double percentChange) { int _colorIndexForPercentChange(double percentChange) {
// Currently the max is 10%.
double maxPercent = 10.0; double maxPercent = 10.0;
return max(0, ((percentChange.abs() / maxPercent) * _kGreenColors.length).floor()); double normalizedPercentChange = math.min(percentChange.abs(), maxPercent) / maxPercent;
return 100 + (normalizedPercentChange * 8.0).floor() * 100;
} }
String _colorForPercentChange(double percentChange) { Color _colorForPercentChange(double percentChange) {
if (percentChange > 0) if (percentChange > 0)
return _kGreenColors[_colorIndexForPercentChange(percentChange)]; return colors.Green[_colorIndexForPercentChange(percentChange)];
return _kRedColors[_colorIndexForPercentChange(percentChange)]; return colors.Red[_colorIndexForPercentChange(percentChange)];
} }
UINode build() { UINode build() {
String border = _colorForPercentChange(percentChange).toString(); // TODO(jackson): This should change colors with the theme
bool up = percentChange > 0; Color color = _colorForPercentChange(percentChange);
String type = up ? 'bottom' : 'top'; const double size = 40.0;
var arrow = new CustomPaint(callback: (sky.Canvas canvas) {
return new FlexContainer( Paint paint = new Paint()..color = color;
inlineStyle: 'border-color: $border', paint.setStyle(sky.PaintingStyle.stroke);
direction: FlexDirection.horizontal, paint.strokeWidth = 2.0;
style: _style, var padding = paint.strokeWidth * 3.0;
children: [ var w = size - padding * 2.0;
new Container( var h = size - padding * 2.0;
inlineStyle: 'border-$type-color: $border', canvas.save();
style: up ? _upStyle : _downStyle canvas.translate(padding, padding);
) if (percentChange < 0.0) {
] var cx = w / 2.0;
); var cy = h / 2.0;
canvas.translate(cx, cy);
canvas.rotate(math.PI);
canvas.translate(-cx, -cy);
}
canvas.drawLine(0.0, h, w, h, paint);
canvas.drawLine(w, h, w / 2.0, 0.0, paint);
canvas.drawLine(w / 2.0, 0.0, 0.0, h, paint);
canvas.restore();
});
return new Container(
child: arrow,
width: size,
height: size,
margin: const EdgeDims.symmetric(horizontal: 5.0));
} }
} }
...@@ -7,7 +7,7 @@ import 'package:sky/framework/components2/ink_well.dart'; ...@@ -7,7 +7,7 @@ import 'package:sky/framework/components2/ink_well.dart';
import 'package:sky/framework/fn2.dart'; import 'package:sky/framework/fn2.dart';
import 'package:sky/framework/rendering/box.dart'; import 'package:sky/framework/rendering/box.dart';
import 'package:sky/framework/theme/typography.dart' as typography; import 'package:sky/framework/theme/typography.dart' as typography;
// import 'stock_arrow.dart'; import 'stock_arrow.dart';
import 'stock_data.dart'; import 'stock_data.dart';
class StockRow extends Component { class StockRow extends Component {
...@@ -27,9 +27,9 @@ class StockRow extends Component { ...@@ -27,9 +27,9 @@ class StockRow extends Component {
changeInPrice = "+" + changeInPrice; changeInPrice = "+" + changeInPrice;
List<UINode> children = [ List<UINode> children = [
// new StockArrow( new StockArrow(
// percentChange: stock.percentChange percentChange: stock.percentChange
// ), ),
new FlexExpandingChild(new Text(stock.symbol)), new FlexExpandingChild(new Text(stock.symbol)),
new Container( new Container(
width: 75.0, width: 75.0,
......
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