Skip to content

Instantly share code, notes, and snippets.

@bluemyria
Last active October 2, 2019 12:04
Show Gist options
  • Save bluemyria/0a4effdd4a3982d1a89bf71c7a08d7d1 to your computer and use it in GitHub Desktop.
Save bluemyria/0a4effdd4a3982d1a89bf71c7a08d7d1 to your computer and use it in GitHub Desktop.
Dart / Flutter myNotes
import 'dart:math';
/*
abstract class Shape {
num get area;
}
*/
abstract class Shape {
factory Shape(String type) {
if (type == 'circle') return Circle(2);
if (type == 'square') return Square(2);
throw 'Can\'t create $type.';
}
num get area;
}
class CircleMock implements Circle {
num area;
num radius;
}
class Circle implements Shape {
final num radius;
Circle(this.radius);
num get area => pi * pow(radius, 2);
}
class Square implements Shape {
final num side;
Square(this.side);
num get area => pow(side, 2);
}
Shape shapeFactory(String type) {
if (type == 'circle') return Circle(2);
if (type == 'square') return Square(2);
throw 'Can\'t create $type.';
}
main() {
// final circle = Circle(3.45);
// final square = Square(5);
/*
final circle = shapeFactory('circle');
final square = shapeFactory('square');
*/
final circle = Shape('circle');
final square = Shape('square');
print(circle.area);
print(square.area);
}
import 'dart:math';
class Rectangle {
int width;
int height;
Point origin;
Rectangle({this.origin = const Point(0, 0), this.width = 0, this.height = 0});
@override
String toString() =>
'Origin: (${origin.x}, ${origin.y}), width: $width, height: $height';
}
main() {
print(Rectangle(origin: const Point(10, 20), width: 100, height: 200));
print(Rectangle(origin: const Point(10, 10)));
print(Rectangle(width: 200));
print(Rectangle());
}
import 'dart:math';
abstract class Shape {
num get area;
}
class Circle implements Shape {
final num radius;
Circle(this.radius);
num get area => pi * pow(radius, 2);
}
class Square implements Shape {
final num side;
Square(this.side);
num get area => pow(side, 2);
}
main() {
final circle = Circle(3.45);
final square = Square(5);
print(circle.area);
print(square.area);
}
String scream(int length) => "A${'a' * length}h!";
main() {
final values = [1, 2, 3, 5, 10, 50];
for (var length in values) {
print(scream(length));
}
// OR
values.map(scream).forEach(print);
// OR
values.skip(1).take(3).map(scream).forEach(print);
}
@bluemyria
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment