I want to get this message view, but I can't add clock and blue tick at the end. It should not look distorted when the message gets longer.
I tried such a code but the result was not the same.
Row sentMessage(BuildContext context, int index) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Container(
constraints: BoxConstraints(
maxWidth: displayWidth(context) * 0.80,
minWidth: displayWidth(context) * 0.20,
),
decoration: BoxDecoration(
color: Color(0xffDCF8C6),
borderRadius: BorderRadius.all(Radius.circular(9))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
child: Text(messages[index]),
),
Container(
child: Text('11:11'),
)
],
),
margin: EdgeInsets.symmetric(
horizontal: displayWidth(context) * 0.02,
vertical: displayWidth(context) * 0.004,
),
padding: EdgeInsets.all(displayWidth(context) * 0.02),
),
alignment: Alignment.centerRight,
width: displayWidth(context),
),
],
);
}
This is the result
I was able to do it myself after spending some time and thinking.
Row sentMessage(BuildContext context, int index) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Container(
constraints: BoxConstraints(
maxWidth: displayWidth(context) * 0.80,
minWidth: displayWidth(context) * 0.20,
),
decoration: BoxDecoration(
color: Color(0xffDCF8C6),
borderRadius: BorderRadius.all(Radius.circular(5))),
child: Wrap(
verticalDirection: VerticalDirection.down,
alignment: WrapAlignment.end,
children: [
Container(
margin: EdgeInsets.zero,
padding: EdgeInsets.only(
bottom: displayHeightOutAppBar(context) * 0.005),
constraints: BoxConstraints(
maxWidth: displayWidth(context) * 0.80,
),
child: Text(
messages[index],
style: TextStyle(
fontSize: displayWidth(context) * 0.04,
color: Colors.black,
),
),
),
Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.only(
top: displayHeightOutAppBar(context) * 0.006,
left: displayWidth(context) * 0.015),
//alignment: Alignment.bottomRight,
constraints: BoxConstraints(
maxWidth: displayWidth(context) * 0.20,
),
child: Text(
'11:11',
style: TextStyle(color: Colors.grey),
),
),
Container(
margin: EdgeInsets.only(
top: displayHeightOutAppBar(context) * 0.006),
child: Icon(
Icons.done_all,
size: displayWidth(context) * 0.04,
color: Colors.blue,
),
),
],
),
margin: EdgeInsets.symmetric(
horizontal: displayWidth(context) * 0.02,
vertical: displayWidth(context) * 0.004,
),
padding: EdgeInsets.only(
left: displayWidth(context) * 0.015,
right: displayWidth(context) * 0.015,
top: displayWidth(context) * 0.015),
),
alignment: Alignment.centerRight,
width: displayWidth(context),
),
],
);
}
I add an explanation with the recommendation of #gmdev
The wrong thing I did was to place the items one on the other using the "Column".Instead I placed them side by side using "Wrap".
I gave "MaxWidth" value to "Containers" that cover the items inside instead of the main "Container".I adjusted the spaces between them with "Padding" and "Margin".
Maybe there are different ways to do this but I think it's the simplest way.Those with different solutions can write
Here I froze again and added the second edit
There is a small triangle on the edge of the first message sent via Whatsapp.The only way to do this is "ClipPath".If your math is not good, you will have to work hard to do this.Couldn't find a similar example on the internet.
Row firstSentMessage(BuildContext context, int index) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: ClipPath(
clipper: MyCustomClipper1(),
child: Container(
constraints: BoxConstraints(
maxWidth: displayWidth(context) * 0.80,
minWidth: displayWidth(context) * 0.20,
),
decoration: BoxDecoration(
color: Color(0xffDCF8C6),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
bottomLeft: Radius.circular(5),
bottomRight: Radius.circular(5)),
),
child: Wrap(
verticalDirection: VerticalDirection.down,
alignment: WrapAlignment.end,
children: [
Container(
margin: EdgeInsets.zero,
padding: EdgeInsets.only(
bottom: displayHeightOutAppBar(context) * 0.005),
constraints: BoxConstraints(
maxWidth: displayWidth(context) * 0.80,
),
child: Text(
messages[index],
style: TextStyle(
fontSize: displayWidth(context) * 0.04,
color: Colors.black,
),
),
),
Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.only(
top: displayHeightOutAppBar(context) * 0.006,
left: displayWidth(context) * 0.015,
),
//alignment: Alignment.bottomRight,
constraints: BoxConstraints(
maxWidth: displayWidth(context) * 0.20,
),
child: Text(
'11:11',
style: TextStyle(color: Colors.grey),
),
),
Container(
margin: EdgeInsets.only(
top: displayHeightOutAppBar(context) * 0.006,
right: displayWidth(context) * 0.015,
),
child: Icon(
Icons.done_all,
size: displayWidth(context) * 0.04,
color: Colors.blue,
),
),
],
),
margin: EdgeInsets.symmetric(
horizontal: displayWidth(context) * 0.01,
vertical: displayWidth(context) * 0.004,
),
padding: EdgeInsets.only(
left: displayWidth(context) * 0.015,
right: displayWidth(context) * 0.015,
top: displayWidth(context) * 0.015),
),
),
alignment: Alignment.centerRight,
width: displayWidth(context),
margin: EdgeInsets.only(top: displayHeightOutAppBar(context) * 0.007),
),
],
);
}
And this is "Clipper".
class MyCustomClipper1 extends CustomClipper<Path> {
#override
Path getClip(Size size) {
Path path = Path();
path.lineTo(0, size.height);
path.lineTo(20, size.height);
path.arcToPoint(Offset(size.width - 12, 13),
radius: Radius.circular(1), clockwise: false);
path.lineTo(size.width, 0);
path.close();
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => true;
}
I added "ClipPath" and "clipper" for it in new codes.I slightly changed the "padding" and "margin" values to make the image correct.
My Code :
[import 'package:flutter/material.dart';
class OwnMessageCard extends StatelessWidget {
const OwnMessageCard({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.centerLeft,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width - 45,
),
child: Card(
elevation: 1,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)),
color: Color(0xffdcf8c6),
margin: EdgeInsets.symmetric(
horizontal: 15,
vertical: 5,
),
child: Stack(children: \[
Padding(
padding: const EdgeInsets.only(
left: 10,
right: 60,
top: 10,
bottom: 20,
),
child: Text(
"Hey, Joy Sinha",
style: TextStyle(
fontSize: 16,
),
),
),
Positioned(
bottom: 4,
right: 10,
child: Row(
children: \[
Text(
"20:58",
style: TextStyle(
fontSize: 13,
color: Colors.grey\[600\],
),
),
SizedBox(
width: 5,
),
Icon(
Icons.done_all,
size: 20,
),
\],
),
),
\]),
),
),
);
}
}][1]
Related
I have a widget meant to represent progress which goes
like this
I need to be able to put a name under a bubble, but when I do that like this:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.green,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
child: Center(
child: Icon(
Icons.check,
color: Colors.green,
size: 30,
),
),
),
Text("Test label"),
],
),
this happens.
I also tried putting the bars in a row with the circle:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
children: [
Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height / 20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: mainColor,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
child: Center(
child: Icon(
Icons.check,
color: mainColor,
size: 30,
),
),
),
Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height /20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
child: Container(
color: grey,
),
),
],
),
Text("Test label"),
],
),
This is better, but still creates problems if the text is bigger
How can I align the text below the circle without causing problems with the lines?
The code for the entire widget (sorry that it's so long, don't know how to show it otherwise):
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height / 20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.green,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
child: Center(
child: Icon(
Icons.check,
color: Colors.green,
size: 30,
),
),
),
Text("Test label"),
],
),Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height / 20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
child: Container(
color: Colors.grey,
),
),
Container(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height / 20,
),
height: 1 + MediaQuery.of(context).size.height / 10,
width: 3 / 2 * MediaQuery.of(context).size.width / 8,
child: Container(
color: Colors.grey,
),
),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.grey,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
),
Container(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height / 20,
),
height: 1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 8,
child: Container(
color: Colors.grey,
),
),
Container(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height / 20,
),
height: 1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 8,
child: Container(
color: Colors.grey,,
),
),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.grey,,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
)
],
),
One solution I would suggest is that try Wrapping Text with ConstrainedBox and Container and limit the max width.
For example -
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 50),
child: Container(
child: Text('Your Text'),
),
),
Thought that will move the text in the next line if it is too long to be placed in a single line.
I am building a flutter application where I am trying to add a container and it's child text.
I want the container to take as much height as the text requires. But instead it takes the height specified.
Following is my code:
class ChangePassword extends StatefulWidget {
#override
_ChangePasswordState createState() =>
_ChangePasswordState();
}
class _ChangePasswordState extends State<ChangePassword> {
final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
key: _scaffoldkey,
drawer: Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
Container(
height: (MediaQuery.of(context).size.height * 0.23),
child: DrawerHeader(
child: Container(
padding:
EdgeInsets.symmetric(horizontal: 10, vertical: 10),
child: Row(
children: [
Container(
height: MediaQuery.of(context).size.width * 0.1,
width: MediaQuery.of(context).size.width * 0.1,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Theme.of(context).primaryColor,
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(imageurl)),
boxShadow: [
BoxShadow(
blurRadius: 25,
color: Colors.black.withOpacity(0.2),
offset: Offset(0, 10),
)
],
),
),
Container(
height: MediaQuery.of(context).size.width * 0.15,
width: MediaQuery.of(context).size.width * 0.5,
padding: EdgeInsets.symmetric(horizontal: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: TextStyle(
fontSize: 15,
color: Colors.lightBlue[900],
fontWeight: FontWeight.bold),
),
Text(email),
SizedBox(
height: 2,
),
Row(
children: [
Icon(
Icons.remove_red_eye_outlined,
size: 12,
),
Text(
'View Profile',
style: TextStyle(
fontSize: 12,
),
)
],
)
],
),
),
],
)),
decoration: BoxDecoration(
color: Colors.white,
),
),
),
],
),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.35,
child: Stack(children: [
Positioned(
height: MediaQuery.of(context).size.height * 0.23,
top: 0,
child: Padding(
padding: EdgeInsets.only(top: 50),
child: Container(
color: Color(0xff002060),
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.1,
child: Align(
alignment: Alignment.topCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
_scaffoldkey.currentState.openDrawer();
},
child: new Container(
child: Icon(
Icons.menu,
color: Colors.white,
size: MediaQuery.of(context).size.height *
0.04,
),
padding: new EdgeInsets.all(7.0),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
),
Container(
height:
MediaQuery.of(context).size.height * 0.07,
child: Center(
child: Text(
'Profile',
style: TextStyle(
color: Colors.white,
),
),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
),
Icon(
Icons.search,
color: Colors.white,
size: MediaQuery.of(context).size.height * 0.04,
),
],
),
),
),
),
),
Positioned(
top: MediaQuery.of(context).size.height * 0.16,
left: MediaQuery.of(context).size.width * 0.03,
child: Row(
children: [
Container(
width: MediaQuery.of(context).size.height * 0.14,
height: MediaQuery.of(context).size.height * 0.14,
decoration: ShapeDecoration(
shape: CircleBorder(), color: Colors.white),
child: Padding(
padding: EdgeInsets.all(8.0),
child: DecoratedBox(
decoration: ShapeDecoration(
shape: CircleBorder(),
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://thumbor.forbes.com/thumbor/fit-in/416x416/filters%3Aformat%28jpg%29/https%3A%2F%2Fspecials-images.forbesimg.com%2Fimageserve%2F5f4ebe0c87612dab4f12a597%2F0x0.jpg%3Fbackground%3D000000%26cropX1%3D292%26cropX2%3D3684%26cropY1%3D592%26cropY2%3D3987',
))),
),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.05,
),
Column(
children: [
Container(
height: MediaQuery.of(context).size.height * 0.08,
child: Text(
'Bill Gates',
style: TextStyle(
fontSize: 25,
color: Colors.white,
fontWeight: FontWeight.bold,
fontFamily: 'Calibri'),
),
),
],
)
],
),
),
Positioned(
top: MediaQuery.of(context).size.height * 0.24,
left: MediaQuery.of(context).size.height * 0.165,
child: FittedBox(
fit: BoxFit.fill,
child: Container(
height: MediaQuery.of(context).size.height * 0.09,
width: MediaQuery.of(context).size.width * 0.6,
child: Text(
'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
style: TextStyle(
color: Colors.black, fontFamily: 'Calibri'),
),
),
),
),
]),
),
],
)),
],
),
));
}
Following is the image:
I want the container that contains the text below 'Bill Gates' to be wrapped according to the information entered there.
The whole container should decrease as I am planning to add widgets below this customised appbar, so is there any way I can achieve this?
I tried to use flexible but it did not work. Can someone help me with this please?
Use fitted box:-
FittedBox(
fit:BoxFit.fitHeight,
child:Text("Your text"),
),
Do not provide the height of the container and it will adjust automatically.
#Ankit,
Culprit:
As you mentioned, the Container widget is intended to take the width and height you explicitly specify, so it does what you tell it to do.
What you should have done in the above case is avoid specifying the height for that Container which contains the Text widget as follows.
Change this code section from:
Positioned(
top: MediaQuery.of(context).size.height * 0.24,
left: MediaQuery.of(context).size.height * 0.165,
child: FittedBox(
fit: BoxFit.fill,
child: Container(
height: MediaQuery.of(context).size.height * 0.09,
width: MediaQuery.of(context).size.width * 0.6,
child: Text(
'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
style: TextStyle(
color: Colors.black, fontFamily: 'Calibri'),
),
),
),
),
To the following code, where I have removed specifying the height property of the container:
Positioned(
top: MediaQuery.of(context).size.height * 0.24,
left: MediaQuery.of(context).size.height * 0.165,
child: FittedBox(
fit: BoxFit.height,
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
child: Text(
'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
style: TextStyle(
color: Colors.black, fontFamily: 'Calibri'),
),
),
),
),
By doing that, and wrapping the Container within a FittedBox, that varying text length Text widget will cause it to adjust its height accordingly.
Following part is important here:
child: FittedBox(
fit: BoxFit.height,
in this widget there is call to setState but it gives me error because this is a global widget where i am just passing the required data i also want to pass the state so it can access the setState method how can i achieve that ??
Thanks in Advance....
Widget postWidget(Map<dynamic,dynamic> homeMap,index,context){
return Padding(
padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.02),
child: Card(
elevation: 4.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Stack(children: <Widget>[
Column(crossAxisAlignment: CrossAxisAlignment.start, children: <
Widget>[
SizedBox(
height: 1,
),
commonProfileData(homeMap, index),
SizedBox(
height: height * 0.005,
),
homeMap['homeModelDataList'][index].description != ""
? Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).size.height * 0.008,
left: MediaQuery.of(context).size.height * 0.025,
right: MediaQuery.of(context).size.height * 0.025),
child: Text(
homeMap['homeModelDataList'][index].description,
textAlign: TextAlign.justify,
),
)
: SizedBox(
height: 0,
),
SizedBox(
height: height * 0.005,
),
homeMap['homeModelDataList'][index].photo != ""
? Container(
margin: EdgeInsets.only(
right: MediaQuery.of(context).size.height * 0.02,
left: MediaQuery.of(context).size.height * 0.02),
child: Container(
clipBehavior: Clip.antiAlias,
height: MediaQuery.of(context).size.height * 0.25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.red,
),
child: Image.network(
homeMap['homeModelDataList'][index].photo,
fit: BoxFit.fill),
),
)
: SizedBox(
height: 0,
),
//////////////////////////// ICON BUTTON //////////////////////////////////
commonButtons(homeMap, index),
openCommentBox &&
postId ==
homeMap['homeModelDataList'][index].postId.toString()
? Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(children: <Widget>[
Container(
margin: EdgeInsets.only(
left:
MediaQuery.of(context).size.height * 0.015,
),
height: MediaQuery.of(context).size.height * 0.05,
width: MediaQuery.of(context).size.height * 0.25,
child: TextFormField(
minLines: 1,
maxLines: 1,
controller: commentDataController,
focusNode: commentDescriptionFocus,
onFieldSubmitted: (value) {
commentDescriptionFocus.unfocus();
},
decoration: InputDecoration(
hintText: "Add Comment",
hintStyle: TextStyle(
color: isCommentDescriptionEmpty
? Colors.red
: Colors.grey),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(
MediaQuery.of(context).size.width *
0.03)),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.grey),
borderRadius: BorderRadius.circular(
MediaQuery.of(context).size.width *
0.03))),
),
),
Container(
clipBehavior: Clip.antiAlias,
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.width * 0.1,
margin: EdgeInsets.only(
right:
MediaQuery.of(context).size.height * 0.01,
left: MediaQuery.of(context).size.height *
0.01),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10)),
child: RaisedButton(
child: Text("Add Comment",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12.2)),
onPressed: () async {
if (commentDataController.text
.trim()
.length ==
0) {
isCommentDescriptionEmpty = true;
print("i am here");
} else {
isCommentDescriptionEmpty = false;
//progressDialog.show();
await sendCommentDataToServer(
commentDataController.text);
//progressDialog.hide();
(commentInfoMap["status"] == "Success")
? toast.showToastMessage(
message:
"Comment posted Successfully !")
: toast.showToastMessage(
message: "Comment post Failed !");
}
}),
)
]),
InkWell(
child: Padding(
padding: EdgeInsets.only(
top:
MediaQuery.of(context).size.height * 0.01,
left: MediaQuery.of(context).size.height *
0.018,
bottom: MediaQuery.of(context).size.height *
0.015,
),
child: RichText(
text: TextSpan(
style: TextStyle(
fontSize: 13, color: Colors.black),
children: [
TextSpan(
text: showPostCommentsBox
? "Hide "
: "Show ",
style: TextStyle(
color: Colors.blueAccent,
),
),
TextSpan(
text: "Comments",
style: TextStyle(color: Colors.black),
),
])),
),
onTap: () {
setState(() {
showPostCommentsBox = !showPostCommentsBox;
});
print(
"button pressed state : $showPostCommentsBox");
}),
],
),
)
: SizedBox(
height: 0,
),
showPostCommentsBox &&
postId ==
homeMap['homeModelDataList'][index].postId.toString()
? Container(
// color: Colors.red,
margin: EdgeInsets.only(
right: MediaQuery.of(context).size.height * 0.01,
left: MediaQuery.of(context).size.height * 0.01),
height: height * 0.15,
child: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: listOfComment.length,
itemBuilder:
(BuildContext context, int position) {
return Padding(
padding: EdgeInsets.only(
right: height * 0.01,
left: height * 0.003,
),
child: Card(
color: Colors.grey[100],
child: ListTile(
title: Text(listOfComment[position][4]
.toString()),
trailing: Text("temp")),
),
);
}),
)
],
),
)
: Container(),
]
)
])),
);
}
in this widget there is call to setState but it gives me error because this is a global widget where i am just passing the required data i also want to pass the state so it can access the setState method how can i achieve that ??
Thanks in Advance....
okay for passing state you have to pass this to the global widget.
For example:
Calling from stateful class:
userDrawer(this);
This is global class:
userDrawer(var state){
return Container(
child: RaisedButton(
child: Text("Click"),
onPressed:(){
state.setState(() {
value = newValue;
}) }
)
pass a function which executes when the button is pressed
userDrawer((){setState(){});
in the global class
userDrawer(Function onPressed){
return FlatButton(onPressed: onPressed, child: Text('Click'));
}
or if you want set a value
userDrawer(Function onPressed){
return FlatButton(onPressed: (){
value = newValue;
onPressed();
}, child: Text('Click'));
}
I am using Stack widget in which I have 2 widgets one is just an image and the other is Singlechildscrollview.
I need to add a button on the footer of the screen and fix this (not moveable). Need to be fixed on screen when the Singlechildscrollview scroll or not I need to fix this button. I am not sure how can I do this because if I out the button in Singlechildscrollview then it will show only when I scroll. I need to fix when I scroll or not button should appear.
Here is my code:
Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(top: statusBarHeight * 0.8),
height: height * 0.4,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/place2.jpg',
),
fit: BoxFit.fill,
),
),
),
SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(top: height * 0.3),
child: SingleChildScrollView(
child: ClipRRect(
borderRadius: BorderRadius.only(
topRight: Radius.circular(30),
topLeft: Radius.circular(30)),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: height * 0.05,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'NYC Food Festival',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 30),
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.calendar_today,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'Sat, May 25, 2020',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.attach_money,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'25,000 PKR',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Snaps',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
child: Column(
children: <Widget>[
CarouselSlider(
options: CarouselOptions(
autoPlay: true,
aspectRatio: 2.0,
enlargeCenterPage: true,
),
items: imageSliders,
),
],
)),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'About',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
padding: EdgeInsets.only(
right: width * 0.03, left: width * 0.03),
child: DescriptionTextWidget(
text:
"Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.")
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Included',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
],
),
),
),
),
),
)
],
)
Here is the output of current view:
This is what I want:
You can achieve this look with relative ease.
Here is an example:
class BottomFadeButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
title: Text("Bottom Fade Button"),
),
body: Container(
// height: 500,
color: Colors.amberAccent,
child: Stack(
children: <Widget>[
Container(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text("Hello "),
);
}),
),
// Bottom Button
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
height: 100,
decoration: BoxDecoration(
// color: Color.fromARGB(110, 255, 255, 255),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
gradient: LinearGradient(
colors: [
Color.fromARGB(30, 255, 255, 255),
Color.fromARGB(255, 255, 255, 255),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
child: Center(
child: InkWell(
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 100,
vertical: 20,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.blueAccent,
),
child: Text(
"BUY TICKETS",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white),
),
),
onTap: () {
return print("Tap");
},
),
),
),
),
],
),
),
),
);
}
}
Output:
yes you can add the button in stack and then wrap with align widget with alignment.bottom_center
I am using the stack widget to show the back arrow button on an image. And its showing but the problem is it's not tappable mean Gesturededector is not working on Stack.
My code
Stack(
children: <Widget>[
Container(
height: height * 0.4,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/place2.jpg',
),
fit: BoxFit.fill,
),
),
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () => print("first container"),
child: Align(
alignment: Alignment.topLeft,
child: Container(
margin: EdgeInsets.only(left: width * 0.03),
padding: EdgeInsets.only(top: statusBarHeight * 2),
child: Icon(
Icons.arrow_back_ios,
size: 25,
color: Colors.white,
),
),
),
),
SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(top: height * 0.3),
child: SingleChildScrollView(
child: ClipRRect(
borderRadius: BorderRadius.only(
topRight: Radius.circular(30),
topLeft: Radius.circular(30)),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: height * 0.05,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'NYC Food Festival',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 30),
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.calendar_today,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'Sat, May 25, 2020',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.attach_money,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'25,000 PKR',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Snaps',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
child: Column(
children: <Widget>[
CarouselSlider(
options: CarouselOptions(
autoPlay: true,
aspectRatio: 2.0,
enlargeCenterPage: true,
),
items: imageSliders,
),
],
)),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'About',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
padding: EdgeInsets.only(
right: width * 0.03, left: width * 0.03),
child: DescriptionTextWidget(
text:
"Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.")
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Included',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
],
),
),
),
),
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
height: 100,
decoration: BoxDecoration(
// color: Color.fromARGB(110, 255, 255, 255),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
gradient: LinearGradient(
colors: [
Color.fromARGB(30, 255, 255, 255),
Color.fromARGB(255, 255, 255, 255),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
child: Center(
child: InkWell(
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 80,
vertical: 20,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.blueAccent,
),
child: Text(
"BOOK NOW",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white),
),
),
onTap: () {
return print("Tap");
},
),
),
),
),
],
)
I need to use it as a back button when I navigate but it's not working then for testing I just print a value onTap but its also not working I try to add behaviour also.
Can you try putting GestureDectector after SingleChildScrollView in a stack.
I guess SingleChildScrollView is lying above GestureDetector since it is a stack.
SingleChildScrollView(),
GestureDetector(),