Skip to content

Instantly share code, notes, and snippets.

@DevKhalyd
Last active July 19, 2022 16:24
Show Gist options
  • Save DevKhalyd/2e155a5c1e487fcca1193ec4e3b95efe to your computer and use it in GitHub Desktop.
Save DevKhalyd/2e155a5c1e487fcca1193ec4e3b95efe to your computer and use it in GitHub Desktop.
Functions Utils in Dart
import 'package:flutter/material.dart';
//Example: https://stackoverflow.com/questions/26748605/how-to-store-function-as-class-member-variable?rq=
//In dart the functions can be Objects
///
typedef Widget BuilderState(
BuildContext context, void Function(void Function()) setState);
double getScreenWidth(BuildContext context) =>
MediaQuery.of(context).size.width;
double getScreenHeight(BuildContext context) =>
MediaQuery.of(context).size.height;
//App colors
//Color primaryColor = ;
//Color acentColor = ;
//Color primaryColorDark = ;
///Remove the stack
nextRemoveWhole(BuildContext context, String route) =>
Navigator.of(context).pushNamedAndRemoveUntil(route, (route) => false);
nextPage(BuildContext context, Widget widget) => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => widget,
));
///This could return anything
///
///If stateful is set to [true] this have to use the builder Function
///
///Also the data to update should be outside from the state method
Future<dynamic> showDialogCustom(BuildContext context,
{Widget dialog,
BuilderState state,
bool barrierDismissible = true,
bool isStateful = false}) async {
assert(dialog != null || state != null,
'Remove dialog because isStateful is set to true');
if (!isStateful) {
return await showDialog(
barrierDismissible: barrierDismissible,
context: context,
builder: (context) => dialog,
);
}
return await showDialog(
barrierDismissible: barrierDismissible,
context: context,
builder: (context) => StatefulBuilder(
builder: state,
),
);
}
String getDate() => DateTime.now().millisecondsSinceEpoch.toString();
back(BuildContext context) => Navigator.pop(context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment