mirror of
https://github.com/rizonesoft/Notepad3.git
synced 2026-06-20 21:13:25 +08:00
172 lines
3.6 KiB
Dart
172 lines
3.6 KiB
Dart
// This is a normal, one-line comment.
|
|
|
|
/// This is a documentation comment, used to document libraries,
|
|
/// classes, and their members. Tools like IDEs and dartdoc treat
|
|
/// doc comments specially.
|
|
|
|
/* Comments like these are also supported. */
|
|
|
|
// Importing core libraries
|
|
import 'dart:math';
|
|
|
|
// Importing libraries from external packages
|
|
import 'package:test/test.dart';
|
|
|
|
// Importing files
|
|
import 'path/to/my_other_file.dart';
|
|
|
|
|
|
var name = 'Voyager I';
|
|
var year = 1977;
|
|
var antennaDiameter = 3.7;
|
|
var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];
|
|
var image = {
|
|
'tags': ['saturn'],
|
|
'url': '//path/to/saturn.jpg'
|
|
};
|
|
|
|
if (year >= 2001) {
|
|
print('21st century');
|
|
} else if (year >= 1901) {
|
|
print('20th century');
|
|
}
|
|
|
|
for (var object in flybyObjects) {
|
|
print(object);
|
|
}
|
|
|
|
for (int month = 1; month <= 12; month++) {
|
|
print(month);
|
|
}
|
|
|
|
while (year < 2016) {
|
|
year += 1;
|
|
}
|
|
|
|
int fibonacci(int n) {
|
|
if (n == 0 || n == 1) return n;
|
|
return fibonacci(n - 1) + fibonacci(n - 2);
|
|
}
|
|
|
|
var result = fibonacci(20);
|
|
|
|
class Spacecraft {
|
|
String name;
|
|
DateTime launchDate;
|
|
|
|
// Constructor, with syntactic sugar for assignment to members.
|
|
Spacecraft(this.name, this.launchDate) {
|
|
// Initialization code goes here.
|
|
}
|
|
|
|
// Named constructor that forwards to the default one.
|
|
Spacecraft.unlaunched(String name) : this(name, null);
|
|
|
|
int get launchYear =>
|
|
launchDate?.year; // read-only non-final property
|
|
|
|
// Method.
|
|
void describe() {
|
|
print('Spacecraft: $name');
|
|
if (launchDate != null) {
|
|
int years =
|
|
DateTime.now().difference(launchDate).inDays ~/
|
|
365;
|
|
print('Launched: $launchYear ($years years ago)');
|
|
} else {
|
|
print('Unlaunched');
|
|
}
|
|
}
|
|
}
|
|
|
|
var voyager = Spacecraft('Voyager I', DateTime(1977, 9, 5));
|
|
voyager.describe();
|
|
|
|
var voyager3 = Spacecraft.unlaunched('Voyager III');
|
|
voyager3.describe();
|
|
|
|
|
|
class Orbiter extends Spacecraft {
|
|
double altitude;
|
|
Orbiter(String name, DateTime launchDate, this.altitude)
|
|
: super(name, launchDate);
|
|
}
|
|
|
|
|
|
class Piloted {
|
|
int astronauts = 1;
|
|
void describeCrew() {
|
|
print('Number of astronauts: $astronauts');
|
|
}
|
|
}
|
|
|
|
|
|
abstract class Describable {
|
|
void describe();
|
|
|
|
void describeWithEmphasis() {
|
|
print('=========');
|
|
describe();
|
|
print('=========');
|
|
}
|
|
}
|
|
|
|
|
|
const oneSecond = Duration(seconds: 1);
|
|
// ···
|
|
Future<void> printWithDelay(String message) async {
|
|
await Future.delayed(oneSecond);
|
|
print(message);
|
|
}
|
|
|
|
|
|
Future<void> printWithDelay(String message) {
|
|
return Future.delayed(oneSecond).then((_) {
|
|
print(message);
|
|
});
|
|
}
|
|
|
|
|
|
Future<void> createDescriptions(Iterable<String> objects) async {
|
|
for (var object in objects) {
|
|
try {
|
|
var file = File('$object.txt');
|
|
if (await file.exists()) {
|
|
var modified = await file.lastModified();
|
|
print(
|
|
'File for $object already exists. It was modified on $modified.');
|
|
continue;
|
|
}
|
|
await file.create();
|
|
await file.writeAsString('Start describing $object in this file.');
|
|
} on IOException catch (e) {
|
|
print('Cannot create description for $object: $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Stream<String> report(Spacecraft craft, Iterable<String> objects) async* {
|
|
for (var object in objects) {
|
|
await Future.delayed(oneSecond);
|
|
yield '${craft.name} flies by $object';
|
|
}
|
|
}
|
|
|
|
if (astronauts == 0) {
|
|
throw StateError('No astronauts.');
|
|
}
|
|
|
|
|
|
try {
|
|
for (var object in flybyObjects) {
|
|
var description = await File('$object.txt').readAsString();
|
|
print(description);
|
|
}
|
|
} on IOException catch (e) {
|
|
print('Could not describe object: $e');
|
|
} finally {
|
|
flybyObjects.clear();
|
|
}
|
|
|