Skip to content

Instantly share code, notes, and snippets.

View timsneath's full-sized avatar

Tim Sneath timsneath

View GitHub Profile
@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 / 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 / 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 / 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 / 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
Last active April 6, 2018 15:57
Example of adding a dependency on a Flutter package
dependencies:
share: "^0.3.1"
@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 / 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 / joke.dart
Last active April 6, 2018 16:00
Basic example of an async HTTP GET call to a JSON web service
import 'package:http/http.dart' as http;
import 'dart:convert';
const dadJokeApi = "https://icanhazdadjoke.com/";
const httpHeaders = const {
'User-Agent': 'DadJokes (https://github.com/timsneath/dadjokes)',
'Accept': 'application/json',
};
main() async {
@timsneath
timsneath / # README
Last active December 13, 2020 22:45
Settings files
My dotfiles