add a page of saved selections

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2022-12-16 09:32:32 -06:00
parent 4a842d1698
commit c5a3c68691
1 changed files with 85 additions and 59 deletions

View File

@ -9,62 +9,59 @@ void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
// #docregion MyApp
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});
// #docregion build
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return const MaterialApp(
title: 'Startup Name Generator', title: 'Startup Name Generator',
home: Scaffold( home: RandomWords(),
appBar: AppBar(
title: const Text('Startup Name Generator'),
),
body: const Center(
child: RandomWords(),
),
),
); );
} }
// #enddocregion build
} }
// #enddocregion MyApp
// #docregion RWS-var
class _RandomWordsState extends State<RandomWords> { class _RandomWordsState extends State<RandomWords> {
final _suggestions = <WordPair>[]; final _suggestions = <WordPair>[];
final _saved = <WordPair>{}; // NEW final _saved = <WordPair>{};
final _biggerFont = const TextStyle(fontSize: 36); final _biggerFont = const TextStyle(fontSize: 18);
// #enddocregion RWS-var
// #docregion RWS-build
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// #docregion itemBuilder return Scaffold(
return ListView.builder( appBar: AppBar(
title: const Text('Startup Name Generator'),
actions: [
IconButton(
icon: const Icon(Icons.list),
onPressed: _pushSaved,
tooltip: 'Saved Suggestions',
),
],
),
body: ListView.builder(
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
itemBuilder: /*1*/ (context, i) { itemBuilder: (context, i) {
if (i.isOdd) return const Divider(); /*2*/ if (i.isOdd) return const Divider();
final index = i ~/ 2; /*3*/ final index = i ~/ 2;
if (index >= _suggestions.length) { if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10)); /*4*/ _suggestions.addAll(generateWordPairs().take(10));
} }
final alreadySaved = _saved.contains(_suggestions[index]); // NEW
// #docregion listTile final alreadySaved = _saved.contains(_suggestions[index]);
return ListTile( return ListTile(
title: Text( title: Text(
_suggestions[index].asPascalCase, _suggestions[index].asPascalCase,
style: _biggerFont, style: _biggerFont,
), ),
trailing: Icon( // NEW from here ... trailing: Icon(
alreadySaved ? Icons.favorite : Icons.favorite_border, alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null, color: alreadySaved ? Colors.red : null,
semanticLabel: alreadySaved ? 'Remove from saved' : 'Save', semanticLabel: alreadySaved ? 'Remove from saved' : 'Save',
), ),
onTap: () { // NEW from here ... onTap: () {
setState(() { setState(() {
if (alreadySaved) { if (alreadySaved) {
_saved.remove(_suggestions[index]); _saved.remove(_suggestions[index]);
@ -72,16 +69,45 @@ class _RandomWordsState extends State<RandomWords> {
_saved.add(_suggestions[index]); _saved.add(_suggestions[index]);
} }
}); });
});
// #enddocregion listTile
}, },
); );
// #enddocregion itemBuilder },
),
);
}
void _pushSaved() {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (context) {
final tiles = _saved.map(
(pair) {
return ListTile(
title: Text(
pair.asPascalCase,
style: _biggerFont,
),
);
},
);
final divided = tiles.isNotEmpty
? ListTile.divideTiles(
context: context,
tiles: tiles,
).toList()
: <Widget>[];
return Scaffold(
appBar: AppBar(
title: const Text('Saved Suggestions'),
),
body: ListView(children: divided),
);
},
),
);
} }
// #enddocregion RWS-build
// #docregion RWS-var
} }
// #enddocregion RWS-var
class RandomWords extends StatefulWidget { class RandomWords extends StatefulWidget {
const RandomWords({super.key}); const RandomWords({super.key});