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,79 +9,105 @@ 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(), class _RandomWordsState extends State<RandomWords> {
), final _suggestions = <WordPair>[];
final _saved = <WordPair>{};
final _biggerFont = const TextStyle(fontSize: 18);
@override
Widget build(BuildContext context) {
return Scaffold(
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),
itemBuilder: (context, i) {
if (i.isOdd) return const Divider();
final index = i ~/ 2;
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10));
}
final alreadySaved = _saved.contains(_suggestions[index]);
return ListTile(
title: Text(
_suggestions[index].asPascalCase,
style: _biggerFont,
),
trailing: Icon(
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
semanticLabel: alreadySaved ? 'Remove from saved' : 'Save',
),
onTap: () {
setState(() {
if (alreadySaved) {
_saved.remove(_suggestions[index]);
} else {
_saved.add(_suggestions[index]);
}
});
},
);
},
), ),
); );
} }
// #enddocregion build
}
// #enddocregion MyApp
// #docregion RWS-var void _pushSaved() {
class _RandomWordsState extends State<RandomWords> { Navigator.of(context).push(
final _suggestions = <WordPair>[]; MaterialPageRoute<void>(
final _saved = <WordPair>{}; // NEW builder: (context) {
final _biggerFont = const TextStyle(fontSize: 36); final tiles = _saved.map(
// #enddocregion RWS-var (pair) {
return ListTile(
title: Text(
pair.asPascalCase,
style: _biggerFont,
),
);
},
);
final divided = tiles.isNotEmpty
? ListTile.divideTiles(
context: context,
tiles: tiles,
).toList()
: <Widget>[];
// #docregion RWS-build return Scaffold(
@override appBar: AppBar(
Widget build(BuildContext context) { title: const Text('Saved Suggestions'),
// #docregion itemBuilder ),
return ListView.builder( body: ListView(children: divided),
padding: const EdgeInsets.all(16.0), );
itemBuilder: /*1*/ (context, i) { },
if (i.isOdd) return const Divider(); /*2*/ ),
final index = i ~/ 2; /*3*/
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10)); /*4*/
}
final alreadySaved = _saved.contains(_suggestions[index]); // NEW
// #docregion listTile
return ListTile(
title: Text(
_suggestions[index].asPascalCase,
style: _biggerFont,
),
trailing: Icon( // NEW from here ...
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
semanticLabel: alreadySaved ? 'Remove from saved' : 'Save',
),
onTap: () { // NEW from here ...
setState(() {
if (alreadySaved) {
_saved.remove(_suggestions[index]);
} else {
_saved.add(_suggestions[index]);
}
});
});
// #enddocregion listTile
},
); );
// #enddocregion itemBuilder
} }
// #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});