utiler 1.4.4 copy "utiler: ^1.4.4" to clipboard
utiler: ^1.4.4 copied to clipboard

Essential utility functions and helpers for Dart/Flutter projects. Simplify everyday coding with tools for common logic patterns. Developed by Mehrab Ghassab

example/main.dart

import 'package:flutter/material.dart';
import 'package:utiler/utiler.dart';

void main() {
  runApp(
    UtilerScope(
      /// ------------------------------------------------------------
      /// App lifecycle listener (optional)
      /// ------------------------------------------------------------
      lifecycleListener: (state) {
        debugPrint('App lifecycle changed: $state');
      },

      /// ------------------------------------------------------------
      /// Logging configuration
      /// ------------------------------------------------------------
      enabledLog: true,
      exportLog: false,
      showLogWidget: true,
      showPerformanceMonitor: true,

      /// ------------------------------------------------------------
      /// Typed theme & locale definitions
      /// (Used with context.appTheme / context.appLocale)
      /// ------------------------------------------------------------
      themes: [
        DummyTheme(id: 'light', color: const Color(0xFF1565C0)),
        DummyTheme(id: 'dark', color: const Color(0xFF263238)),
      ],
      locales: [
        DummyLocale(id: 'en', title: 'English'),
        DummyLocale(id: 'fa', title: 'فارسی'),
      ],

      /// ------------------------------------------------------------
      /// JSON-based theme configuration
      /// (Alternative to typed themes)
      ///
      /// NOTE:
      /// Do NOT mix typed + JSON for same type in real usage.
      /// ------------------------------------------------------------
      jsonThemes: {
        'light': {
          'home': {'background': const Color(0xFF1565C0)},
          'profile': {'background': const Color(0xFF1565C0)},
        },
        'dark': {
          'home': {'background': const Color(0xFF263238)},
          'profile': {'background': const Color(0xFF263238)},
        },
      },

      /// Loads every `.json` file under the directory.
      jsonThemesAddress: 'assets/theme',

      /// ------------------------------------------------------------
      /// JSON-based localization
      /// ------------------------------------------------------------
      jsonLocales: {
        'en': {
          'home': {'appbar': 'Home Screen'},
          'profile': {'appbar': 'Profile Screen'},
        },
        'fa': {
          'home': {'appbar': 'صفحه اصلی'},
          'profile': {'appbar': 'صفحه پروفایل'},
        },
      },

      /// Loads every `.json` file under the directory.
      jsonLocalesAddress: 'assets/locale',

      /// ------------------------------------------------------------
      /// Transition animations for theme & locale switching
      /// ------------------------------------------------------------
      themeAnimation: ValuesAnimationType.fade,
      localeAnimation: ValuesAnimationType.blurReveal,
      themeAnimationDuration: const Duration(milliseconds: 400),
      localeAnimationDuration: const Duration(milliseconds: 400),

      /// Root app widget
      child: const _MyApp(),
    ),
  );
}

class _MyApp extends StatelessWidget {
  const _MyApp();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'UtilerScope demo',
      debugShowCheckedModeBanner: false,
      home: const _HomePage(),
    );
  }
}

class _HomePage extends StatefulWidget {
  const _HomePage();

  @override
  State<_HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<_HomePage> {
  @override
  Widget build(BuildContext context) {
    /// ------------------------------------------------------------
    /// Typed access (recommended when using DummyTheme/DummyLocale)
    /// ------------------------------------------------------------
    final theme = context.appTheme as DummyTheme;
    final locale = context.appLocale as DummyLocale;

    /// ------------------------------------------------------------
    /// JSON-based access (optional mode)
    /// ------------------------------------------------------------
    final jsonTheme = context.appJsonTheme;
    final jsonLocale = context.appJsonLocale;

    return Scaffold(
      drawerScrimColor:
          (jsonTheme?['home'] as Map?)?['background'] ?? theme.color,

      backgroundColor: 'home.background'.cr,

      appBar: AppBar(
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(locale.title),
            Text((jsonLocale?['home'] as Map?)?['appbar'] ?? ''),
            Text('home.appbar'.tr),
          ],
        ),
      ),

      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Wrap(
                spacing: 12,
                runSpacing: 8,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      Utiler.changeAppTheme('light');
                      context.changeAppTheme('light');
                    },
                    child: const Text('Theme: light'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      Utiler.changeAppTheme('dark');
                      context.changeAppTheme('dark');
                    },
                    child: const Text('Theme: dark'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      Utiler.changeAppLocale('en');
                      context.changeAppLocale('en');
                    },
                    child: const Text('Locale: en'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      Utiler.changeAppLocale('fa');
                      context.changeAppLocale('fa');
                    },
                    child: const Text('Locale: fa'),
                  ),
                ],
              ),

              const SizedBox(height: 20),
              DropdownButtonFormField<ValuesAnimationType?>(
                key: ValueKey(Utiler.themeAnimationType),
                initialValue: Utiler.themeAnimationType,
                decoration: const InputDecoration(
                  labelText: 'Default theme animation',
                  border: OutlineInputBorder(),
                ),
                items: [
                  const DropdownMenuItem(
                    value: null,
                    child: Text('instant (none)'),
                  ),
                  ...ValuesAnimationType.values.map(
                    (type) =>
                        DropdownMenuItem(value: type, child: Text(type.name)),
                  ),
                ],
                onChanged: (value) async {
                  await Utiler.changeThemeAnimation(value);
                  setState(() {});
                },
              ),
              const SizedBox(height: 12),
              DropdownButtonFormField<ValuesAnimationType?>(
                key: ValueKey(Utiler.localeAnimationType),
                initialValue: Utiler.localeAnimationType,
                decoration: const InputDecoration(
                  labelText: 'Default locale animation',
                  border: OutlineInputBorder(),
                ),
                items: [
                  const DropdownMenuItem(
                    value: null,
                    child: Text('instant (none)'),
                  ),
                  ...ValuesAnimationType.values.map(
                    (type) =>
                        DropdownMenuItem(value: type, child: Text(type.name)),
                  ),
                ],
                onChanged: (value) async {
                  await Utiler.changeLocaleAnimation(value);
                  setState(() {});
                },
              ),
              const SizedBox(height: 20),
              const Text(
                'This demo shows theme & locale switching using Utiler.',
              ),
            ],
          ),
        ),
      ),
    );
  }
}

/// ------------------------------------------------------------
/// Dummy implementations for demonstration purposes
/// ------------------------------------------------------------

class DummyTheme extends ThemeValues {
  @override
  String id;

  final Color color;

  DummyTheme({required this.id, required this.color});
}

class DummyLocale extends LocaleValues {
  @override
  String id;

  final String title;

  DummyLocale({required this.id, required this.title});
}
3
likes
160
points
27
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Essential utility functions and helpers for Dart/Flutter projects. Simplify everyday coding with tools for common logic patterns. Developed by Mehrab Ghassab

Repository (GitHub)
View/report issues

Topics

#flutter #utilities #localization #theming #api

License

MIT (license)

Dependencies

connectivity_plus, connectivity_plus_platform_interface, flutter, flutter_secure_storage, hive_ce, http, path, web

More

Packages that depend on utiler