helpersFilesContent top-level property
Implementation
Map<String, String> helpersFilesContent = {
'app_assets': '''
class AppAssets {
const AppAssets._();
}''',
//-------------------------------------------------------------------------------------------------------------------------
'constants': '''
class Constants {
const Constants._();
}
''',
//-------------------------------------------------------------------------------------------------------------------------
'extensions': '''
import 'package:flutter/material.dart';
extension Navigation on BuildContext {
Future<dynamic> pushNamed(String routeName, {Object? arguments}) {
return Navigator.of(this).pushNamed(routeName, arguments: arguments);
}
Future<dynamic> pushReplacementNamed(String routeName, {Object? arguments}) {
return Navigator.of(this)
.pushReplacementNamed(routeName, arguments: arguments);
}
Future<dynamic> pushNamedAndRemoveUntil(String routeName,
{Object? arguments, required RoutePredicate predicate}) {
return Navigator.of(this)
.pushNamedAndRemoveUntil(routeName, predicate, arguments: arguments);
}
void pop() => Navigator.of(this).pop();
}
extension StringExtension on String? {
bool isNullOrEmpty() => this == null || this == "";
}
extension ListExtension<T> on List<T>? {
bool isNullOrEmpty() => this == null || this!.isEmpty;
}
extension BuildContextExtension<T> on BuildContext {
double get height => MediaQuery.sizeOf(this).height;
double get width => MediaQuery.of(this).size.width;
EdgeInsets get viewPadding => MediaQuery.of(this).viewPadding;
double get bottomViewPadding => MediaQuery.of(this).viewPadding.bottom;
EdgeInsets get viewInsets => MediaQuery.of(this).viewInsets;
EdgeInsets get padding => MediaQuery.of(this).padding;
ThemeData get theme => Theme.of(this);
MediaQueryData get mediaQuery => MediaQuery.of(this);
ScaffoldMessengerState get scaffoldMassenger => ScaffoldMessenger.of(this);
Size get screenSize => MediaQuery.of(this).size;
T? get argument => ModalRoute.of(this)?.settings.arguments as T?;
ColorScheme get colorScheme => Theme.of(this).colorScheme;
}
''',
//-------------------------------------------------------------------------------------------------------------------------
'shared_perf_keys': '''
class SharedPrefKeys {
const SharedPrefKeys._();
static const String userToken = 'userToken';
}
''',
//-------------------------------------------------------------------------------------------------------------------------
'shared_pref_helper': '''
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SharedPrefHelper {
// private constructor as I don't want to allow creating an instance of this class itself.
SharedPrefHelper._();
/// Removes a value from SharedPreferences with given [key].
static removeData(String key) async {
debugPrint('SharedPrefHelper : data with key : \$key has been removed');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.remove(key);
}
/// Removes all keys and values in the SharedPreferences
static clearAllData() async {
debugPrint('SharedPrefHelper : all data has been cleared');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.clear();
}
/// Saves a [value] with a [key] in the SharedPreferences.
static setData(String key, value) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
debugPrint("SharedPrefHelper : setData with key : \$key and value : \$value");
switch (value.runtimeType) {
case String:
await sharedPreferences.setString(key, value);
break;
case int:
await sharedPreferences.setInt(key, value);
break;
case bool:
await sharedPreferences.setBool(key, value);
break;
case double:
await sharedPreferences.setDouble(key, value);
break;
default:
return null;
}
}
/// Gets a bool value from SharedPreferences with given [key].
static getBool(String key) async {
debugPrint('SharedPrefHelper : getBool with key : \$key');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getBool(key) ?? false;
}
/// Gets a double value from SharedPreferences with given [key].
static getDouble(String key) async {
debugPrint('SharedPrefHelper : getDouble with key : \$key');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getDouble(key) ?? 0.0;
}
/// Gets an int value from SharedPreferences with given [key].
static getInt(String key) async {
debugPrint('SharedPrefHelper : getInt with key : \$key');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getInt(key) ?? 0;
}
/// Gets an String value from SharedPreferences with given [key].
static getString(String key) async {
debugPrint('SharedPrefHelper : getString with key : \$key');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getString(key) ?? '';
}
/// Saves a [value] with a [key] in the FlutterSecureStorage.
// static setSecuredString(String key, String value) async {
// const flutterSecureStorage = FlutterSecureStorage();
// debugPrint(
// "FlutterSecureStorage : setSecuredString with key : \$key and value : \$value");
// await flutterSecureStorage.write(key: key, value: value);
// }
/// Gets an String value from FlutterSecureStorage with given [key].
// static getSecuredString(String key) async {
// const flutterSecureStorage = FlutterSecureStorage();
// debugPrint('FlutterSecureStorage : getSecuredString with key :');
// return await flutterSecureStorage.read(key: key) ?? '';
// }
/// Removes all keys and values in the FlutterSecureStorage
// static clearAllSecuredData() async {
// debugPrint('FlutterSecureStorage : all data has been cleared');
// const flutterSecureStorage = FlutterSecureStorage();
// await flutterSecureStorage.deleteAll();
// }
}
''',
//-------------------------------------------------------------------------------------------------------------------------
'spacing': '''
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
SizedBox vGap(double height) => SizedBox(
height: height.h,
);
SizedBox hGap(double width) => SizedBox(
width: width.w,
);
''',
//-------------------------------------------------------------------------------------------------------------------------
'validators_regex': '''
class ValidatorsRegex{
const ValidatorsRegex._();
static bool isEmailValid(String email) {
return RegExp(r'^.+@[a-zA-Z]+\.{1}[a-zA-Z]+(\.{0,1}[a-zA-Z]+)\$')
.hasMatch(email);
}
static bool isPasswordValid(String password) {
return RegExp(
r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@\$!%*?&])[A-Za-z\d@\$!%*?&]{8,}\$")
.hasMatch(password);
}
static bool isPhoneNumberValid(String phoneNumber) {
return RegExp(r'^(010|011|012|015)[0-9]{8}\$').hasMatch(phoneNumber);
}
static bool hasLowerCase(String password) {
return RegExp(r'^(?=.*[a-z])').hasMatch(password);
}
static bool hasUpperCase(String password) {
return RegExp(r'^(?=.*[A-Z])').hasMatch(password);
}
static bool hasNumber(String password) {
return RegExp(r'^(?=.*?[0-9])').hasMatch(password);
}
static bool hasSpecialCharacter(String password) {
return RegExp(r'^(?=.*?[#?!@\$%^&*-])').hasMatch(password);
}
static bool hasMinLength(String password) {
return RegExp(r'^(?=.{8,})').hasMatch(password);
}
}
''',
};