Skip to content

Instantly share code, notes, and snippets.

View Toglefritz's full-sized avatar

Scott Hatfield Toglefritz

View GitHub Profile
@Toglefritz
Toglefritz / flutter_mvc.dart
Last active July 4, 2023 20:38
Basic template for Flutter "WidgetView" MVC design pattern
/// The Model Widget defines external parameters.
class Page extends StatefulWidget {
const Page({Key? key}) : super(key: key);
@override
PageController createState() => PageController();
}
/// The Controller handles the state and business logic.
class PageController extends State<Page> {
@Toglefritz
Toglefritz / flutter_watch_and_phone_view_loading.dart
Created July 20, 2022 21:32
A Flutter Widget tree snippet that loads two different views depending upon the host device screen size.
// Return the view appropriate for the device screen size
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
debugPrint('Host device screen width: ${constraints.maxWidth}');
// Watch-sized device
if (constraints.maxWidth < 300) {
return DailyForecastViewWatch(this);
}
// Phone-sized device
@Toglefritz
Toglefritz / daily_forecast_main.dart
Created July 22, 2022 11:08
The build method for main.dart in the Partly Windy app.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Partly Windy',
theme: buildLightTheme(),
darkTheme: buildDarkTheme(),
themeMode: ThemeMode.system,
debugShowCheckedModeBanner: false,
home: const DailyForecastRoute(),
);
@Toglefritz
Toglefritz / daily_forecast_wear_os_mode_detection.dart
Created July 24, 2022 13:49
A Flutter widget that loads different views for Wear OS devices depending on whether they are in ambient or active mode.
AmbientMode(
builder: (BuildContext context, WearMode mode, child) {
return mode == WearMode.active
? DailyForecastViewWatchActive(state)
: DailyForecastViewWatchAmbient(state);
},
);
@Toglefritz
Toglefritz / daily_forecast_ambient_view.dart
Last active July 24, 2022 14:07
The view rendered by the Partly Windy app for Wear OS devices while they are in ambient mode.
/// View for [DailyForecastRoute] for watch-sized devices while the watch
/// is in an ambient mode.
class DailyForecastViewWatchAmbient extends StatelessWidget {
final DailyForecastController state;
const DailyForecastViewWatchAmbient(this.state, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
@Toglefritz
Toglefritz / daily_forecast_active_view.dart
Created July 24, 2022 14:15
The view rendered by the Partly Windy app for Wear OS devices in active mode.
/// View for [DailyForecastRoute] for watch-sized devices while the watch
/// is in an active mode.
class DailyForecastViewWatchActive extends StatelessWidget {
final DailyForecastController state;
const DailyForecastViewWatchActive(this.state, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
@Toglefritz
Toglefritz / openai_completions_request.dart
Created July 29, 2022 10:40
A class in the Partly Windy app that represents the parameters used in the body of a request to the OpenAI completions endpoint.
/// Represents the parameters used in the body of a request to the OpenAI completions endpoint.
class CompletionsRequest {
final String model;
final String prompt;
final int maxTokens;
final double? temperature;
final int? topP;
final int? n;
final bool? stream;
final int? longprobs;
@Toglefritz
Toglefritz / openai_completions_request_to_json.dart
Created July 29, 2022 10:46
A method to convert a CompletionsRequest object into a JSON-formatted String to be sent in a RESTful API request.
String toJson() {
Map<String, dynamic> jsonBody = {
'model': model,
'prompt': prompt,
'max_tokens': maxTokens,
};
if (temperature != null) {
jsonBody.addAll({'temperature': temperature});
}
@Toglefritz
Toglefritz / openai_completions_response.dart
Last active July 29, 2022 10:56
A class in the Partly Windy app that represents a response received from the OpenAI completions endpoint.
class CompletionsResponse {
final String? id;
final String object;
final int? created;
final String? model;
final List<dynamic>? choices; // This list contains the completions
final Map<String, dynamic>? usage;
final int? promptTokens;
final int? completionTokens;
final int? totalTokens;
@Toglefritz
Toglefritz / openai_completions_response_factory_constructor.dart
Created July 29, 2022 11:21
A factory constructor for the CompletionsResponse class in the Partly Windy app.
/// Returns a [CompletionResponse] from the JSON obtained from the
/// completions endpoint.
factory CompletionsResponse.fromResponse(Response response) {
// Get the response body in JSON format
Map<String, dynamic> responseBody = json.decode(response.body);
// Parse out information from the response
Map<String, dynamic> usage = responseBody['usage'];
// Parse out the choices