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']);
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
isNumeric
- Checks if the string is numeric.isAlphabet
- Checks if the string consists only of alphabets.hasCapitalLetter
- Checks if the string contains at least one capital letter.isBool
- Checks if the string is a boolean value.removeWhiteSpaces
- Removes all space from the string.textSize
- Returns the size of the text using TextPainter.wrapString
- Adds a new line to the string after a specified number of words.equalsIgnoreCase
- Compares two strings for equality, ignoring case.removeSurrounding
- Removes a specified delimiter from both ends of the string, if present.replaceAfter
- Replaces part of the string after the first occurrence of a given delimiter.- View All String Extensions
Number Extensions
isSuccessHttpResCode
- Checks if the numeric HTTP status code represents a successful response.toHttpResStatus
- Converts the numeric HTTP status code to its corresponding HttpResStatus enum.tryToInt
- Safely converts a nullable number to an integer.tryToDouble
- Safely converts a nullable number to a double.percentage
- Calculates the percentage this number represents out of a total.isNegative & isPositive
- Checks if the number is negative or positive.isZeroOrNull
- Checks if the number is zero or null.asBool
- Evaluates the truthiness of a nullable number based on whether it's greater than 0.isZero
- Checks if the number is zero.numberOfDigits
- Returns the number of digits in the number.- View All 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 aColor
object to its hex string representation.toColor
- Converts a hex color string to aColor
object.isHexColor
- Checks if the given string is a valid hexadecimal color string.
Flutter Extensions
ThemeExtensions
themeData
- Quickly get the currentThemeData
from the nearestTheme
widget ancestor.txtTheme
- Effortlessly obtain theTextTheme
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
mq
- Quick Access to MediaQueryDatanullableMQ
- Nullable MediaQueryData AccessdeviceOrientation
- Device OrientationnavigationMode
- Navigation Modepadding and viewInsets
- Screen Padding and InsetsscreenDimensions
- Screen DimensionsadditionalFeatures
- Additional Features- View All Media Query Extensions
FocusScopeExtensions
focusScope
- Getting the Nearest FocusScopeNode.unFocus
- Removing Focus.requestFocus
- Requesting Focus.requestFocusCall
- Requesting Focus (Using GestureTapCallback).hasFocus
- Checking if a Node Has Focus.hasPrimaryFocus
- Checking if a Node Has Primary Focus.
Navigator Extensions
popPage
- Pop PagepopRoot
- Pop Rootnavigator
- Navigator StatecanPop
- Can PoppPage
- Push PagepReplacement
- Push ReplacementpAndRemoveUntil
- Push And Remove UntilpNamedAndRemoveUntil
- Push Named And Remove UntilpNamed
- Push NamedpReplacementNamed
- Push Replacement NamedpopUntil
- Pop UntildismissActivePopup
- Dismiss Active Popup- View All Navigator Extensions
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.