by: Eliezer Antonio
Most of us Flutter developers add fixed sizes to our components, whether it's text, images, or other widgets, and sometimes because of this, on different screen sizes, we end up with the famous overflow error. With this package, each component adjusts to every different device size. Although there are other approaches to solve this, an automatic adaptation that provides the right size for each device can be better in terms of UX and has other advantages.
- Width in percent
- Height in percent
- Diagonal in percent
- Detect tablet size
- Flutter Android
- Flutter iOS
- Flutter web
- Flutter desktop
Add flutter_responsivity_widget: ^0.0.1 to your pubspec.yaml dependency file. And import:
import 'package:flutter_responsivity_widget/flutter_responsivity_widget.dart';class ExamplePage extends StatelessWidget {
const ExamplePage({super.key});
@override
Widget build(BuildContext context) {
final responsive = Responsive(context);
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Image.asset(
'assets/logo_jp.jpg',
width: responsive.wp(100), // wp- width in %
height: responsive.hp(100), // hp- height in %
),
),
);
}
}
class ExamplePage extends StatelessWidget {
const ExamplePage({super.key});
@override
Widget build(BuildContext context) {
final responsive = Responsive(context);
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Text(
'Example',
style: TextStyle(
fontSize: responsive.dp(8), // dp- diagonal in %
),
),
),
);
}
}
class ExamplePage extends StatelessWidget {
const ExamplePage({super.key});
@override
Widget build(BuildContext context) {
final responsive = Responsive(context);
final isTablet = responsive.isTablet;
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: isTablet
? Text(
'iPad',
style: TextStyle(
fontSize: responsive.dp(10),
),
)
: Text( 'iPhone',
style: TextStyle(
fontSize: responsive.dp(10),
)
),
),
);
}
}
Contributions are welcome! If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request