Flutter UI breaks when "smallest width" is changed in developer options - flutter

I am new into flutter programming. I have been working on a certain UI as shown below.
If I attempt to change the "smallest width" option in the developer options and set to some other value than the default, the whole UI breaks as shown.
I must add that the middle area which got completely broken was a stack widget.
#override
Widget build(BuildContext context) {
return Consumer<SinglePlayerGameState>(
builder: (context, gameState, child) => Expanded(
flex: 48,
child: Container(
color: gameState.getGameColor(),
child: Center(
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
child: Transform.rotate(
angle: 45,
child: UNOcard(UNOcardData(CardTypes.BACK)),
),
top: -30,
left: 90,
),
Positioned(
child: Transform.rotate(
angle: 65,
child: UNOcard(UNOcardData(CardTypes.BACK)),
),
top: 20,
left: 50,
),
Positioned(
child: Transform.rotate(
angle: 180,
child: UNOcard(UNOcardData(CardTypes.BACK)),
),
top: 100,
left: 65,
),
Positioned(
child: Transform.rotate(
angle: 45,
child: UNOcard(UNOcardData(CardTypes.BACK)),
),
top: 10,
right: 10,
),
Positioned(
child: Transform.rotate(
angle: 65,
child: UNOcard(UNOcardData(CardTypes.BACK)),
),
top: 120,
right: 50,
),
Positioned(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.black),
child: Text(
"${gameState.playingCards.length}",
style: TextStyle(
fontSize: 20,
fontFamily: 'Frijole',
color: Colors.white),
),
padding: EdgeInsets.all(7),
),
top: 10,
left: 10,
),
Positioned(
child: SizedBox(
child: Center(
child: FlatButton(
onPressed: () {
gameState.nextTurn();
},
child: Icon(
FontAwesomeIcons.undoAlt,
size: 25,
),
color: Colors.white,
)),
height: 35,
width: 60,
),
bottom: 10,
left: 10,
),
Positioned(
child: DragTarget<UNOcardData>(
onWillAccept: (UNOcardData data) {
var res = isValidMove(
data, gameState.onGoingCards.last, gameState);
if(res){
print(res);
return true;
}else{
vibrate();
return false;
}
},
onAccept: (UNOcardData data) {
print(data);
performAction(data, gameState);
},
builder: (context, candidateData, rejectedData) =>
Transform.rotate(
angle: 0,
child: gameState.onGoingCards.last,
),
),
top: 60,
right: 100,
),
],
),
),
),
),
);
}
}
Like we use units like "vmin , vmax , vh " etc in html/css for responsive texts and sizes, what are the alternatives in flutter ? How do we responsive apps in flutter exactly ? I am not talking about scaling to other screen sizes, but to slightly different resolutions and logical pixel densities.
Thanks for your time.

That's because you are using margins from the edges with
Positioned(
child: Transform.rotate(
angle: 180,
child: UNOcard(UNOcardData(CardTypes.BACK)),
),
top: 100,
left: 65,
),
This top means that it's going to position itself 100 units from the top. What you actually want is some percentage offset from the center. You can do it like this:
Positioned.fill(
child: Align(
// (0, 0) is the center, (1, 1) is right bottom, (-1, -1) is left top and so on
alignment: Alignment(0.25, 0.25),
child: YOUR_WIDGET
)
);
For more responsive UI you can get screen_sizes with MediaQuery.of(context) or use a package like flutter_screenutil to scale your UI according to a sample, like for example 1080x1920 screen.

Related

Animation in Flutter: How to resize a Stack?

What's the right way to animate a change in size of a Stack widget?
The first idea was to wrap the Stack with a SizeTransition:
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizeTransition(
sizeFactor: _height, // Just a Tween<double>(begin: 200, end: 400)
axis: Axis.vertical,
axisAlignment: -1,
child: Stack(
children: [
Positioned(
bottom: 10,
left: 10,
child: Container(width: 50, height: 50, color: Colors.blue),
),
Positioned(
top: 10,
right: 10,
child: Container(width: 50, height: 50, color: Colors.green),
),
],
),
),
],
);
}
But running the snippet causes the following error:
'package:flutter/src/rendering/stack.dart': Failed assertion: line 588 pos 12: 'size.isFinite': is not true.
So, apparently, that's not the right way to do it. Questions now is: How do you animate resizing of a Stack?
Wrapping the Stack with a SizedBox would fix the error message, but then, how do you get the size from SizedTransition to set the proper size of a SizedBox?
You can use AnimatedContainer as parent of Stack widget by providing height or width one you like to change over time.
AnimatedContainer(
color: Colors.purple,
duration: const Duration(milliseconds: 200),
height: height,
child: Stack(
children: [
Positioned(
bottom: 10,
left: 10,
child: Container(width: 50, height: 50, color: Colors.blue),
),
Positioned(
top: 10,
right: 10,
child: Container(width: 50, height: 50, color: Colors.green),
),
],
),
),

Flutter Container on top of rows

I made a bunch of rows. Now I want a Container on top of these rows. How can I do this. I tried it with the stack widget ,but it just stacked everything on top of each other and I had no control over the positioning.
How can I draw a Container on top of a bunch of rows.
Thank you for your help.
You can check the below code, though your asking is little bit confusing. You need to use Positioned and set some value at top and left.
Scaffold(
body: Stack(
children: [
Positioned(
top:0 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
),
),
Positioned(
top:100 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: Row(),
),
),
Positioned(
top:200 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: Row(),
),
),
Positioned(
top:300 ,
left: 0,
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: Row(),
),
),
],
),
)

Flutter - How to make image responsive with all screen sizes

Hi I am making a book app in flutter which will have material buttons on top of the image per page. but I notice when I go from iPhone X to iPhone XR or any others, the buttons are all over the place and I see the picture isn't aligned correctly either or same. I need only the landscape mode picture to fill the whole page and be totally responsive on all screens including iPad....
how can I go about doing this..?
I tried media query but when I do screenSizes.width* any number what's happening is the buttons are not displaying and ... the image is scrolling infinitely and all screen sizes are still showing different images.
I will share my code below for one button and the image
#override
Widget build(BuildContext context) {
var screenSizes = MediaQuery.of(context).size;
return Scaffold(
appBar: new AppBar(title: new Text("1 Alif-laam-meem آلم, Pg2")),
body: new SingleChildScrollView(
child: Container(
constraints: BoxConstraints.expand(
// BoxConstraints
// BoxConstraints.expand(
// height: 1000,
// .expand(
width: screenSizes.width * 10,
height: screenSizes.height * 10,
),
// children: [
child: Stack(children: <Widget>[
SafeArea(
top: true,
bottom: true,
right: true,
left: true,
child: new Container(
// padding: EdgeInsets.zero,
child: new Image.asset(
"test/assets/quranpg0.png",
// fit: BoxFit.fitWidth,
fit: BoxFit.cover,
// color: Colors.purpleAccent,
// colorBlendMode:ui.BlendMode.colorDodge,
))),
Positioned(
// right: 600,
bottom: screenSizes.width * 0.985,
left: screenSizes.height * 0.87,
// bottom: 754.0,
// left: 360,
// left: width*0.5,
// top: 900,
// top: 100,
child: Opacity(
opacity: 0.95,
child: _visible
? MaterialButton(
// minWidth:screenSize.width*0.1,
// 0.1 = 10% of the screen , 0.5 = 50% of the screen
// ios sizes usually are 620...
// height: screenSize.height*0.1,
onPressed: () => changeTextArabic1(),
onLongPress: () => changeTextEnglish1(),
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(80.0),
// child: Text(labels[i]),
),
child: Ink(
child: Text(surah0),
// disabledTextColor: Colors.transparent,
// color: Colors.cyan[300],
// // color: Colors.purple[300],
// highlightColor: Colors.blue,
// // shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(60.0)
// // ),
// textColor: Colors.white,
// color: Colors.cyan[400],
padding: EdgeInsets.only(
top: screenSizes.height * 0.05,
left: screenSizes.width * 0.02,
right: screenSizes.width * 0.02,
bottom: screenSizes.height * 0.05),
// EdgeInsets.all(21.0),
// left: 28, top: 25, right: 28, bottom: 25),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
// all together looks COOL royal Blue:
Colors.tealAccent[400],
Colors.cyan[900],
Colors.teal
// Color(0xff0E0077),
// Color(0xff1B00EE)
],
// begin: Alignment.centerLeft,
// end: Alignment.centerRight,
),
),
),
// color: Colors.blue[300],
// highlightColor: Colors.blue,
splashColor: Colors.cyan,
// ),
textColor: Colors.white,
)
: Container(),
),
),
If you need to determine the screen orientation, you can do it like so:
var orientation = MediaQuery.of(context).orientation;
For a fully working solution that will help you scale elements correctly, I strongly suggest exploring this repo: https://github.com/TechieBlossom/learning_platform_app
There's even a video series with explanations that accompany it:
https://www.youtube.com/watch?v=afBmGC63iIQ

Image width, height not adjusting to the params in flutter

I am trying to scale down the image a bit so that it won't be cropped in grid item element. Actual image size is 300x300.
below is my code where I am explicitly trying to reduce the image width and height, I have also attempted many combinations with the fit type. I do not see any visible impact though. Can you let me know how should I achieve this? Thank you for your patience.
#override
Widget build(BuildContext context) {
return Scaffold(
body: GridView.count(
padding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
childAspectRatio: 1.5,
crossAxisCount: 2,
children: List.generate(
8,
(index) {
return Center(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(5),
child: CircleAvatar(
backgroundImage: Image.asset(
'assets/icons/antenna_icon.png',
height: 30,
width: 30,
fit: BoxFit.fitWidth,
).image,
backgroundColor: Colors.grey.shade300,
radius: 50,
)),
Text(
"Evve",
style: GoogleFonts.roboto(color: Colors.black87, fontSize: 15),
),
],
));
},
),
));
}
You can use Transform.scale as the child of the CircleAvatar and set the image as the child of the Transform.scale:
CircleAvatar(
child: Transform.scale(
scale: 0.8,
child: Image.asset(
'assets/icons/antenna_icon.png',
height: 30,
width: 30,
fit: BoxFit.fitWidth,
),
),
backgroundColor: Colors.grey.shade300,
radius: 50,
)

Overflow hidden for CircleAvatar wiget (Flutter)

I need to put icon into CircleAvatar Widget to allow user upload his image.
Something like this:
This is my code:
child: CircleAvatar(
child: Stack(
children: <Widget>[
Positioned(
bottom: 0,
right: 0,
left: 0,
height: 33,
child: Container(
height: 20,
width: 30,
color: Color.fromRGBO(0, 0, 0, .74),
child: Center(
child:
Icon(Icons.photo_camera, color: Colors.grey),
),
),
)
],
),
radius: 68.0,
backgroundImage:
NetworkImage('https://via.placeholder.com/300'),
backgroundColor: Colors.transparent,
)
But I have this result:
Internal box with camera icon overflow from parent widget (CircleAvatar);
What you want can be simply done with - ClipOval
Your Code:
body: Center(
child: CircleAvatar(
child: ClipOval(
child: Stack(
children: <Widget>[
Image.network('https://via.placeholder.com/300'),
Positioned(
bottom: 0,
right: 0,
left: 0,
height: 33,
child: GestureDetector(
onTap: (){
print('upload Clicked');
},
child: Container(
height: 20,
width: 30,
color: Color.fromRGBO(0, 0, 0, .74),
child: Center(
child: Icon(Icons.photo_camera, color: Colors.grey),
),
),
),
),
],
),
),
radius: 68.0,
// backgroundImage: NetworkImage('https://via.placeholder.com/300'),
backgroundColor: Colors.transparent,
),
),
Output:
You can create your Custom Clipper to give the circular bottom to your widget.
You need to use the ClipPath widget for it, pass your widget in your case Container containing your camera icon.
and in clipper pass your CustomClipper.
ClipPath
return ClipPath(
child: your_widget,
clipper: BottomWaveClipper(),
);
CustomCliper
class BottomCircleClipper extends CustomClipper<Path> {
#override
Path getClip(Size size) {
//your path to draw circle
}
#override
bool shouldReclip(TriangleClipper oldClipper) => false;
}
Here are some links that will help you.
https://iirokrankka.com/2017/09/04/clipping-widgets-with-bezier-curves-in-flutter/
https://medium.com/flutter-community/clipping-in-flutter-e9eaa6b1721a