I want to absolutely position a text on a container.
return Container(
height: 150,
width: 150,
decoration: BoxDecoration(
color: Color(0xE5E5EAee), borderRadius: BorderRadius.circular(20)),
child: Text('Camping'),
);
This is the present image
This is what need to be acheived
Did tried to position it with the 'align positioned' dependency. But got stuck with the same result.
using your own example considering there is only one child in the container u can do the following but if u do have additional childrens the best way would be to use a Stack widget and use Align/Positioned widget to position the child widgets as per ur needs
Container(
height: 150,
width: 150,
padding: EdgeInsets.only(bottom: 10), // how much space from the bottom
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
color: Color(0xE5E5EAee),
borderRadius: BorderRadius.circular(20)),
child: Text('Camping'),
)
You can try this, use a Stack and positioned the Text with the widget Positioned as you need it, Positioned has 4 properties for position top, rigth, bottom and left for you to positioned the widget wherever you want.
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 150,
width: 150,
decoration: BoxDecoration(
color: Color(0xE5E5EAee), borderRadius: BorderRadius.circular(20)),
child: Stack(
children: [
Positioned(
top: 120,
left: 48,
child: Text('Camping'))
,
],
),
);
;
}
}
Wrap the Text widget with Align widget and give alignment as the below code
Container(
height: 150,
width: 150,
decoration: BoxDecoration(
color: Color(0xE5E5EAee), borderRadius:
BorderRadius.circular(20)),
child: Align(
alignment: Alignment.bottomCenter, // ADD THIS
child: Text('Camping')),
),
Then give padding as per your need
Related
I want to add background that has circle like picture below:
And in my Figma, has asset like this:
And my code is like this:
Widget buildArea(int index) => Card(
color: ColorName.brandSecondaryBlue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
child: Container(
padding: const EdgeInsets.only(top: 20, bottom: 20),
child: buildAreaItem(),
),
);
Anyone else who know how to add it?
May not be a clean way but try Using a Stack widget and use Positioned widget inside.
Here's a sample for what you are trying to achieve.
Scaffold(
body: Center(
child: Stack(
children: [
Container(
width: 300,
height: 150,
decoration: BoxDecoration(
color: Colors.indigo,
borderRadius: BorderRadius.circular(20)
),
),
Positioned(
left: -50,
top: -35,
child: Stack(
alignment: AlignmentDirectional.center,
children:
[
Container(
width: 150,
height: 150,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(100)
),),
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.indigo,
borderRadius: BorderRadius.circular(100)
),),
]
))
],
),
)
)
Try This Code:
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import 'dart:ui';
import 'package:google_fonts/google_fonts.dart';
import 'package:myapp/utils.dart';
class circlescreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
double baseWidth = 428;
double fem = MediaQuery.of(context).size.width / baseWidth;
double ffem = fem * 0.97;
return Container(
width: double.infinity,
child: Container(
// iphone13promax1auq (1:2)
width: double.infinity,
decoration: BoxDecoration (
color: Color(0xff3241a0),
borderRadius: BorderRadius.circular(20*fem),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
// ellipse1hmV (1:4)
margin: EdgeInsets.fromLTRB(0*fem, 0*fem, 237*fem, 272*fem),
width: 318*fem,
height: 318*fem,
decoration: BoxDecoration (
borderRadius: BorderRadius.circular(159*fem),
border: Border.all(color: Color(0xff3c4baf)),
),
),
Container(
// ellipse3jCP (1:6)
margin: EdgeInsets.fromLTRB(0*fem, 0*fem, 0*fem, 113*fem),
width: 198*fem,
height: 198*fem,
decoration: BoxDecoration (
borderRadius: BorderRadius.circular(99*fem),
border: Border.all(color: Color(0xff3c4baf)),
),
),
Container(
// ellipse2oi3 (1:5)
margin: EdgeInsets.fromLTRB(0*fem, 0*fem, 205*fem, 0*fem),
width: 318*fem,
height: 318*fem,
decoration: BoxDecoration (
borderRadius: BorderRadius.circular(159*fem),
border: Border.all(color: Color(0xff3c4baf)),
),
),
],
),
),
);
}
}
export your figma component with the just the background( purple with circles).
Then add that in assets and use it in the child of Card. So your widget tree would be :
Card
=>
Container( //with background decoration as image and necessary padding if any
)
=>
buildAreaItem()
You can also crop the image to get just the top corner ring or play with the fit property in DecorationImage widget.
I Have the following build method
#override
Widget build(BuildContext context) {
return Container(
width: widget.size.width,
height: widget.size.height,
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
color: widget.backgroundColor,
borderRadius: const BorderRadius.all(
Radius.circular(30),
),
),
child: Stack(
fit: StackFit.expand,
children: [
Container(
height: 5,
color: widget.glossColor,
),
Container(
height: 10,
color: widget.glossColor2,
),
],
),
);
}
I want the change the height of the container with the gloss color. But it's height and width are always the same as the outer container doing it like this.
I want to make it so that the stack has the same size as the outer container and the content can never be bigger and scales down to fit the stack. But also I want to be able to control the height and width of the container inside the stack.
Instead of wrapping small containers with Expanded, wrap it with IntrinsicHeight widget. This widget sizes its child to the child's intrinsic height.
Example:
IntrinsicHeight(
child: Container(
color: Colors.blue,
child: SmallContainers(),
),
),
I have a PageView with images list inside. I want to keep the image ratio and set a rounded corners to them.
Usually I had no problem to clip image inside simple list or single image.
But in this case, the ClipRRect is not wrapping the image and when I'm setting size to red Container nothing happened.
Result :
Code :
double maxiHeight = 250;
List<Widget> postList = [];
#override
void initState() {
for(Post p in Model.instance.postList) {
postList.add(post(p));
}
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
height: maxiHeight,
child: PageView(
controller: PageController(viewportFraction: 0.5),
children: postList
),
);
}
Widget post(Post post) {
return ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Container(
margin: EdgeInsets.only(right: 10),
color: Colors.red,
child: Image.network(post.thumb, height: 50)
)
);
}
I want to keep my image ratio.
What am I missing here ?
About your code snippet, you are providing margin right 1st then wrapping with ClipRRect. Here the main Container is getting its size and then using margin, after wrapping with ClipRRect it is avoiding 10px from right to Clip. And this is how we are getting current output.
But we want padding outside The Container and without border radius. Means after decorating the image, just provide some padding. You can follow this and using fit: BoxFit.cover will cover the widget size.
Center(
child: Padding(
padding: EdgeInsets.only(right: 10),
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Container(
// margin: EdgeInsets.only(right: 10),// not here
color: Colors.red,
child: Image.network(
// post.thumb,
"https://unsplash.it/1440/3040?random",
// fit: BoxFit.fitHeight,
height: 150,
),
),
),
),
),
With the following modified code it should work:
#override
Widget build(BuildContext context) {
return Center(
child: SizedBox(
height: 500.0,
child: ListView(scrollDirection: Axis.horizontal, children: postList),
),
);
}
Widget post(String uri) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.network(uri, fit: BoxFit.fitHeight)
),
);
}
Try to add BoxFit.cover to Image.network widget
Widget post(Post post) {
return ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Container(
margin: EdgeInsets.only(right: 10),
color: Colors.red,
child: Image.network(post.thumb, height: 50, fit: BoxFit.cover,),
)
);
}
I need to make an svg image size like on the photo below
But when i'm trying to give this size through SvgPicture.asset constructor or with a SizedBox it doesn't make any effect and i'm getting something like this
here is my code of that widget
Container(
width: 80,
height: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: yellowGradient,
),
child: SizedBox(
height: 29,
width: 45,
child: SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
),
),
),
I think that my Svg picture stretches to container size but i don't know what causes such a behaviour because there is SizedBox
Container doesn't know how to align the child if it is smaller than the container itself, so it expands it to fill the containers size.
The SizedBox can also be removed as SvgPicture.asset(...) now accepts size parameters.
Based on the above information, a possible solution for you will be the following, the important bit being the addition of alignment: Alignment.center:
Container(
width: 80,
height: 80,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: yellowGradient,
),
child: SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
height: 29,
width: 45,
),
),
For further information, refer to "Layout behaviour" in the Container widget documentation here.
you can use padding or margin
Container(
width: 40,
height: 40,
padding: const EdgeInsets.all(15),
child: SvgPicture.asset(
"assets/icons/outline/edit2.svg",
),
);
Instead of wrapping it with a SizedBox, use the fields height and width of the SvgPicture.
SvgPicture.asset(
dishIconPath,
color: whiteIconColor,
height: 29,
width: 45,
),
I am learning Flutter development. I want to display some rounded square shaped containers with short text in a row. However, the possible shapes are either a circle or rectangle. So I decided to set the width and height of the rectangle so that they would be equal.
The following is the code I used to create the Container
Container(
width: double.maxFinite,
height:
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
child: Text(keyText),
),
When I set the shape in Box decoration alone, the Container resized to the size of the text. When I set the width property of the Container, I was able to divide the available width of the screen among the containers, which ensures flexibility of size if the screen was smaller or bigger, instead of hard coding it. Now I want to make the height the exact same size that the width turns out to be, thus obtaining a square container. Any idea how I could do that?
EDIT: I tried the following code and this is how the containers are looking like in both cases:
Container(
alignment: Alignment.center,
width: double.maxFinite,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
After using MediaQuery.of(context).size.width for height property or both
Here is a solution without using MediaQuery:
AspectRatio(
aspectRatio: 1,
child: Container(
width: double.infinity,
child: Text(),
),
)
Use MediaQuery. MediaQuery.of(context).size.width gives you the Screen width size value.
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
child: Text(keyText),
)
check this quick example that I made just for you!
// This function is just an example
List<Widget> buildContainers(BuildContext context, int containerCount) {
// Here you get the width of all your containers
final containerWidth = MediaQuery.of(context).size.width / containerCount;
// Make height = width for squares!
final containerHeight = containerWidth;
// We will gather all containers in this list
List containerList = <Container>[];
for (var i = 0; i < containerCount; i++) {
containerList.add(
Container(
// Use the predefined width and height here!
width: containerWidth,
height: containerHeight,
// Random color to differentiate each container
// You could replace this with your child widget
color: Color(
Random().nextInt(0xFFFFFFFF),
),
),
);
}
return containerList;
}
#override
Widget build(BuildContext context) {
return Container(
child: Row(
children: [
// If you are wondering what are these dots (...)
// they are called "spread operators" and you can find more about them here:
// https://dart.dev/guides/language/language-tour#spread-operator
...buildContainers(context, 7),
],
),
);
}
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: ThemeProvider.themeOf(context).data.primaryColor,
borderRadius: BorderRadius.circular(10),
),
child: Text(keyText),
),