A lightweight library offering extensions for Flutter widgets and BuildContext
to simplify tasks like alignment, padding, navigation, and more. These extensions reduce boilerplate, enhance readability, and streamline your widget code.
To use these extensions in your Flutter project:
- Clone the Repository:
git clone https://github.com/MAHMOUDELSAYED7/Flutter-extensions.git
- Navigate to the Directory:
cd Flutter-extensions
- Install Dependencies:
flutter pub get
Explore the extensions in the lib/extensions/
folder and integrate them into your project.
These examples showcase how the extensions simplify Flutter code. Each includes a Before (traditional) and After (extension-based) version with a brief explanation.
Widget build(BuildContext context) {
return Scaffold(
body: Align(
alignment: Alignment.bottomRight,
child: Text('Bottom Right'),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
body: Text('Bottom Right').alignBottomRight(),
);
}
Explanation: alignBottomRight()
removes the need for an Align
widget, reducing nesting.
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Text('Symmetric Padding'),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
body: Text('Symmetric Padding').withSymmetricPadding(vertical: 10.0, horizontal: 20.0),
);
}
Explanation: withSymmetricPadding()
replaces Padding
, improving readability with named parameters.
Widget build(BuildContext context) {
return Scaffold(
body: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => NextPage()),
);
},
child: Text('Next Page'),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
body: ElevatedButton(
onPressed: () => context.push(NextPage()),
child: Text('Next Page'),
),
);
}
Explanation: push()
simplifies navigation by abstracting Navigator
boilerplate.
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
print('Tapped the icon');
},
child: Icon(Icons.star),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
body: Icon(Icons.star).onTap(() => print('Tapped the icon')),
);
}
Explanation: onTap()
replaces GestureDetector
, making tap handling more concise.
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox(
width: 80.0,
height: 80.0,
child: Container(color: Colors.blue),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
body: Container(color: Colors.blue).withSquareSize(80.0),
);
}
Explanation: withSquareSize()
eliminates SizedBox
, setting a square size directly.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}
// Usage
BlocProvider.of<CounterCubit>(context).increment();
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
void increment() => emit(state + 1);
}
// Simplified usage
context.cubit<CounterCubit>().increment();
Explanation: cubit<T>()
simplifies Cubit access, replacing verbose BlocProvider.of<T>
.
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
return Scaffold(
body: Container(
width: screenWidth * 0.5,
height: 100,
color: Colors.green,
child: Text('Half Width Container'),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: context.width * 0.5,
height: 100,
color: Colors.green,
child: Text('Half Width Container'),
),
);
}
Explanation: context.width
provides direct screen width access, skipping MediaQuery
.
Here’s a concise overview of the extensions provided:
- Alignment:
center()
,alignBottomRight()
, etc., for easy widget alignment. - Padding:
withPadding()
,withSymmetricPadding()
, etc., for flexible padding. - Navigation:
push()
,back()
, etc., for simplified routing. - Gesture:
onTap()
,onLongPress()
, etc., for gesture handling. - SizedBox:
withWidth()
,withSquareSize()
, etc., for sizing widgets. - Cubit/Bloc:
cubit<T>()
,bloc<T>()
for state management access. - Media Query:
width
,height
,isLandscape
, etc., for screen data. - ClipRRect:
circular()
,topRounded()
, etc., for border radius. - Positioned:
positionedTopLeft()
, etc., forStack
positioning. - Transform:
rotate()
,scale()
, etc., for widget transformations. - Visibility:
visible()
,invisible()
, etc., for show/hide control. - Theme:
theme
,textTheme
, etc., for theme access. - Post Frame Callback:
addPostFrameCallback()
, etc., for post-build actions.
For detailed usage and additional methods, refer to the source files in lib/extensions/
.
For any questions or feedback, please reach out via email: [email protected]