corextra 1.0.4
corextra: ^1.0.4 copied to clipboard
Handy Dart extensions and utility functions for easier coding.
example/corextra_example.dart
import 'package:corextra/corextra.dart';
import 'package:flutter/material.dart';
void main() {
// Override default breakpoints (optional)
ResponsiveBreakpoints.setCustomBreakpoints(xsMax: 500, mdMax: 1100);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'corextra Demo',
home: Scaffold(
appBar: AppBar(title: const Text('corextra Demo')),
body: const DemoScreen(),
),
);
}
}
class DemoScreen extends StatelessWidget {
const DemoScreen({super.key});
@override
Widget build(BuildContext context) {
// Examples of corextra utilities:
debugPrint('${'123'.toTryInt()}'); // 123
debugPrint('${'3.14'.toTryDouble()}'); // 3.14
debugPrint('${'true'.toTryBool()}'); // true
debugPrint('hello world'.capitalize()); // Hello World
List<int>? items;
debugPrint('${items.isNullOrEmpty}'); // true
debugPrint('${[1, 2, 3].isNotNullOrEmpty}'); // true
debugPrint('${(-5).toZeroIfNegative()}'); // 0
debugPrint('${(-3.5).toZeroIfNegative()}'); // 0.0
debugPrint('${isStringEmpty(null)}'); // true
debugPrint('${isListEmpty([1, 2])}'); // false
// DateTime extension examples:
String? dateStr = '10/08/2023';
DateTime? dt = dateStr.toTryDateTime(inputFormat: 'dd/MM/yyyy');
debugPrint('Parsed date: $dt'); // Parsed DateTime or null
String? isoDate = '2023-08-10T14:00:00Z';
DateTime? dt2 = isoDate.toTryDateTime();
debugPrint('Parsed ISO date: $dt2'); // Parsed DateTime or null
debugPrint(
'Formatted dt: ${dt.formatDate(outputFormat: 'yyyy-MM-dd')}',
); // e.g. "2023-08-10"
debugPrint(
'Formatted dt2: ${dt2.formatDate(outputFormat: 'dd MMM yyyy')}',
); // e.g. "10 Aug 2023"
return LayoutBuilder(
builder: (context, constraints) {
String screenSizeLabel;
if (ResponsiveBreakpoints.isXS(constraints)) {
screenSizeLabel = 'Extra Small Screen';
} else if (ResponsiveBreakpoints.isSMContext(context)) {
screenSizeLabel = 'Small Screen';
} else if (ResponsiveBreakpoints.isMD(constraints)) {
screenSizeLabel = 'Medium Screen';
} else if (ResponsiveBreakpoints.isLGContext(context)) {
screenSizeLabel = 'Large Screen';
} else if (ResponsiveBreakpoints.isXL(constraints)) {
screenSizeLabel = 'Extra Large Screen';
} else {
screenSizeLabel = 'XXL or larger Screen';
}
return Center(
child: Text(
'Screen size: $screenSizeLabel',
style: const TextStyle(fontSize: 24),
),
);
},
);
}
}