Created
August 8, 2019 22:17
-
-
Save eileooo/c697ec7b4390a9ff1171e2bf87453aa3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/material.dart'; | |
| import 'package:english_words/english_words.dart'; | |
| import 'dart:math'; | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Welcome to Flutter', | |
| home: Scaffold( | |
| appBar: AppBar( | |
| title: Text('Welcome to Flutter'), | |
| backgroundColor: Color.fromRGBO(0, 100000, 0, 100), | |
| ), | |
| body: Center( | |
| child: RandomWords(), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class RandomWords extends StatefulWidget { | |
| @override | |
| RandomWordsState createState() => RandomWordsState(); | |
| } | |
| class RandomWordsState extends State<RandomWords> { | |
| final _suggestions = <WordPair>[]; | |
| final _biggerFont = const TextStyle(fontSize: 18.0); | |
| final _saved = Set<WordPair>(); | |
| 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]); | |
| }); | |
| } | |
| Widget _buildRow(WordPair pair) { | |
| final bool saved = _saved.contains(pair); | |
| return ListTile( | |
| trailing: IconButton( | |
| icon: Icon(saved ? Icons.favorite : Icons.favorite_border), | |
| color: saved | |
| ? Colors.red | |
| : null, | |
| onPressed: () { | |
| setState(() { | |
| if (_saved.contains(pair)) { | |
| _saved.remove(pair); | |
| } else | |
| _saved.add(pair); | |
| }); | |
| }), | |
| title: Text( | |
| pair.asPascalCase, | |
| style: _biggerFont, | |
| ), | |
| ); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: _buildSuggestions(), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment