Skip to content

Instantly share code, notes, and snippets.

@Andrious
Created March 23, 2020 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrious/21fe8b4d786345ff4f74683dcfd7bdea to your computer and use it in GitHub Desktop.
Save Andrious/21fe8b4d786345ff4f74683dcfd7bdea to your computer and use it in GitHub Desktop.
The WordPair class in the 'Write Your First App' example.
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