I am trying to build something similar to a chat bubble, where each chat bubble would have text aligned normally, and timestamp aligned to the bottom right of the bubble. The size of the bubble must expand with the text, and should not take more space than necessary.
In order to do this I used a Column with two Container children. The second child would be having the timestamp.
When I do this without adding "alignment" property to the second Container child, the bubble size correctly shrinks/expands to the text, like so:
But when I add alignment: Alignment.bottomRight to the second container, the bubble size expands to fill the entire width of the screen.
Is there a way to align the "7:30 PM" text to the bottom right of the bubble without expanding the bubble size?
Scaffold(
body: Container(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
child: Text(
"Without Alignment",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
),
),
Container(
// alignment: Alignment.bottomRight,
decoration: BoxDecoration(
border: Border.all(),
),
child: Text(
"7:30 PM",
textDirection: TextDirection.rtl,
textAlign: TextAlign.right,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
),
),
],
),
),
),
This may help you
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MainWidget(),
);
}
}
class MainWidget extends StatefulWidget {
#override
_MainWidgetState createState() => _MainWidgetState();
}
class _MainWidgetState extends State<MainWidget> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
shrinkWrap: true,
children: <Widget>[
ReceivedMessageBubble(
message:
"This is a demo message send by XXXX. So don't reply it. Trying to making it multiple lines. You can increase it",
time: "12:45 PM",
),
SendMessageBubble(
message:
"Oh!!! OK",
time: "12:45 PM",
),
],
),
);
}
}
class SendMessageBubble extends StatelessWidget {
final String message;
final String time;
const SendMessageBubble({
Key key,
this.message,
this.time,
}) : assert(message != null),
assert(time != null),
super(key: key);
#override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
double maxBubbleWidth = constraints.maxWidth * 0.7;
return Align(
alignment: Alignment.centerRight,
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxBubbleWidth),
child: Container(
decoration: BoxDecoration(
color: Colors.blue[300],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(0.0),
bottomLeft: Radius.circular(10.0),
bottomRight: Radius.circular(10.0),
),
),
margin: const EdgeInsets.all(10.0),
padding: const EdgeInsets.all(10.0),
child: IntrinsicWidth(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(message),
const SizedBox(height: 5.0),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
time,
style: const TextStyle(
color: Colors.black54,
fontSize: 10.0,
),
),
],
)
],
),
),
),
),
);
},
);
}
}
class ReceivedMessageBubble extends StatelessWidget {
final String message;
final String time;
const ReceivedMessageBubble({
Key key,
this.message,
this.time,
}) : assert(message != null),
assert(time != null),
super(key: key);
#override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
double maxBubbleWidth = constraints.maxWidth * 0.7;
return Align(
alignment: Alignment.centerLeft,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: maxBubbleWidth,
),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(0.0),
topRight: Radius.circular(10.0),
bottomLeft: Radius.circular(10.0),
bottomRight: Radius.circular(10.0),
),
),
margin: const EdgeInsets.all(10.0),
padding: const EdgeInsets.all(10.0),
child: IntrinsicWidth(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(message,),
const SizedBox(height: 5.0),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(
time,
style: const TextStyle(
color: Colors.black54,
fontSize: 10.0,
),
),
],
)
],
),
),
),
),
);
},
);
}
}
Here is the Demo DartPad
i edited my answer, try this :
Column(crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Container(
child: Text(
"Without Alignment",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
),
),
Container(
//alignment: Alignment.bottomRight,
//constraints: BoxConstraints.expand(),
decoration: BoxDecoration(
border: Border.all(),
),
child: Text(
"7:30 PM",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
),
),
],
);
try this, it's work on me..
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 300,
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.blue,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Text(
"Without Alignment",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(),
),
child: Text(
"7:30 PM",
textDirection: TextDirection.rtl,
textAlign: TextAlign.right,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
),
),
],
)
],
),
),
),
Related
I am trying to put some widgets in a row where the first widget should be square shaped and use the intrinsic width and height to modify the smaller one of these values so it results in a square shape. The side length should depend on the child widget, which is in my case a Text. Padding the text equally from all sides does not look right. I already tried achieving this with the AspectRatio widget and its working as far as it results in the expected square shape, but it does not use the minimal space but rather scales up as much as possible.
Any idea how to get this result without hardcoding the width and height?
example code
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 100),
Text(' current status:'),
SizedBox(height: 5),
Row(
children: [
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.red.shade300,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue),
),
child: const Center(
child: Text(
'square',
textAlign: TextAlign.center,
),
),
),
SizedBox(width: 10),
Flexible(
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.blue.shade200,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.red),
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'placeholder for other widgets',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
],
),
SizedBox(height: 50),
Text(' I want this:'),
SizedBox(height: 5),
Row(
children: [
SizedBox(
height: 50,
child: AspectRatio(
aspectRatio: 1,
child: Container(
decoration: BoxDecoration(
color: Colors.red.shade300,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue),
),
child: Center(
child: Text(
'square',
textAlign: TextAlign.center,
),
),
),
),
),
SizedBox(width: 10),
Flexible(
child: Container(
height: 50,
decoration: BoxDecoration(
color: Colors.blue.shade200,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.red),
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'placeholder for other widgets',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
],
),
],
),
),
);
}
}
EDIT:
my code looks like this now
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey _key = GlobalKey();
double boxWidth = 0.0;
final String inputText = 'square';
#override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
BuildContext? keyContext = _key.currentContext;
if (keyContext != null) {
RenderBox renderBox = keyContext.findRenderObject() as RenderBox;
boxWidth = renderBox.size.width;
}
});
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 100),
Text(' input: $inputText'),
const SizedBox(height: 50),
const Text(' with StatefulWidget'),
const SizedBox(height: 10),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.all(10),
key: _key,
height: boxWidth,
decoration: BoxDecoration(
color: Colors.red.shade300,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue),
),
child: Center(
child: Text(
inputText,
textAlign: TextAlign.center,
),
),
),
const SizedBox(width: 10),
Flexible(
child: Container(
padding: const EdgeInsets.all(10),
height: boxWidth,
decoration: BoxDecoration(
color: Colors.blue.shade200,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.red),
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'placeholder for other widgets',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
],
),
const SizedBox(height: 50),
const Text(' with AspectRatio'),
const SizedBox(height: 10),
Row(
children: [
Flexible(
flex: 2,
child: AspectRatio(
aspectRatio: 1,
child: Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.red.shade300,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue),
),
child: Center(
child: Text(
inputText,
textAlign: TextAlign.center,
),
),
),
),
),
const SizedBox(width: 10),
Flexible(
flex: 8,
child: Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.blue.shade200,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.red),
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'placeholder for other widgets',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
],
),
],
),
),
);
}
}
This is how the different solution approaches behave:
You can achieve this using flexible and aspect ratio
Row(
children : [
Flexible(
flex: 2,
child: AspectRatio(
aspectRatio: 1,
Container(
color: Colors.red,
)
)
),
Flexible(
flex: 8,
child: Text(),
)
]
)
You can change the flex value as per your requirement
Edit
You can use intrinsic height in a row to set the child to the tallest child
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SizedBox(
width: MediaQuery.of(context).size.width,
child: IntrinsicHeight(
child: Row(children: [
AspectRatio(aspectRatio: 1, child: Container(color: Colors.red)),
Flexible(
child: Text(
"djdjdjdhjvc gf\nf bg \nhg hshs\nfjgdhjfhj \n hxhkj \n fjdjd d \n ejejdj "))
])));
}
}
you can do this:
GlobalKey _key = GlobalKey();
double boxWidth = 0.0;
#override
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
final _keyContext = _key.currentContext;
if (_keyContext != null) {
final box = _keyContext.findRenderObject() as RenderBox;
boxWidth = box.size.width;
}
});
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.all(10),
key: _key,
height: boxWidth,
decoration: BoxDecoration(
color: Colors.red.shade300,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.blue),
),
child: const Center(
child: Text(
'square',
textAlign: TextAlign.center,
),
),
),
SizedBox(width: 10),
Flexible(
child: Container(
padding: EdgeInsets.all(10),
height: boxWidth,
decoration: BoxDecoration(
color: Colors.blue.shade200,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.red),
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'placeholder for other widgets',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
),
],
),
],
),
),
);
}
Flutter - While importing listview widget from one file then it just throws an error on the app's screen "Bottom overflowed by 500 pixels" but when if listview is directly used inside the home file it works fine. only throws error while importing listview widget from another file.
Here is the home file and the file which has listview widget
import 'package:flutter/material.dart';
import 'package:flutter_application_1/Pages/widgets/exercise_list.dart';
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: const [
ExerciseList()
],
),
),
);
}
}
Second file
import 'package:flutter/material.dart';
class ExerciseList extends StatelessWidget {
const ExerciseList({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return ListView(
shrinkWrap: true,
physics: const AlwaysScrollableScrollPhysics(),
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.all(5),
width: 150,
height: 135,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: const Image(
image: NetworkImage(
"https://media.istockphoto.com/photos/man-lifting-weights-on-a-bench-press-picture-id180200014?b=1&k=20&m=180200014&s=170667a&w=0&h=VE9cTw0Pyus1IIENTjWxSkM9wyQhPFFIxikCpHDbwm8=")),
),
const SizedBox(
height: 8,
),
const Text(
"Chest Workout",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
)
],
),
),
),
],
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.all(5),
width: 150,
height: 135,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: const Image(
image: NetworkImage(
"https://media.istockphoto.com/photos/man-lifting-weights-on-a-bench-press-picture-id180200014?b=1&k=20&m=180200014&s=170667a&w=0&h=VE9cTw0Pyus1IIENTjWxSkM9wyQhPFFIxikCpHDbwm8=")),
),
const SizedBox(
height: 8,
),
const Text(
"Chest Workout",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
)
],
),
),
),
],
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.all(5),
width: 150,
height: 135,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: const Image(
image: NetworkImage(
"https://media.istockphoto.com/photos/man-lifting-weights-on-a-bench-press-picture-id180200014?b=1&k=20&m=180200014&s=170667a&w=0&h=VE9cTw0Pyus1IIENTjWxSkM9wyQhPFFIxikCpHDbwm8=")),
),
const SizedBox(
height: 8,
),
const Text(
"Chest Workout",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
)
],
),
),
),
],
),
],
);
}
}
Do this:
import 'package:flutter/material.dart';
class ExerciseList extends StatelessWidget {
const ExerciseList({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
//wrap your listview with a constraint widget: i.e container or SizedBox
//give it some height of your choice and wrap with SingleChildScrollview //widget to prevent the overflow from occuring
return Scaffold(
body: SingleChildScrollView(
child: SizedBox(
height:
MediaQuery.of(context).size.height, // takes screen full height
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.vertical,
//choose direction of your choice
physics: const BouncingScrollPhysics(),
// scroll effects not the actual scrolling
children: List.generate(10, (index) =>Row(
children: [
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.all(5),
width: 150,
height: 135,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: const Image(
image: NetworkImage(
"https://media.istockphoto.com/photos/man-lifting-weights-on-a-bench-press-picture-id180200014?b=1&k=20&m=180200014&s=170667a&w=0&h=VE9cTw0Pyus1IIENTjWxSkM9wyQhPFFIxikCpHDbwm8=")),
),
const SizedBox(
height: 8,
),
const Text(
"Chest Workout",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 14),
)
],
),
),
),
],
))
))),
);
}
}
in your main you don't need a column
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: const ExerciseList()
);
}
}
Try wrapping ExerciseList with SingleChildScrollView instead of Column:
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: EdgeInsets.all(20.0),
child: SingleChildScrollView(
child: ExerciseList(),
),
),
);
}
}
simply wrapped listview inside sizedbox and gave it a height of 150 and it's done we are good to go!☺☺
SizedBox(
child: ListView(
shrinkWrap: true,
physics: const AlwaysScrollableScrollPhysics(),
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.all(5),
width: 150,
height: 135,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: const Image(
image: NetworkImage(
"https://media.istockphoto.com/photos/man-lifting-weights-on-a-bench-press-picture-id180200014?b=1&k=20&m=180200014&s=170667a&w=0&h=VE9cTw0Pyus1IIENTjWxSkM9wyQhPFFIxikCpHDbwm8=")),
),
const SizedBox(
height: 8,
),
const Text(
"Chest Workout",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 14),
)
],
),
),
),
],
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.all(5),
width: 150,
height: 135,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: const Image(
image: NetworkImage(
"https://media.istockphoto.com/photos/man-lifting-weights-on-a-bench-press-picture-id180200014?b=1&k=20&m=180200014&s=170667a&w=0&h=VE9cTw0Pyus1IIENTjWxSkM9wyQhPFFIxikCpHDbwm8=")),
),
const SizedBox(
height: 8,
),
const Text(
"Chest Workout",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 14),
)
],
),
),
),
],
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.all(5),
width: 150,
height: 135,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(5),
child: const Image(
image: NetworkImage(
"https://media.istockphoto.com/photos/man-lifting-weights-on-a-bench-press-picture-id180200014?b=1&k=20&m=180200014&s=170667a&w=0&h=VE9cTw0Pyus1IIENTjWxSkM9wyQhPFFIxikCpHDbwm8=")),
),
const SizedBox(
height: 8,
),
const Text(
"Chest Workout",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 14),
)
],
),
),
),
],
),
],
));
I am trying to make an app and I have used a container and a column widget under a stack widget but the width of the container and the positioned widget of a column widget are not updating according to the screen sizes.
Screenshot:
Please check the demo code given below and re-edit the code. Thank you.
code
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
var width = MediaQuery.of(context).size.width;
return Center(
child: Container(
height: 135,
width: width,
decoration: BoxDecoration(
border: Border.all(color: Colors.yellow),
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Stack(
children: <Widget>[
Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Stack(
children: <Widget>[
Image.network(
'https://images3.alphacoders.com/823/82317.jpg',
fit: BoxFit.cover,
height: 120,
width: 120,
),
],
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 15, 5, 0),
child: Container(
width: width * 0.6,
height: 110,
child: const Text(
'product.name',
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 17),
),
),
),
],
),
),
Positioned(
top: 80,
left: width * 0.85,
child: Column(
children: const [
Text(
'Rs200',
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
),
Text(
'Rs300',
style: TextStyle(
decoration: TextDecoration.lineThrough,
fontSize: 17,
color: Colors.blueGrey),
),
],
),
),
],
)),
);
}
}
You should try this :
Container(
margin: EdgeInsets.all(16.0),
height: 135,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.yellow),
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Stack(
children: <Widget>[
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Stack(
children: <Widget>[
Image.network(
'https://images3.alphacoders.com/823/82317.jpg',
fit: BoxFit.cover,
height: 120,
width: 120,
),
],
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(50, 15, 5, 0),
child: Container(
height: 110,
child: const Text(
'product.name',
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 17),
),
),
),
],
),
),
Positioned(
top: 80,
right: 20,
child: Column(
children: const [
Text(
'Rs200',
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.bold),
),
Text(
'Rs300',
style: TextStyle(
decoration: TextDecoration.lineThrough,
fontSize: 17,
color: Colors.blueGrey),
),
],
),
),
],
),
),
And your screen look like this -
There is no need to use of any Stack. I think you can do things using the row and column widget, check out the example that i have added below let me know if it work.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: 135,
decoration: BoxDecoration(
border: Border.all(color: Colors.yellow),
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Stack(
children: <Widget>[
Image.network(
'https://images3.alphacoders.com/823/82317.jpg',
fit: BoxFit.cover,
height: 120,
width: 120,
),
],
),
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 15, 5, 0),
child: const Text(
'asdfnweoriwqer qweroiqwoer qweruqwoer sadfsdf dfsdf ',
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 17),
),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: const [
Text(
'Rs200',
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.bold),
),
Text(
'Rs300',
style: TextStyle(
decoration: TextDecoration.lineThrough,
fontSize: 17,
color: Colors.blueGrey),
),
],
),
),
],
),
],
),
),
],
)),
),
);
}
}
Layout Explanation:
There will be one row which will have two items 1) image and another will be a column.
Now the Column will have expanded widget to get max width.
Column childern will have two row widget one product name and another price row which will have Column as widget for text.
we have used row to take the max width.
For the Text to expand accordingly you have to add the expanded widget inside the row widget so that it scales based on the text that is coming.
Note : Please add padding according to your need.
Let me know if it works.
First of all, you have used so many redundant widgets which do not serve any purpose except making the code look ugly and complex. I have refactored the code and have used the least number of widgets while fulfilling your requirement:
Demo on the action: https://dartpad.dev/?null_safety=true&id=75d503fcbe2bdb0e8b37ff21fc284a30
Gist link: https://gist.github.com/omishah/75d503fcbe2bdb0e8b37ff21fc284a30 ( Do give star if this works for you. :) )
I have made the image also responsive which you can remove if you don't want to have it.
Complete code:
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return Scaffold(
body: IntrinsicHeight(
child: Container(
margin: EdgeInsets.all(
8.0), // just to make the contianer stand out, you can remove it
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.yellow),
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.network(
'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png',
fit: BoxFit.cover,
height:
width * 0.25, // 25% of screen width
width: width * 0.25,
),
),
SizedBox(width: 15),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'product.name',
style: TextStyle(fontSize: 17),
),
Align(
alignment: Alignment.centerRight,
child: Wrap(direction: Axis.vertical, children: [
Text(
'Rs200',
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.bold),
),
Text(
'Rs300',
style: TextStyle(
decoration: TextDecoration.lineThrough,
fontSize: 17,
color: Colors.blueGrey),
),
]))
])),
],
),
)));
}
}
I have issue my screen showing orange line in end and show ing error of A Renderflex overflowed by 5105 pixels on the bottom .
My code
class Orderlist extends StatelessWidget {
final posts;
Orderlist({Key key, this.posts}) : super(key: key);
#override
Widget build(BuildContext context) {
double stackWidth = MediaQuery.of(context).size.width;
double stackHeight = MediaQuery.of(context).size.height;
return SingleChildScrollView(
child: Column(
children: [
test(),
test(),
test(),
test(),
test()
],
),
);
}
}
class test extends StatelessWidget {
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Container(
color: Color(0xfff6f6f6),
child: Padding(
padding: const EdgeInsets.all(14.0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
color: Color(0xfff8f8f8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(
'asdada',
style: TextStyle(
fontFamily: 'SFPROBOLD',
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.bold),
),
Text(
'asda'
.toString(),
style: TextStyle(
color: Colors.grey, fontSize: 14))
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: width * 0.13,
decoration: BoxDecoration(
color: Color(0xffef9500),
borderRadius: BorderRadius.all(
Radius.circular(5))),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Center(
child: Text('Pending',
style: TextStyle(
fontFamily: 'SFPROBOLD',
color: Colors.white,
fontSize: 9))),
),
),
),
],
),
),
),
Padding(
padding: const EdgeInsets.all(13.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Text(
'Delivery',
style: TextStyle(
fontFamily: 'SFPROBOLD',
color: Color(0xffea6c7b),
fontSize: 13),
),
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Text(
'asdada',
style: TextStyle(fontSize: 15),
),
),
IconButton(
icon: Icon(
Icons.arrow_forward,
color: Colors.grey,
),
onPressed: () {
// do something
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
OrderDetails()),
);
},
)
],
)
],
),
)
],
),
),
),
);
}
}
Try to wrap the first widget with SingleScrollview but its not working. Also try to wrap the Listviewbuilder but still showing pixels error and if i wrap SignleScrollview with expanded then its showing parent directory error
I think the error is because you wrapped the column in ListView as well as SingleChildScrollView. Try removing any one of them.
I am following along this tutorial and I ran into a problem. I have a main container and within the main container there's a column. Inside the column, there are various elements and at the end there's an extended container that creates an overlapping effect. However, the problem is that the extended container does not cover the remaining rest of the device height, and it leaves a space towards the end showing the background of the parent container. I'm new to flutter so I'm now learning the syntax, I've tried doubling the height of the expanded container but that did not work. How can I fix this?
import 'package:flutter/material.dart';
class LoginScreen extends StatefulWidget {
#override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.symmetric(vertical: 30.0),
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
colors: [
Colors.blue[900],
Colors.blue[500],
Colors.blue[400],
],
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 40.0),
Padding(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Eazy",
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
fontWeight: FontWeight.w400,
),
),
Text(
"Bank",
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
fontWeight: FontWeight.w400,
),
),
],
),
),
SizedBox(
height: 10.0,
),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(60),
topRight: Radius.circular(60),
),
),
),
),
],
),
),
);
}
}
The issue is with the padding: EdgeInsets.symmetric(vertical: 30.0), the padding takes a value of 30.0 logical pixels for both vertical directions(top and down).
You can fix that by using the padding: EdgeInsets.only(top: 30.0) which only gives padding to the top of the container.
I added a demo using your widget tree as an example:
import 'package:flutter/material.dart';
class LoginScreen extends StatefulWidget {
#override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.only(top: 30.0), // new line
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
colors: [
Colors.blue[900],
Colors.blue[500],
Colors.blue[400],
],
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 40.0),
Padding(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Eazy",
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
fontWeight: FontWeight.w400,
),
),
Text(
"Bank",
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
fontWeight: FontWeight.w400,
),
),
],
),
),
SizedBox(
height: 10.0,
),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(60),
topRight: Radius.circular(60),
),
),
),
),
],
),
),
);
}
}