Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.76 KB | None | 0 0
  1.  
  2. class RandomWordsState extends State<RandomWords> {
  3.   @override
  4.   Widget build(BuildContext context) {
  5.     return Scaffold (
  6.       appBar: AppBar(
  7.         title: Text('Startup Name Generator'),
  8.       ),
  9.       body: _buildSuggestions(),
  10.     );
  11.   }
  12.  
  13.   final _suggestions = <WordPair>[];
  14.  
  15.   final _biggerFont = const TextStyle(fontSize: 18.0);
  16.  
  17.   Widget _buildSuggestions() {
  18.     return ListView.builder(
  19.       padding: const EdgeInsets.all(16.0),
  20.       // The itemBuilder callback is called once per suggested word pairing,
  21.       // and places each suggestion into a ListTile row.
  22.       // For even rows, the function adds a ListTile row for the word pairing.
  23.       // For odd rows, the function adds a Divider widget to visually
  24.       // separate the entries. Note that the divider may be difficult
  25.       // to see on smaller devices.
  26.       itemBuilder: (context, i) {
  27.         // Add a one-pixel-high divider widget before each row in theListView.
  28.         if (i.isOdd) return Divider();
  29.  
  30.         // The syntax "i ~/ 2" divides i by 2 and returns an integer result.
  31.         // For example: 1, 2, 3, 4, 5 becomes 0, 1, 1, 2, 2.
  32.         // This calculates the actual number of word pairings in the ListView,
  33.         // minus the divider widgets.
  34.         final index = i ~/ 2;
  35.         // If you've reached the end of the available word pairings...
  36.         if (index >= _suggestions.length) {
  37.           // ...then generate 10 more and add them to the suggestions list.
  38.           _suggestions.addAll(generateWordPairs().take(10));
  39.         }
  40.         return _buildRow(_suggestions[index]);
  41.       }
  42.     );
  43.   }
  44.  
  45.   Widget _buildRow(WordPair pair) {
  46.     return ListTile(
  47.       title: Text(
  48.         pair.asPascalCase,
  49.         style: _biggerFont,
  50.       ),
  51.     );
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement