Skip to content

Instantly share code, notes, and snippets.

View timsneath's full-sized avatar

Tim Sneath timsneath

View GitHub Profile
@timsneath
timsneath / joke.cmd
Last active April 6, 2018 15:59
Using a Dart script from the command line
[C:\git\dart] dart joke.dart
Why did the burglar hang his mugshot on the wall? To prove that he was framed!
[C:\git\dart]
@timsneath
timsneath / jokestate.dart
Last active April 5, 2018 00:07
Handling asynchronous state with a FutureBuilder widget
class JokePageState extends State<JokePage> {
Future<String> response;
initState() {
super.initState();
response = http.read(dadJokeApi, headers: httpHeaders);
}
Widget build(BuildContext context) {
return new Scaffold(
@timsneath
timsneath / pubspec.yaml
Last active April 6, 2018 15:57
Example of adding a dependency on a Flutter package
dependencies:
share: "^0.3.1"
@timsneath
timsneath / shareFragment.dart
Last active April 6, 2018 15:57
Example of using the Flutter share package
import 'package:share/share.dart';
...
shareAction() {
if (displayedJoke != '') {
share(displayedJoke);
}
}
@timsneath
timsneath / pubspec.yaml
Created April 4, 2018 23:51
Fragment of Flutter manifest to declare support for a custom font family
flutter:
uses-material-design: true
fonts:
- family: Patrick Hand
fonts:
- asset: assets/fonts/PatrickHand-Regular.ttf
@timsneath
timsneath / customFont.dart
Created April 5, 2018 00:09
Adding a font to existing Flutter code
// declaring a text style based on custom font
const jokeTextStyle = const TextStyle(
fontFamily: 'Patrick Hand',
fontSize: 34.0,
color: Colors.black87,
letterSpacing: -0.5,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal);
// now all we have to do is add a style to the Text widget
@timsneath
timsneath / refreshButton.dart
Created April 5, 2018 00:33
Simple refresh button in Flutter
_refreshAction() {
setState(() {
_response = http.read(dadJokeApi, headers: httpHeaders);
});
}
floatingActionButton: new FloatingActionButton(
onPressed: _refreshAction,
tooltip: 'New joke',
child: new Icon(Icons.refresh),
@timsneath
timsneath / dismissible.dart
Created April 5, 2018 00:55
Adding support for dismissible jokes
_refreshAction() {
setState(() {
_response = http.read(dadJokeApi, headers: httpHeaders);
});
}
...
if (decoded['status'] == 200) {
_displayedJoke = decoded['joke'];
@timsneath
timsneath / buildWidget.dart
Created May 4, 2018 20:28
Simple example of a Widget build method without new keyword in Dart 2.
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.info),
onPressed: _aboutAction,
),
IconButton(
icon: Icon(Icons.share),
@timsneath
timsneath / scopedConst.dart
Created May 4, 2018 21:07
Example of scoped constant.
const comments = <Comment>[
Comment(
"Creating apps is just faster and more fun with Flutter.",
"Posse Inc."),
Comment(
"Yesterday I was trying #Flutter for the first time, today I published an application.",
"@CristianDudca"),
Comment(
"This weekend: Met and fell in love with Flutter.",
"@FIREYOSE"),