forked from flutter/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.dart
More file actions
78 lines (66 loc) · 2.28 KB
/
utils.dart
File metadata and controls
78 lines (66 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import 'dart:math';
import 'package:english_words/english_words.dart';
// ignore: implementation_imports
import 'package:flutter/material.dart';
// This file has a number of platform-agnostic non-Widget utility functions.
const _myListOfRandomColors = [
Colors.red,
Colors.blue,
Colors.teal,
Colors.yellow,
Colors.amber,
Colors.deepOrange,
Colors.green,
Colors.indigo,
Colors.lime,
Colors.pink,
Colors.orange,
];
final _random = Random();
// Avoid customizing the word generator, which can be slow.
// https://github.com/filiph/english_words/issues/9
final wordPairIterator = generateWordPairs();
String generateRandomHeadline() {
final artist = capitalizePair(wordPairIterator.first);
switch (_random.nextInt(10)) {
case 0:
return '$artist says ${nouns[_random.nextInt(nouns.length)]}';
case 1:
return '$artist arrested due to ${wordPairIterator.first.join(' ')}';
case 2:
return '$artist releases ${capitalizePair(wordPairIterator.first)}';
case 3:
return '$artist talks about his ${nouns[_random.nextInt(nouns.length)]}';
case 4:
return '$artist talks about her ${nouns[_random.nextInt(nouns.length)]}';
case 5:
return '$artist talks about their ${nouns[_random.nextInt(nouns.length)]}';
case 6:
return '$artist says their music is inspired by ${wordPairIterator.first.join(' ')}';
case 7:
return '$artist says the world needs more ${nouns[_random.nextInt(nouns.length)]}';
case 8:
return '$artist calls their band ${adjectives[_random.nextInt(adjectives.length)]}';
case 9:
return '$artist finally ready to talk about ${nouns[_random.nextInt(nouns.length)]}';
}
assert(false, 'Failed to generate news headline');
return null;
}
List<MaterialColor> getRandomColors(int amount) {
return List<MaterialColor>.generate(amount, (index) {
return _myListOfRandomColors[_random.nextInt(_myListOfRandomColors.length)];
});
}
List<String> getRandomNames(int amount) {
return wordPairIterator
.take(amount)
.map((pair) => capitalizePair(pair))
.toList();
}
String capitalize(String word) {
return '${word[0].toUpperCase()}${word.substring(1).toLowerCase()}';
}
String capitalizePair(WordPair pair) {
return '${capitalize(pair.first)} ${capitalize(pair.second)}';
}