admin管理员组文章数量:1123429
so I'm working with this Flutter project and I want to set a dynamic dimensions that will work on different sizes of devices. But I'm having some trouble since its not really applying the changes. Maybe my formula is wrong? I am following a tutorial on YouTube.
This is the main device I am basing off. Pixel 4 API 31 Android 12.0 ("S").
And this is the other device. Pixel 2 API 31 Android 12.0 ("S")
As you can see, container is not centered.
Here is the dimensions file. The Pixel 4 is 781 in height.
import 'package:get/get.dart';
class Dimensions {
static double screenHeight = Get.context!.height;
static double screenWidth = Get.context!.width;
static double pageView = screenHeight/2.44;
static double pageViewContainer = screenHeight/3.55;
static double pageViewTextContainer = screenHeight/6.50;
//dynamic height padding and margin
static double height10 = screenHeight/78.1;
static double height15 = screenHeight/52.06;
static double height20 = screenHeight/39.05;
static double height30 = screenHeight/26.03;
static double height45 = screenHeight/17.35;
//dynamic width padding and margin
static double width10 = screenHeight/78.1;
static double width15 = screenHeight/52.06;
static double width20 = screenHeight/39.05;
static double width30 = screenHeight/26.03;
//dynamic fonts
static double font20 = screenHeight/39.05;
//dynamic radius
static double raidus15 = screenHeight/52.06;
static double radius20 = screenHeight/39.05;
static double radius30 = screenHeight/26.03;
}
And this is the Food Page.
import 'package:dots_indicator/dots_indicator.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:food_delivery/utils/colors.dart';
import 'package:food_delivery/widgets/big_text.dart';
import 'package:food_delivery/widgets/icon_and_text.dart';
import 'package:food_delivery/widgets/small_text.dart';
import '../utils/dimensions.dart';
return Transform(
transform: matrix,
child: Stack(
children: [
Container (
height: Dimensions.pageViewContainer,
margin: EdgeInsets.only(left: Dimensions.width10, right: Dimensions.width10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(Dimensions.radius30),
color: index.isEven?Color(0xFF69c5df):Color(0xFF9294dcc),
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage("assets/image/food0.png"))
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: Dimensions.pageViewTextContainer,
margin: EdgeInsets.only(left: Dimensions.width30, right: Dimensions.width30, bottom: Dimensions.width30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(Dimensions.radius20),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Color(0xFFe8e8e8),
blurRadius: 5.0,
offset: Offset(0, 5)
),
BoxShadow(
color: Colors.white,
offset: Offset(-5, 0)
),
BoxShadow(
color: Colors.white,
offset: Offset(5, 0)
)
]
),
child: Container(
padding: EdgeInsets.only(top: Dimensions.height15, left: Dimensions.width15, right: Dimensions.width15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BigText(text: "Chinese Side"),
SizedBox(height: Dimensions.height10,),
Row(
children: [
Wrap(
children: List.generate(5, (index) => Icon(Icons.star, color: AppColors.mainColor, size: 15,)),
),
SizedBox(width: Dimensions.height10),
SmallText(text: "4.5"),
SizedBox(width: Dimensions.height10),
SmallText(text: "1287"),
SizedBox(width: Dimensions.height10),
SmallText(text: "comments")
],
),
SizedBox(height: Dimensions.height20,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconAndTextWidget(
icon: Icons.circle_sharp,
text: "Normal",
iconColor: AppColors.iconColor1),
IconAndTextWidget(
icon: Icons.location_on,
text: "1.7 km",
iconColor: AppColors.mainColor),
IconAndTextWidget(
icon: Icons.access_time_rounded,
text: "32 mins",
iconColor: AppColors.iconColor2),
],
)
],
),
),
),
)
],
),
);
}
}
Can someone please help me? I'm just starting to learn Flutter and Dart and I don't know whats wrong and I'm just following the tutorial step-by-step.
TYIA!!!
so I'm working with this Flutter project and I want to set a dynamic dimensions that will work on different sizes of devices. But I'm having some trouble since its not really applying the changes. Maybe my formula is wrong? I am following a tutorial on YouTube.
This is the main device I am basing off. Pixel 4 API 31 Android 12.0 ("S").
And this is the other device. Pixel 2 API 31 Android 12.0 ("S")
As you can see, container is not centered.
Here is the dimensions file. The Pixel 4 is 781 in height.
import 'package:get/get.dart';
class Dimensions {
static double screenHeight = Get.context!.height;
static double screenWidth = Get.context!.width;
static double pageView = screenHeight/2.44;
static double pageViewContainer = screenHeight/3.55;
static double pageViewTextContainer = screenHeight/6.50;
//dynamic height padding and margin
static double height10 = screenHeight/78.1;
static double height15 = screenHeight/52.06;
static double height20 = screenHeight/39.05;
static double height30 = screenHeight/26.03;
static double height45 = screenHeight/17.35;
//dynamic width padding and margin
static double width10 = screenHeight/78.1;
static double width15 = screenHeight/52.06;
static double width20 = screenHeight/39.05;
static double width30 = screenHeight/26.03;
//dynamic fonts
static double font20 = screenHeight/39.05;
//dynamic radius
static double raidus15 = screenHeight/52.06;
static double radius20 = screenHeight/39.05;
static double radius30 = screenHeight/26.03;
}
And this is the Food Page.
import 'package:dots_indicator/dots_indicator.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:food_delivery/utils/colors.dart';
import 'package:food_delivery/widgets/big_text.dart';
import 'package:food_delivery/widgets/icon_and_text.dart';
import 'package:food_delivery/widgets/small_text.dart';
import '../utils/dimensions.dart';
return Transform(
transform: matrix,
child: Stack(
children: [
Container (
height: Dimensions.pageViewContainer,
margin: EdgeInsets.only(left: Dimensions.width10, right: Dimensions.width10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(Dimensions.radius30),
color: index.isEven?Color(0xFF69c5df):Color(0xFF9294dcc),
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage("assets/image/food0.png"))
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: Dimensions.pageViewTextContainer,
margin: EdgeInsets.only(left: Dimensions.width30, right: Dimensions.width30, bottom: Dimensions.width30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(Dimensions.radius20),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Color(0xFFe8e8e8),
blurRadius: 5.0,
offset: Offset(0, 5)
),
BoxShadow(
color: Colors.white,
offset: Offset(-5, 0)
),
BoxShadow(
color: Colors.white,
offset: Offset(5, 0)
)
]
),
child: Container(
padding: EdgeInsets.only(top: Dimensions.height15, left: Dimensions.width15, right: Dimensions.width15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BigText(text: "Chinese Side"),
SizedBox(height: Dimensions.height10,),
Row(
children: [
Wrap(
children: List.generate(5, (index) => Icon(Icons.star, color: AppColors.mainColor, size: 15,)),
),
SizedBox(width: Dimensions.height10),
SmallText(text: "4.5"),
SizedBox(width: Dimensions.height10),
SmallText(text: "1287"),
SizedBox(width: Dimensions.height10),
SmallText(text: "comments")
],
),
SizedBox(height: Dimensions.height20,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconAndTextWidget(
icon: Icons.circle_sharp,
text: "Normal",
iconColor: AppColors.iconColor1),
IconAndTextWidget(
icon: Icons.location_on,
text: "1.7 km",
iconColor: AppColors.mainColor),
IconAndTextWidget(
icon: Icons.access_time_rounded,
text: "32 mins",
iconColor: AppColors.iconColor2),
],
)
],
),
),
),
)
],
),
);
}
}
Can someone please help me? I'm just starting to learn Flutter and Dart and I don't know whats wrong and I'm just following the tutorial step-by-step.
TYIA!!!
Share Improve this question asked 16 hours ago marcelinemarceline 3431 gold badge4 silver badges12 bronze badges1 Answer
Reset to default 0First off, I recommend using MediaQuery in order to check and re-arrange ur components according to whatever the sizes might be
final width = MediaQuery.of(context).size.width
final height = MediaQuery.of(context).size.height
Example on how to use those variables
return Scaffold(
backgroundColor: width < 600 ? Colors.Red[300] : Colors.Green[300]
)
We can also use any kind of LayoutBuilders as they can call for constraints, which gives the same values as MediaQuery
@Override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints){
if (constraints.maxWidth < 600 {
return mobileComponents;
else {
return webComponents;
}
}
}
)}
About the values from MediaQuery or using a LayoutBuilder constraints, you are free to choose when to re-arrange your components based on the width or height, however you can do a quick search to see the common values used for mobile or web or whatever your developments targets are.
Let me know if this helped you.
本文标签: androidDynamic dimensions using Dart is not applying changes in smaller pixelsStack Overflow
版权声明:本文标题:android - Dynamic dimensions using Dart is not applying changes in smaller pixels - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736570335a1944762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论