Skip to content

Instantly share code, notes, and snippets.

@aab29
aab29 / main.dart
Created July 11, 2018 19:33
Using Dart's generator functions to calculate the blended color values in a horizontal gradient
class Color {
final double red;
final double green;
final double blue;
const Color(this.red, this.green, this.blue);
static double blendedChannel(double leftChannel, double rightChannel, double progress) =>
leftChannel * (1.0 - progress) + rightChannel * progress;
@aab29
aab29 / main.dart
Created July 10, 2018 19:13
An example of how concise you can make your constructor code in Dart
class City {
String name;
String countryName;
int population;
bool isCapital;
City(this.name, this.countryName, this.population, {this.isCapital = false});
@override
@aab29
aab29 / main.dart
Last active July 6, 2018 20:37
Showing off Dart's two syntaxes for optional arguments: optional named arguments and optional positional arguments.
enum SheepSoundLength {
short,
medium,
long
}
const LettersCountBySheepSoundLength = const {
SheepSoundLength.short : 1,
SheepSoundLength.medium : 3,
@aab29
aab29 / main.dart
Last active July 6, 2018 20:36
Demonstrating the power of Dart's cascade syntax with an arbitrarily intricate Sentence class.
enum SentencePurpose { declarative, imperative, interrogative, exclamatory }
const endPunctuationByPurpose = const {
SentencePurpose.declarative: ".",
SentencePurpose.imperative: ".",
SentencePurpose.interrogative: "?",
SentencePurpose.exclamatory: "!"
};
const underWaterSoundsByCharacter = const {
@aab29
aab29 / main.dart
Last active July 6, 2018 20:40
A silly simulation of a boat picking up cargo boxes at a port to demonstrate Dart's fat arrow syntax
import "dart:math";
class Port {
static final randomGenerator = new Random();
static const cargoNames = const [
"guano",
"fuzzy dice",
"weasles",
"bubble wrap",
"shoelaces",
@aab29
aab29 / main.dart
Last active July 6, 2018 20:35
Dart's syntax is very familiar
void main() {
var familiarLanguages = ["C", "C++", "Objective-C", "C#", "Java", "JavaScript"];
print("Welcome to Dart!");
for (var language in familiarLanguages) {
print("If you know $language, you already know most of Dart.");
}