Skip to content

Instantly share code, notes, and snippets.

@ruan65
Last active December 13, 2019 08:41
Show Gist options
  • Save ruan65/f69b04ca1088de37e73337d67f497da0 to your computer and use it in GitHub Desktop.
Save ruan65/f69b04ca1088de37e73337d67f497da0 to your computer and use it in GitHub Desktop.
common use snippets in dart
// To put bottom sheet into Stack (close on back press)
WidgetsBinding.instance.addPostFrameCallback((_) {
_scaffoldKey.currentState.showBottomSheet<void>(_bottomSheetBuilder);
});
// screen sizes
const i5s = Size(320.0, 568.0);
const i8 = Size(375.0, 667.0)
// rounded button
Widget roundedButton(
String title,
VoidCallback onPressed, {
double h = 50,
double w = 200,
double r = 5,
double fSize = 20,
Color color = Colors.blue,
Color textColor = Colors.white,
}) =>
ButtonTheme(
height: h,
minWidth: w,
child: RaisedButton(
color: fabColor,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(r)),
onPressed: onPressed,
child: Text(
title,
style: TextStyle(color: textColor, fontSize: fSize),
)));
// text
String capitalize(String s) => (s != null && s.length > 1)
? s[0].toUpperCase() + s.substring(1)
: s != null ? s.toUpperCase() : '';
// random color
import 'dart:math' as math;
final rnd = math.Random();
Color getRandomColor(math.Random rnd) => Color(rnd.nextInt(0xffffffff));
Color getRandomColor(math.Random rnd) => Color.fromARGB(
255, rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255));
// navigation
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailedScreen(color)),
);
Navigator.of(context).pop();
SchedulerBinding.instance.addPostFrameCallback((_) {
navigateToAuthSuccessOrErrorScreen(context,
AuthSuccessErrorSettings(false, widget.authType));
});
class CustomAnimatedRouter extends PageRouteBuilder {
final Widget widget;
CustomAnimatedRouter(this.widget)
: super(
transitionDuration: Duration(milliseconds: 500),
pageBuilder: (BuildContext context, Animation<double> anim,
Animation<double> secondaryAnim) {
return widget;
},
transitionsBuilder: (BuildContext context, Animation<double> anim,
Animation<double> secondaryAnim, Widget child) {
return FadeTransition(
opacity: Tween(begin: 0.0, end: 1.0).animate(
CurvedAnimation(parent: anim, curve: Curves.fastOutSlowIn)),
child: child,
);
});
}
// Internec connectivity
connectivity: ^0.4.4
Future<bool> isConnectedToInternet() async {
var connectivityResult = await (Connectivity().checkConnectivity());
return connectivityResult == ConnectivityResult.mobile ||
connectivityResult == ConnectivityResult.wifi;
}
// Post build delay
SchedulerBinding.instance.addPostFrameCallback(
(_) => model.handleItemCreated(index));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment