Advertisement
Guest User

Untitled

a guest
May 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:english_words/english_words.dart';
  3.  
  4. void main() => runApp(MyApp());
  5.  
  6. class MyApp extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return MaterialApp(
  10. title: 'Startup Name Generator',
  11. theme: new ThemeData(
  12. // Add the 3 lines from here...
  13. primaryColor: Colors.deepPurple,
  14. ), // ... to here.
  15. home: RandomWords(),
  16. );
  17. }
  18. }
  19.  
  20. class RandomWordsState extends State<RandomWords> {
  21. final _suggestions = <WordPair>[];
  22. final Set<WordPair> _saved = new Set<WordPair>();
  23. final _biggerFont = const TextStyle(fontSize: 18.0);
  24.  
  25. Widget _buildSuggestions() {
  26. return ListView.builder(
  27. padding: const EdgeInsets.all(16.0),
  28.  
  29. // and places each suggestion into a ListTile row.
  30. // For even rows, the function adds a ListTile row for the word pairing.
  31. // For odd rows, the function adds a Divider widget to visually
  32. // separate the entries. Note that the divider may be difficult
  33. // to see on smaller devices.
  34. itemBuilder: (context, i) {
  35. // Add a one-pixel-high divider widget before each row in theListView.
  36. if (i.isOdd) return Divider();
  37.  
  38. // The syntax "i ~/ 2" divides i by 2 and returns an integer result.
  39. // For example: 1, 2, 3, 4, 5 becomes 0, 1, 1, 2, 2.
  40. // This calculates the actual number of word pairings in the ListView,
  41. // minus the divider widgets.
  42. final index = i ~/ 2;
  43. // If you've reached the end of the available word pairings...
  44. if (index >= _suggestions.length) {
  45. // ...then generate 10 more and add them to the suggestions list.
  46. _suggestions.addAll(generateWordPairs().take(10));
  47. }
  48. return _buildRow(_suggestions[index]);
  49. });
  50. }
  51.  
  52. Widget _buildRow(WordPair pair) {
  53. final bool alreadySaved = _saved.contains(pair);
  54. return ListTile(
  55. title: Text(
  56. pair.asPascalCase,
  57. style: _biggerFont,
  58. ),
  59. trailing: new Icon(
  60. // Add the lines from here...
  61. alreadySaved ? Icons.favorite : Icons.favorite_border,
  62. color: alreadySaved ? Colors.red : null,
  63. ), // ... to here.
  64. onTap: () {
  65. // Add 9 lines from here...
  66. setState(() {
  67. if (alreadySaved) {
  68. _saved.remove(pair);
  69. } else {
  70. _saved.add(pair);
  71. }
  72. });
  73. }, // ... to here.
  74. );
  75. }
  76.  
  77. @override
  78. Widget build(BuildContext context) {
  79. return Scaffold(
  80. appBar: AppBar(
  81. title: Text('Startup Name Generator'),
  82. actions: <Widget>[
  83. // Add 3 lines from here...
  84. new IconButton(icon: const Icon(Icons.list), onPressed: _pushSaved),
  85. ],
  86. ),
  87. body: _buildSuggestions(),
  88. );
  89. }
  90.  
  91. void _pushSaved() {
  92. Navigator.of(context).push(
  93. new MaterialPageRoute<void>(
  94. builder: (BuildContext context) {
  95. final Iterable<ListTile> tiles = _saved.map(
  96. (WordPair pair) {
  97. return new ListTile(
  98. title: new Text(
  99. pair.asPascalCase,
  100. style: _biggerFont,
  101. ),
  102. );
  103. },
  104. );
  105. final List<Widget> divided = ListTile.divideTiles(
  106. context: context,
  107. tiles: tiles,
  108. ).toList();
  109.  
  110. return new Scaffold(
  111. // Add 6 lines from here...
  112. appBar: new AppBar(
  113. title: const Text('Saved Suggestions'),
  114. ),
  115. body: new ListView(children: divided),
  116. ); // ... to here.
  117. },
  118. ),
  119. );
  120. }
  121. }
  122.  
  123. class RandomWords extends StatefulWidget {
  124. @override
  125. RandomWordsState createState() => new RandomWordsState();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement