Skip to content

Instantly share code, notes, and snippets.

@febritecno
Last active November 14, 2020 03:36
Show Gist options
  • Save febritecno/b130e896154168ef3d19e1914a7559b3 to your computer and use it in GitHub Desktop.
Save febritecno/b130e896154168ef3d19e1914a7559b3 to your computer and use it in GitHub Desktop.
flutter responsif helper
import 'package:flutter/material.dart';
import 'package:one_context/one_context.dart';
////* Device size by media query
//double deviceWidth(BuildContext context) => MediaQuery.of(context).size.width;
//double deviceHeight(BuildContext context) => MediaQuery.of(context).size.height;
double deviceWidth = OneContext().mediaQuery.size.width;
double deviceHeight = OneContext().mediaQuery.size.height;
double wp(double percentage) {
return (deviceWidth * percentage) / 100;
}
double hp(double percentage) {
return (deviceHeight * percentage) / 100;
}
double wp2(double percentage, double min,BuildContext context) {
double _ret = wp(percentage);
if (_ret < min){
return min;
}
return _ret;
}
double hp2(double percentage, double min) {
double _ret = hp(percentage);
if (_ret < min){
return min;
}
return _ret;
}
double wp3(double percentage, double min, double max) {
double _ret = wp(percentage);
if (_ret < min){
return min;
}
if (_ret > max){
return max;
}
return _ret;
}
double hp3(double percentage, double min, double max) {
double _ret = hp(percentage);
if (_ret < min){
return min;
}
if (_ret > max){
return max;
}
return _ret;
}
import 'package:flutter/material.dart';
class ResponsiveContainer extends StatelessWidget {
/// The [child] contained by the container.
///
/// If null, and if the [constraints] are unbounded or also null, the
/// container will expand to fill all available space in its parent, unless
/// the parent provides unbounded constraints, in which case the container
/// will attempt to be as small as possible.
///
/// {@macro flutter.widgets.child}
final Widget child;
/// Align the [child] within the container.
///
/// If non-null, the container will expand to fill its parent and position its
/// child within itself according to the given value. If the incoming
/// constraints are unbounded, then the child will be shrink-wrapped instead.
///
/// Ignored if [child] is null.
///
/// See also:
///
/// * [Alignment], a class with convenient constants typically used to
/// specify an [AlignmentGeometry].
/// * [AlignmentDirectional], like [Alignment] for specifying alignments
/// relative to text direction.
final AlignmentGeometry alignment;
/// Empty space to inscribe inside the [decoration]. The [child], if any, is
/// placed inside this padding.
///
/// This padding is in addition to any padding inherent in the [decoration];
/// see [Decoration.padding].
final EdgeInsetsGeometry padding;
/// Empty space to surround the [decoration] and [child].
final EdgeInsetsGeometry margin;
/// [widthPercent] value percent of screen total width.
final double widthPercent;
/// [heightPercent] value percent of screen total height.
final double heightPercent;
const ResponsiveContainer({Key key, this.alignment, this.padding, this.margin, this.child, @required this.widthPercent, @required this.heightPercent}) : super(key: key);
double percent(double value, double porcentagem) {
return (value * porcentagem) / 100;
}
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
var width = size.width;
var height = size.height;
return Container(
padding: padding,
margin: margin,
alignment: alignment,
child: child,
width: percent(width, widthPercent),
height: percent(height, heightPercent),
);
}
}
class Screen {
static double get _ppi => (Platform.isAndroid || Platform.isIOS)? 150 : 96;
static bool isLandscape(BuildContext c) => MediaQuery.of(c).orientation == Orientation.landscape;
//PIXELS
static Size size(BuildContext c) => MediaQuery.of(c).size;
static double width(BuildContext c) => size(c).width;
static double height(BuildContext c) => size(c).height;
static double diagonal(BuildContext c) {
Size s = size(c);
return sqrt((s.width * s.width) + (s.height * s.height));
}
//INCHES
static Size inches(BuildContext c) {
Size pxSize = size(c);
return Size(pxSize.width / _ppi, pxSize.height/ _ppi);
}
static double widthInches(BuildContext c) => inches(c).width;
static double heightInches(BuildContext c) => inches(c).height;
static double diagonalInches(BuildContext c) => diagonal(c) / _ppi;
}
class Device {
static bool get isDesktop => !isWeb ?? (isWindows || isLinux || isMacOS);
static bool get isMobile => isAndroid || isIOS;
static bool get isWeb => kIsWeb;
static bool get isWindows => Platform.isWindows;
static bool get isLinux => Platform.isLinux;
static bool get isMacOS => Platform.isMacOS;
static bool get isAndroid => Platform.isAndroid;
static bool get isFuchsia => Platform.isFuchsia;
static bool get isIOS => Platform.isIOS;
}
// bool isDesktop = Device.isDesktop;
// bool isWeb = Device.isWeb
// bool isMobile = Device.isMobile;
// bool isLandscape = Screen.isLandscape(context)
// bool isLargePhone = Screen.diagonal(context) > 720;
// bool isTablet = Screen.diagonalInches(context) >= 7;
// bool isNarrow = Screen.widthInches(context) < 3.5;
// bool isDesktop = Platform.isWindows || Platform.isLinux || Platform.isMacOS;
// bool isMobile = Platform.isAndroid || Platform.isIOS;
// bool isWeb = kIsWeb;
// https://blog.gskinner.com/archives/2020/03/flutter-simplify-platform-detection-responsive-sizing.html
import 'package:flutter/material.dart';
class Screen{
Size screenSize;
Screen(this.screenSize);
double wp(percentage) {
return percentage / 100 * screenSize.width;
}
double hp(percentage) {
return percentage / 100 * screenSize.height;
}
double wp2(double percentage, double min) {
double _ret = percentage / 100 * screenSize.width;
if (_ret < min){
return min;
}
return _ret;
}
double hp2(double percentage, double min) {
double _ret = percentage / 100 * screenSize.height;
if (_ret < min){
return min;
}
return _ret;
}
double wp3(double percentage, double min, double max) {
double _ret = percentage / 100 * screenSize.width;
if (_ret < min){
return min;
}
if (_ret > max){
return max;
}
return _ret;
}
double hp3(double percentage, double min, double max) {
double _ret = percentage / 100 * screenSize.height;
if (_ret < min){
return min;
}
if (_ret > max){
return max;
}
return _ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment