Skip to content

Instantly share code, notes, and snippets.

@benznest benznest/main.dart
Created Sep 20, 2018

Embed
What would you like to do?
My flutter learning
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: new HomePage());
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(appBar: new AppBar(
title: new Text('Page 2'),
actions: <Widget>[ // Add 3 lines from here...
new IconButton(icon: const Icon(Icons.apps),
onPressed: () => navigateSecondPage(context)),
],
),
body: Center(
child: new RandomWords(),
),
);
}
void navigateSecondPage(BuildContext context) {
Navigator.of(context).pushReplacement(
new MaterialPageRoute<void>(
builder: (BuildContext context){
return new Scaffold( // Add 6 lines from here...
appBar: new AppBar(
title: const Text('Second Page'),
),
body: new Center(child: new Text("Hello Second Page")),);
},
),
);
}
}
class RandomWords extends StatefulWidget {
@override
RandomWordsState createState() => new RandomWordsState();
}
class RandomWordsState extends State<RandomWords> {
final _suggestions = <WordPair>[];
final Set<WordPair> _saved = new Set<WordPair>(); // Add this line.
final _biggerFont = const TextStyle(fontSize: 32.0);
@override
Widget build(BuildContext context) {
return _buildListView();
}
Widget _buildListView() {
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: (context, i) {
if (i.isOdd) return Divider();
final index = i ~/ 2;
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10));
}
return _buildRow(_suggestions[index]);
}
);
}
Widget _buildRow(WordPair pair) {
final bool alreadySaved = _saved.contains(pair); // Add this line.
return ListTile(
title: Text(
pair.asPascalCase,
style: _biggerFont,
),
trailing: new Icon( // Add the lines from here...
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
), onTap: () {
// Add 9 lines from here...
setState(() {
if (alreadySaved) {
_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
You can’t perform that action at this time.