I'm trying to create a circle image. Unfortunately, the width of the container is not honoured and I can't figure out why. What am I missing?
Drawer _getDrawer(List<Job> data) {
return Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the Drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
_getDrawerHeader(),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
},
),
],
),
);
}
DrawerHeader _getDrawerHeader() {
return DrawerHeader(
child: StreamBuilder(
stream: FirebaseAuth.instance.currentUser().asStream(),
builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
if (snapshot.hasData) {
return ListView(
children: <Widget>[
_getCircleImage(snapshot.data.photoUrl),
Text('test'),
Text('test'),
],
);
}
return Center(child: CircularProgressIndicator());
}),
decoration: BoxDecoration(
color: Colors.blue,
),
);
}
_getCircleImage(String url) {
return Container(
width: 64.0,
height: 64.0,
decoration: new BoxDecoration(
image: new DecorationImage(
image: new NetworkImage(url),
fit: BoxFit.cover,
),
shape: BoxShape.circle,
),
);
}
Flutter – Change Container Border's Color & Width To change the color and width of Container's border, use its decoration property. Set decoration property with BoxDecoration() object. Set the border property of BoxDecoration() object, with required color and width as per your applications specifications.
Creating a Full Width Button in Flutter (The Solution)The full width is set to the Elevated Button by adding a style parameter. Then you can use the ElevatedButton. styleFrom class to provide the size of the button using the property called minimumSize.
Container class in Flutter. Container class in flutter is a convenience widget that combines common painting, positioning, and sizing of widgets. A Container class can be used to store one or more widgets and position it on the screen according to our convenience. Basically a container is like a box to store contents.
That's a little tricky but it's how Flutter works, your Container doesn't know the constraints of the Parent, then It try to fill all the space available. Show activity on this post. A widget can decide its own size only within the constraints given to it by its parent.
Widgets themselves do not have constraints. When you specify a width / height on a Container, you're not constraining Container. You're constraining the child of Container. Container will then size itself based on the size of its child. As such, parent widgets always have the last word on how their descendants should be sized.
Since your outer container gives minWidth=minHeight=200 constraints to the inner container, thus the size of your inner container cannot be less than Size (200, 200).
That's a little tricky but it's how Flutter works, your Container
doesn't know the constraints of the Parent, then It try to fill all the space available.
You can fix it adding an Align
Widget
_getCircleImage(String url) {
return Align(
alignment: Alignment.centerLeft,
child: Container(
width: 64.0,
height: 64.0,
decoration: new BoxDecoration(
image: new DecorationImage(
image: new NetworkImage(url),
fit: BoxFit.cover,
),
shape: BoxShape.circle,
),
),
);
}
More info : https://docs.flutter.io/flutter/widgets/Container-class.html
A widget can decide its own size only within the constraints given to it by its parent. This means a widget usually can’t have any size it wants.
A widget can’t know and doesn’t decide its own position on the screen, since it’s the widget’s parent who decides the position of the widget.
The parent widget takes the entire space available to draw the widget, Here Container is the parent widget, and it's taking whatever space is available, so to give heigh/width to the Container, that needed to be placed inside any widget which assigns x,y position of widgets to get it to draw.
So as per the above description, Container
should have a parent that aligns Container
on the screen.
Ex: Use
Align(
alignment: Alignment.center,
child: Container(),
);
OR
Center(
child: Container(),
);
Problem:
Container
's constraints wont' have any impact if the passed constraints were tight.
For example:
SizedBox.fromSize(
size: Size.fromRadius(100), // Tight constraints are passed to the Container below
child: Container(
width: 100, // No impact
height: 100, // No impact
color: Colors.blue,
),
)
Solutions:
You need to loose those tight constraints. There are multiple ways of doing it. I'm listing a few here:
Use UnconstrainedBox
:
SizedBox.fromSize(
size: Size.fromRadius(100),
child: UnconstrainedBox( // <-- UnconstrainedBox
child: Container(
width: 100, // Works
height: 100, // Works
color: Colors.blue,
),
),
)
Use Center
or Align
:
SizedBox.fromSize(
size: Size.fromRadius(100),
child: Center( // <-- Center
child: Container(
width: 100, // Works
height: 100, // Works
color: Colors.blue,
),
),
)
Use a Column
or better Wrap
:
SizedBox.fromSize(
size: Size.fromRadius(100),
child: Wrap( // <-- Wrap
children: [
Container(
width: 100, // Works
height: 100, // Works
color: Colors.blue,
),
],
),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With