Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created June 22, 2024 15:58
Show Gist options
  • Save fredgrott/da3e25dfd61a7122e55b445202a43049 to your computer and use it in GitHub Desktop.
Save fredgrott/da3e25dfd61a7122e55b445202a43049 to your computer and use it in GitHub Desktop.
app snippet
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:sdk_cs_googlefonts/src/sample_feature/sample_item_details_view.dart';
import 'package:sdk_cs_googlefonts/src/sample_feature/sample_item_list_view.dart';
import 'package:sdk_cs_googlefonts/src/settings/settings_controller.dart';
import 'package:sdk_cs_googlefonts/src/settings/settings_view.dart';
import 'package:sdk_cs_googlefonts/src/themes/color_scheme_dark.dart';
import 'package:sdk_cs_googlefonts/src/themes/color_scheme_light.dart';
import 'package:sdk_cs_googlefonts/src/themes/color_seeds.dart';
import 'package:sdk_cs_googlefonts/src/themes/dark_theme.dart';
import 'package:sdk_cs_googlefonts/src/themes/harmonize.dart';
import 'package:sdk_cs_googlefonts/src/themes/light_theme.dart';
// trick from dynamic color package example as
// cs generation does not shift hue color of semantic
// colors towards primary properly
ColorScheme csl = colorSchemeLight(primarySeedColor).harmonizedSemantic();
ColorScheme csd = colorSchemeDark(primarySeedColor).harmonizedSemantic();
/// The Widget that configures your application.
class MyApp extends StatelessWidget {
const MyApp({
super.key,
required this.settingsController,
});
final SettingsController settingsController;
@override
Widget build(BuildContext context) {
// Glue the SettingsController to the MaterialApp.
//
// The ListenableBuilder Widget listens to the SettingsController for changes.
// Whenever the user updates their settings, the MaterialApp is rebuilt.
return ListenableBuilder(
listenable: settingsController,
builder: (BuildContext context, Widget? child) {
return MaterialApp(
// Providing a restorationScopeId allows the Navigator built by the
// MaterialApp to restore the navigation stack when a user leaves and
// returns to the app after it has been killed while running in the
// background.
restorationScopeId: 'app',
// Provide the generated AppLocalizations to the MaterialApp. This
// allows descendant Widgets to display the correct translations
// depending on the user's locale.
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', ''), // English, no country code
],
// Use AppLocalizations to configure the correct application title
// depending on the user's locale.
//
// The appTitle is defined in .arb files found in the localization
// directory.
onGenerateTitle: (BuildContext context) => AppLocalizations.of(context)!.appTitle,
// Define a light and dark color theme. Then, read the user's
// preferred ThemeMode (light, dark, or system default) from the
// SettingsController to display the correct theme.
theme: lightTheme(context, csl),
darkTheme: darkTheme(context, csd),
themeMode: settingsController.themeMode,
// Define a function to handle named routes in order to support
// Flutter web url navigation and deep linking.
onGenerateRoute: (RouteSettings routeSettings) {
return MaterialPageRoute<void>(
settings: routeSettings,
builder: (BuildContext context) {
switch (routeSettings.name) {
case SettingsView.routeName:
return SettingsView(controller: settingsController);
case SampleItemDetailsView.routeName:
return const SampleItemDetailsView();
case SampleItemListView.routeName:
default:
return const SampleItemListView();
}
},
);
},
);
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment