Skip to content

Instantly share code, notes, and snippets.

@xantiagoma
Last active November 10, 2018 21:44
Show Gist options
  • Save xantiagoma/5e4760fcd24fba98fef618e06a6dbf19 to your computer and use it in GitHub Desktop.
Save xantiagoma/5e4760fcd24fba98fef618e06a6dbf19 to your computer and use it in GitHub Desktop.
Hello World Flutter / Dart
import 'package:flutter/cupertino.dart'; // importa todas las funciones sin prefix
// import 'package:flutter/cupertino.dart' as cupertino; // go-style use cupertino.runApp(...
void main(){
runApp(
Center(
child: Text(
'Hello World',
), // Text
)
);
}
import 'package:flutter/material.dart'; // importa todas las funciones sin prefix
// import 'package:flutter/cupertino.dart' as cupertino; go-style
import 'package:english_words/english_words.dart' as words;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Trakting',
// Scaffold es un widge tocn barra de titulo y otras cosas
home: RandomWords(),
theme: ThemeData(
primaryColor: Colors.green
),
);
}
}
class RandomWords extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return RandomWordsState();
}
}
class RandomWordsState extends State<RandomWords> {
final _suggestions = <words.WordPair>[];
final _saved = Set<words.WordPair>();
final _textStyle = TextStyle(fontSize: 18.0);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Welcome to flutter'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.list),
onPressed: _pushSaved,
)
],
),
body: _buildSuggestions(),
);
}
_pushSaved() {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
final tiles = _saved.map((pair) {
return ListTile(
title: Text(
pair.asPascalCase,
style: _textStyle,
),
);
});
final divaded =
ListTile.divideTiles(tiles: tiles, context: context).toList();
return Scaffold(
appBar: AppBar(
title: Text('Saved'),
),
body: ListView(children: divaded,)
);
}));
}
Widget _buildSuggestions() {
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: (context, index) {
if (index.isOdd) {
return Divider();
}
if (index >= _suggestions.length) {
// llego al final
_suggestions.addAll(words.generateWordPairs().take(10));
}
final indexWithDivider = index ~/ 2;
return _buildRow(_suggestions[index]);
});
}
Widget _buildRow(words.WordPair pair) {
final isSaved = _saved.contains(pair);
return ListTile(
title: Text(
pair.asPascalCase,
style: _textStyle,
),
trailing: Icon(
isSaved ? Icons.favorite : Icons.favorite_border,
color: isSaved ? Colors.red : Colors.blueGrey.shade100,
),
onTap: () {
setState(() {
if (isSaved) {
_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