show Image over image flutter - flutter

Need to show image above other image outside the widget margin.
I tried using stack, but it not coming outside.
Stack(children: <Widget>[
Container(child: Image.asset('assets/discover.png')),
Row(
children: <Widget>[
Expanded(child: Text(''), flex: 9),
Expanded(
child: ClipRRect(
borderRadius: new BorderRadius.circular(80.0),
child: Image.asset('assets/user.jpg')),
flex: 6,
),
Expanded(child: Text(''), flex: 9)
],
),
]);

Below source code works for your scenario. You have to give top padding for the image's container and make the entire stack's alignment as topCenter.
Additional things:
Consider giving height and width for the circled image.
Keep the big image widget in AspectRatio widget to occupy the full width of its parent widget and give fit property as well for showing entire image in the right fit.
.
Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 40),
child: AspectRatio(aspectRatio: 1, child: Image.asset('assets/discover.png', fit: BoxFit.cover),),
),
ClipRRect(
borderRadius: new BorderRadius.circular(40.0),
child: Image.asset('assets/user.jpg', height: 80, width: 80),
),
],
)

return Scaffold(
appBar: AppBar(
title: const Text('Flutter demo'),
),
body: Container(
padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
alignment: Alignment.topCenter,
child: Stack(children: <Widget>[
Container(
margin: EdgeInsets.only(top: 30),
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQGLanNnFsLi3QnQFdh-k-mkwG6yrEEXhorSoElObizTnP0_8rR')),
),
Align(
alignment: Alignment.topCenter,
child: CircleAvatar(
radius: 30.0,
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSIekuJOwtOWtZl9QX3t46Yz_7RCZ4Kpebnugsst2OFfNl-SGjf'),
backgroundColor: Colors.grey,
),
)
]),
));

Stack(children: <Widget>[
Container(
margin: EdgeInsets.only(top: 50.0),
child: Image.network(
'https://cdn.pixabay.com/photo/2015/06/19/21/24/the-road-815297__340.jpg'),
),
Row(
children: <Widget>[
Expanded(child: Text(''), flex: 9),
Expanded(
child: ClipRRect(
borderRadius: new BorderRadius.circular(80.0),
child: Image.asset('assets/ic_profile.png'),
),
flex: 6,
),
Expanded(child: Text(''), flex: 9)
],
)
]),

Related

Flutter: White space coming while using BoxConstraints

White space coming in horizontal view, while giving max width to Container with BoxConstraints.
Versions
Flutter: 3.3.0
Dart: 2.18.0
My code
return Scaffold(
body: Container(
decoration: ...,
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
flex: 1,
child: Container(
padding: const EdgeInsets.all(10.0),
child: Image.asset(
'assets/logo/logo.png',
width: 300,
),
),
),
Flexible(
flex: 2,
child: Card(
shape: ...,
elevation: 8.0,
child: Container(
constraints: const BoxConstraints(
maxWidth: 500,
),
height: 400,
child: ListView(
padding: const EdgeInsets.all(40),
children: [
Form(
key: _formKey,
child: Column(
children: [
...
],
),
),
],
),
),
),
),
],
),
),
);
Screen short:
Horizontal view, white space coming on right side
Vertical view
Just set the outer Container width to the screen width wit MediaQuery.of(context).size.width or even easier to double.infinity. It's going to be like the following:
return Scaffold(
body: Container(
decoration: ...,
width: double.infinity,
padding: const EdgeInsets.all(16.0),

Align children with first child in Column

How can I align my widget to take the same width as ClipRect image?
Right now it overflows in my screen. Find attached how it is and how I desire my output would be.
Is it possible to center it within the red lines I drew?
Widget build(BuildContext context) {
final mainChildKey = GlobalKey();
return Flexible(
child: Column(
children: <Widget>[
ColumnWithMainChild(
mainChildKey: mainChildKey,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
imgparam,
width: 350,
height: 200,
fit: BoxFit.cover,
),
),
Padding(
padding: EdgeInsetsDirectional.fromSTEB(0, 8, 0, 8),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Text(
'Good with children: $param_good_children',
),
GFProgressBar(
width: 150,
percentage: 0.3,
backgroundColor: Colors.pink,
progressBarColor: Colors.red,
)
],
),
Divider(),
Text(
'Good with children: $param_good_children',
),
],
),
),
),
],
),
]));
}
}
Do the following Changes
Set Padding to Parent column
Set width of NetworkImage to double.infinity
Wrap your Progress Bar with Flexible
Code:
Widget build(BuildContext context) {
final mainChildKey = GlobalKey();
return Scaffold(
appBar: AppBar(),
body: Padding( // Set Padding to Paren
padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
"https://images.unsplash.com/photo-1474922651267-219b23864ae8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80",
width: double.infinity, // Set width of NetworkImage to double.infinity
height: 200,
fit: BoxFit.cover,
),
),
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(0, 8, 0, 8),
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
const Text(
'Good with children: 5',
),
Flexible( //Wrap your Progress Bar with Flexible
child: GFProgressBar(
width: 150,
percentage: 0.3,
backgroundColor: Colors.pink,
progressBarColor: Colors.red,
),
)
],
),
const Divider(),
const Text(
'Good with children: 5',
),
],
),
),
),
]),
),
);
}
Output:

Flutter appbar Row independent alignment

I've been trying to achieve this alignment in a flutter AppBar:
I tried doing it in multiple ways, with Flex (which didn't really work, all the widget got clustered together) and with static paddings...
Flex:
Don't know why my sizedBoxes expand... Also I don't know how to align the button, dropdown and drawer to the left...
AppBar(
backgroundColor: Theme.of(context).colorScheme.background,
automaticallyImplyLeading: false,
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
fit: FlexFit.tight,
flex: 4,
child: Container(
color: Theme.of(context).colorScheme.onPrimary,
child: Column(
children: const [
Image(
fit: BoxFit.contain,
image: AssetImage('assets/images/danube_logo.png'),
),
],
),
),
),
Flexible(
fit: FlexFit.tight,
flex: 6,
child: Container(
color: Theme.of(context).colorScheme.onPrimary,
child: Column(
children: const [
Image(
fit: BoxFit.contain,
image: AssetImage('assets/images/eu_logo.png'),
),
],
),
),
),
const Flexible(
fit: FlexFit.tight,
flex: 2,
child: MyTripsButton(),
),
const Flexible(
fit: FlexFit.tight,
flex: 2,
child: LanguageSelector(),
),
const Flexible(
fit: FlexFit.tight,
flex: 2,
child: DrawerMenuButton(),
),
],
),
);
Paddings, kinda work but not the best solution...:
AppBar(
backgroundColor: Theme.of(context).colorScheme.background,
automaticallyImplyLeading: false,
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(left: 13.0, right: 130.0),
child: Container(
color: Theme.of(context).colorScheme.onPrimary,
child: Column(
children: const [
Image(
fit: BoxFit.contain,
image: AssetImage('assets/images/danube_logo.png'),
),
],
),
),
),
Container(
color: Theme.of(context).colorScheme.onPrimary,
child: Column(
children: const [
Image(
fit: BoxFit.contain,
image: AssetImage('assets/images/eu_logo.png'),
),
],
),
),
Padding(
padding: const EdgeInsets.only(right: 25.0),
child: const MyTripsButton(),
),
Padding(
padding: const EdgeInsets.only(right: 50.0),
child: const LanguageSelector(),
),
Padding(
padding: EdgeInsets.only(
right: 30.0,
),
child: DrawerMenuButton()),
],
),
);
Am I going around this the wrong way?
Just use two row widgets. Wrap your 2 left widgets within one row and other widgets in the second row. After this wrap both of your rows into a parent row and give the property of spaceBetween. Not the best solution but it will work

Flutter: How to add space between two buttons in a row

I created two container in a row and want it in center of the screen so i used padding for it, but they more are close to each other, i want to add space between them. here is the snap of output.
Code:
Padding(
padding: EdgeInsets.fromLTRB(40, 250, 30, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Colors.pink,
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("in",style:TextStyle(fontSize: 20))),
],
),
),
),
),
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Color(int.parse(presentcolor)),
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("out",style:TextStyle(fontSize: 20))),
],
),
),
),
),
Use a Spacer or a SizedBox between 2 widgets.
Other option is to set mainAxisAlignment of Row to MainAxisAlignment.spaceBetween.
I started using the Gap package for adding spaces between widgets in rows or columns. Basically you just specify how big the space should be, so an 8px gap would be Gap(8).
Row(
children: [
widget1(),
Gap(8),
widget2(),
]);
The syntax is easy to use and there are special widgets like the MaxGap or SliverGap for specific use cases.
for centering this you can use Center() widget instead of padding and for putting space between the two you can add margin for the first one from the left. the code will be like this:
Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
flex: 2,
child: Container(
margin: EdgeInsets.only(right: 20),
decoration: BoxDecoration(
color: Colors.pink,
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("in",style:TextStyle(fontSize: 20))),
],
),
),
),
),
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Color(int.parse(presentcolor)),
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("out",style:TextStyle(fontSize: 20))),
],
),
),
),
),
you can change righ margin to the suitable value for you.
This is the easiest way to add "Text" containing 4 spaces or or however many spaces you like:
Row(
children: [
Flexible(),
Text(" "),
Flexible(),
]);

Fill all available horizontal space in a stack

Inside a card I have a stack with 1) an image and 2) a text inside a container.
How can I make the container width to fill the card width?
Card(
clipBehavior: Clip.antiAlias,
child: Stack(
children: <Widget>[
Positioned.fill(child: Image.network(
image_url,
fit: BoxFit.fitWidth,
),
),
Positioned(
bottom: 0,
child: Container(
padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
decoration: new BoxDecoration(color: Colors.black12),
child: Row(
children: <Widget>[
Text("test1"),
Text("test2"),
Text("test3"),
],
),
),
),
],
)
);
Set the left and right values to 0 so that the container will behave so.
Card(
clipBehavior: Clip.antiAlias,
child: Stack(
children: <Widget>[
Positioned.fill(child: Image.network(
image_url,
fit: BoxFit.fitWidth,
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
padding: new EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
decoration: new BoxDecoration(color: Colors.black12),
child: Row(
children: <Widget>[
Text("test1"),
Text("test2"),
Text("test3"),
],
),
),
),
],
)
);
Wrap all the childrens with the Expanded widget.
Row(
children: <Widget>[
Expanded(
child: Text("test1")
),
Expanded(
child: Text("test2")
),
Expanded(
child: Text("test3")
),
],
),
From the Expanded Widget Documentation:
Using an Expanded widget makes a child of a Row, Column, or Flex expand to fill the available space in the main axis (e.g., horizontally for a Row or vertically for a Column). If multiple children are expanded, the available space is divided among them according to the flex factor.