Flutter Helper Utils

Make Flutter development easier with Flutter Helper Utils! This toolkit is packed with state management, helper methods and extensions that boost your productivity and help you write less code and build apps faster.

Flutter Helper Utils Logo

Table of Contents

Watcher (State Management):

Watcher is a simple state management that utilizes Flutter's native ValueListenableBuilder and ValueNotifier, providing a lightweight, efficient, and straightforward way to manage state changes. Detailed Documentation here.

here's a quick example to showcase its simplicity:

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

main() => runApp(MyCounter());

class MyCounter extends StatelessWidget {

  final IntWatcher counter = 0.watch;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Watch Counter',
      home: Scaffold(
        appBar: AppBar(title: const Text('Watch Counter')),
        body: Center(
          child: ValueWatcher(
            watcher: counter,
            builder: (context, value) {
              return Text('Counter: $value');
            },
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => counter.increment(1),
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}

Helper Methods

Converting Objects:

Convert objects to various types, such as int, double, bool, String, List, Set and Map, They are handy when working with data from APIs, where you often encounter dynamic types. They offer simple and flexible methods to perform accurate type conversions. Here is a sample:

Before:

Map = {score: "12.4"};


int score = double.parse(map['key']).toInt();

Notice that we need to parse to double first then to int.

With Package:


int score = toInt(map['key']);

More Details here

Extensions

Our package provides a variety of extensions on Dart and Flutter's built-in types. Below are the categories of available extensions, sorted by their functionalities:

Core Data Types

String Extensions

Number Extensions

Boolean Extensions

  • isTrue - Checks if the Boolean value is true, considering null safety.
  • isFalse - Checks if the Boolean value is false or null.
  • val - Returns the Boolean value or false if it is null.
  • binary - Returns 1 if the Boolean is true and zero if it is false or null.
  • binaryText - Returns '1' if the Boolean is true and '0' if it is false or null.

DateTime Extensions

  • format & tryFormat - format a DateTime object as a string using a specified format (e.g. "yyyy-MM-dd"), while tryFormat attempts to do the same but returns null if unsuccessful.
  • tryToDateTime - Safely attempts to convert a string to a DateTime object. Returns null if the conversion fails.
  • toDateTime - Converts a string to a DateTime object. Throw an exception if the conversion fails.
  • dateFormat - Returns a DateFormat object based on the string format provided.
  • toDateWithFormat - Converts a string to a DateTime object using a specific format.
  • tryToDateWithFormat - Safely attempts to convert a string to a DateTime object using a specific format. Returns null if the conversion fails.
  • timestampToDate - Converts a timestamp string to a DateTime object.
  • toSmallMonthName & toFullMonthName - Convert an integer representing a month to its abbreviated name.
  • toSmallDayName & toFullDayName - Convert an integer representing a day of the week to its abbreviated name.
  • View All DateTime Extensions

Duration Extensions

  • delayed - Utility to delay some code execution.
  • fromNow - Adds the Duration to the current DateTime and gives a future time.
  • ago - Subtracts the Duration from the current DateTime and gives a pastime.

UI & Design

Color Extensions

  • toHex - Converts a Color object to its hex string representation.
  • toColor - Converts a hex color string to a Color object.
  • isHexColor - Checks if the given string is a valid hexadecimal color string.

Flutter Extensions

ThemeExtensions

  • themeData - Quickly get the current ThemeData from the nearest Theme widget ancestor.
  • txtTheme - Effortlessly obtain the TextTheme for your context.
  • brightness - Determine the brightness setting of the app theme.
  • sysBrightness - Determine the brightness setting of the system.
  • isDark & isLight - Convenient boolean to quickly check if the current theme is light or dark.

MediaQueryExtensions

FocusScopeExtensions

Exceptions

The ConvertObject class throws a ParsingException if there is an error while converting an object. This exception provides information about the type of the object and the method used for conversion.

Contributions

Contributions to this package are welcome. If you have any suggestions, issues, or feature requests, please create a pull request on the repository.

License

flutter_helper_utils is available under the BSD 3-Clause License.

Buy Me A Coffee