Created
March 23, 2020 16:46
-
-
Save Andrious/21fe8b4d786345ff4f74683dcfd7bdea to your computer and use it in GitHub Desktop.
The WordPair class in the 'Write Your First App' example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart' show Icon, State; | |
import 'package:flutter/material.dart' show Colors, Icons; | |
import 'package:english_words/english_words.dart' as w; | |
class WordPair { | |
WordPair(this.state); | |
final State state; | |
final suggestions = <w.WordPair>[]; | |
final Set<w.WordPair> saved = <w.WordPair>{}; | |
int index; | |
void build(int i) { | |
index = i ~/ 2; | |
if (index >= suggestions.length) { | |
suggestions.addAll(w.generateWordPairs().take(10)); | |
} | |
} | |
String get current => suggestions[index].asPascalCase; | |
Icon get icon { | |
bool alreadySaved = saved.contains(suggestions[index]); | |
return Icon( | |
alreadySaved ? Icons.favorite : Icons.favorite_border, | |
color: alreadySaved ? Colors.red : null, | |
); | |
} | |
void onTap(int i) => state.setState(() { | |
int index = i ~/ 2; | |
w.WordPair pair = suggestions[index]; | |
if (pair == null) return; | |
if (saved.contains(suggestions[index])) { | |
saved.remove(pair); | |
} else { | |
saved.add(pair); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment