Created
January 2, 2020 12:56
-
-
Save stefanasandei/f3bc866f3290d8afd3c07c2ca2cb27e8 to your computer and use it in GitHub Desktop.
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/foundation.dart' | |
show debugDefaultTargetPlatformOverride; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:english_words/english_words.dart'; | |
void main() { | |
// See https://github.com/flutter/flutter/wiki/Desktop-shells#target-platform-override | |
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; | |
runApp(new MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// #docregion build | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Startup Name Generator', | |
theme: ThemeData( | |
primaryColor: Colors.white, | |
), | |
home: RandomWords(), | |
); | |
} | |
// #enddocregion build | |
} | |
// #enddocregion MyApp | |
// #docregion RWS-var | |
class RandomWordsState extends State<RandomWords> { | |
final _suggestions = <WordPair>[]; | |
final Set<WordPair> _saved = Set<WordPair>(); | |
final _biggerFont = const TextStyle(fontSize: 18.0); | |
// #enddocregion RWS-var | |
// #docregion _buildSuggestions | |
Widget _buildSuggestions() { | |
return ListView.builder( | |
padding: const EdgeInsets.all(16.0), | |
itemBuilder: /*1*/ (context, i) { | |
if (i.isOdd) return Divider(); /*2*/ | |
final index = i ~/ 2; /*3*/ | |
if (index >= _suggestions.length) { | |
_suggestions.addAll(generateWordPairs().take(10)); /*4*/ | |
} | |
return _buildRow(_suggestions[index]); | |
}); | |
} | |
// #enddocregion _buildSuggestions | |
// #docregion _buildRow | |
Widget _buildRow(WordPair pair) { | |
final bool alreadySaved = _saved.contains(pair); | |
return ListTile( | |
title: Text( | |
pair.asPascalCase, | |
style: _biggerFont, | |
), | |
trailing: Icon( | |
alreadySaved ? Icons.favorite : Icons.favorite_border, | |
color: alreadySaved ? Colors.red : null, | |
), | |
onTap: () { | |
setState(() { | |
if (alreadySaved) { | |
_saved.remove(pair); | |
} else { | |
_saved.add(pair); | |
} | |
}); | |
}, | |
); | |
} | |
// #enddocregion _buildRow | |
// #docregion RWS-build | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Startup Name Generator'), | |
actions: <Widget>[ | |
IconButton(icon: Icon(Icons.list), onPressed: _pushSaved), | |
], | |
), | |
body: _buildSuggestions(), | |
); | |
} | |
// #enddocregion RWS-build | |
void _pushSaved() { | |
Navigator.of(context).push( | |
MaterialPageRoute<void>( | |
// Add 20 lines from here... | |
builder: (BuildContext context) { | |
final Iterable<ListTile> tiles = _saved.map( | |
(WordPair pair) { | |
return ListTile( | |
title: Text( | |
pair.asPascalCase, | |
style: _biggerFont, | |
), | |
); | |
}, | |
); | |
final List<Widget> divided = ListTile.divideTiles( | |
context: context, | |
tiles: tiles, | |
).toList(); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Saved Suggestions'), | |
), | |
body: ListView(children: divided), | |
); | |
}, | |
), | |
); | |
} | |
// #docregion RWS-var | |
} | |
// #enddocregion RWS-var | |
class RandomWords extends StatefulWidget { | |
@override | |
RandomWordsState createState() => RandomWordsState(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment