How to add interactive pins on images in flutter? - image

I have trouble integrating images with interactive pins on Flutter.is it even possible? if so, how can I do it? Im looking for something similar to WordPress's Image Mapper plugin!

The Stack widget is what you want:
Stack(
children: <Widget>[
<your image>,
Positioned(
left: 50.0,
top: 30.0,
child: <your pin>
),
...
],
)

Related

Add Image and back arrow on top of container with color gradient in flutter does not work

I am trying to add an image and a white back arrow on top of a container with background gradient color while of-course preserving the background. Here is the result that I want:
This is the code that I tried:
Stack(
children: <Widget>[
Container(
height: 200,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[Color(0xffb21fc3), Color(0xff960cd1)],
),
),
),
Image.asset(
'assets/vip_big.jpg',
colorBlendMode: BlendMode.overlay,
),
Positioned(
top: 45,
left: 15,
child: IconButton(
icon: Icon(
Icons.arrow_back_ios,
size: 45.0,
color: Colors.white,
),
onPressed: () {}),
),
],
),
Here is what I got:
What changes do I need to make to get the correct output?
Replace the white color from image to transparent. Right now, because the image is jpeg, there is no transparency. So the image overlaps your gradient container.

how to add rounded image in the center of the container

I am creating a profile page, i want top like this
here is my code
Widget build(BuildContext context) {
return Scaffold(
appBar: new MyAppBar(title: Text("My Profile")
),
drawer:drawer(),
body:
SingleChildScrollView(
child:Stack(children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: 150,
color: Colors.blue[500],
child: Align(
alignment:Alignment(-1.4,4.0),
child:Container(
//margin: EdgeInsets.all(20),
width: 400,
height: 125,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage('https://patientcaremedical.com/wp-content/uploads/2018/04/male-catheters.jpg'),
),
),
))),
])));
}
}
and here is its output
it is round from the bottom, can u please help where i am doing wrong.
You mean the bottom of your circle looks flat? That has nothing to do with your code. It's because the image you're using has white at the bottom. Take a look for yourself. If you change the backgroundColor: of your Scaffold to Colors.red or something other than white, you will see what I am talking about. All you need to do is use a different image that doesn't have white at the bottom, or crop your current one.

Child container of AlertDialog not covering width

I want to show a popup on a button tap. When I am trying to show AlertDialog with my custom design child Container of this AlertDialog not covering whole space. It has some padding from corners.
I have implemented in this way:
AlertDialog(
shape: OutlineInputBorder(
borderRadius: BorderRadius.circular(16.0)
),
content: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
color: Colors.redAccent,
width: screenSize.width,
height: screenSize.height*.90,
child: Text('test'),
)
],
),
),
)
For more info I am adding screenshot.
I found the solution for this. I resolve this issue by adding following tag in AlertDialog:
contentPadding: EdgeInsets.all(0.0),

Limit max width of Container in Flutter

Widget build(context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 300,
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: color ?? Colors.blue,
borderRadius: BorderRadius.circular(10)
),
child: msg
)
],
);
}
This is build method of my widget and It renders this UI depending on what I pass as msg paramter
Loading text
Some very long text
Now the issue I am facing is that I am not able to wrap the text inside this container/blue box without setting it's width but If I set width of container/blue box then it will always stay that wide no matter how short the text is.
Now is there a way to set maxWidth (like let's say 500) of container/blue box? So that the blue box will become as wide as required for small text like "Loading" and for long text it will expand till it reaches width of 500 units and then will wrap the text ?
Required output for long text:
For small text like loading I dont want any change in UI but for long text I want it to look like this.
You can add a constraint to the Container Widget with the preferred maxWidth like this:
Widget build(context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
constraints: BoxConstraints(minWidth: 100, maxWidth: 200),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: color ?? Colors.blue,
borderRadius: BorderRadius.circular(10)
),
child: msg
)
],
);
}
Use ConstrainedBox with BoxConstraints maxWidth, wrapped in a Flexible() widget. The Flexible widget allows for the box to resize for smaller screens as well.
Flexible(
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 150),
child: Container(
color : Colors.blue,
child: Text('Your Text here'),
),
),
),
This required to be inside Row or Column Widget
1. Row
Row(
children: [
Container(
constraints: BoxConstraints(maxWidth: 300),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0)),
child: Text("Long Text....")
),
],
);
2. Column
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
constraints: BoxConstraints(maxWidth: 300),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0)),
child: Text("Long Text....")
),
],
);
Here in both examples, it's working because Column and Row allow it's children to expand to it's given constraint/size.
Note: If a child wants a different size from its parent and the parent doesn’t have enough information to align it, then the child’s size might be ignored.
Here's a simple method I use for long texts:
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 200),
child: Container(
child: Text(
'Long string of text',
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
)
Note the maxLines attribute which might be helpful should you wish to show just a single line of text.
Use Constraints property in Container widget.
use maxWidth & minWidth value to achieve the requirement.
Container(
constraints: BoxConstraints(minWidth: 150, maxWidth: 300),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5)),
child: Text("")
)
This is how i fixed it
Center(
child: Container(
child: this.child,
width: 300,
height: double.infinity,
),
)
For me at least putting the Constrained box in IntrinsicWidth has solved issue.
IntrinsicWidth(
child: Container(
constraints: BoxConstraints(maxWidth: 200),
child: Row(
children: [
],
),
),
);
Maybe use a Flexible around the text and then wrap the Flexible in a fixed size containter? Just an idea.
I know it's late, but someone may get help in the future.
I used the auto_size_text plugin to get it done:
AutoSizeText(
'A really long String',
style: TextStyle(fontSize: 30),
minFontSize: 18,
maxLines: 4,
overflow: TextOverflow.ellipsis,
)
In my case my solution was this:
Wrapped in Row and add Spacer (in comment bubble widget I didn't define either width any max-width. it grows depends on the text.
to limit width added padding to comment bubble.
return Row(
children: [
CommentBubble(
chatText: snapshot.get('text'),
sentAt: snapshot.get('sentAt'),
sendBy: snapshot.get('sendBy'),
),
Spacer(),
],
);

How do I center text vertically and horizontally in Flutter?

I'd like to know how to center the contents of a Text widget vertically and horizontally in Flutter.
I only know how to center the widget itself using Center(child: Text("test")) but not the content itself. By default, it's aligned to the left. In Android, I believe the property of a TextView that achieves this is called gravity.
Example of what I want:
Text alignment center property setting only horizontal alignment.
I used below code to set text vertically and horizontally center.
Code:
child: Center(
child: Text(
"Hello World",
textAlign: TextAlign.center,
),
),
You can use TextAlign property of Text constructor.
Text("text", textAlign: TextAlign.center,)
I think a more flexible option would be to wrap the Text() with Align() like so:
Align(
alignment: Alignment.center, // Align however you like (i.e .centerRight, centerLeft)
child: Text("My Text"),
),
Using Center() seems to ignore TextAlign entirely on the Text widget. It will not align TextAlign.left or TextAlign.right if you try, it will remain in the center.
child: Align(
alignment: Alignment.center,
child: Text(
'Text here',
textAlign: TextAlign.center,
),
),
This produced the best result for me.
THE TEXT TITLE MUST BE ALWAYS ON TOP.
wrong way:
child: Center(
child: Text(
textAlign: TextAlign.center,
"Hello World",
),
),
right way:
child: Center(
child: Text(
"Hello World",
textAlign: TextAlign.center,
),
),
U need to use textAlign property on the Text widget. It produced the best results for me
Text(
'Hi there',
textAlign: TextAlign.center,
)
If you are a intellij IDE user, you can use shortcut key Alt+Enter and then choose Wrap with Center and then add textAlign: TextAlign.center
Put the Text in a Center:
Container(
height: 45,
color: Colors.black,
child: Center(
child: Text(
'test',
style: TextStyle(color: Colors.white),
),
),
);
Text element inside Center of SizedBox work much better way, below Sample code
Widget build(BuildContext context) {
return RawMaterialButton(
fillColor: Colors.green,
splashColor: Colors.greenAccent,
shape: new CircleBorder(),
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
width: 100.0,
height: 100.0,
child: Center(
child: Text(
widget.buttonText,
maxLines: 1,
style: TextStyle(color: Colors.white)
),
)
)]
),
),
onPressed: widget.onPressed
);
}
Enjoy coding 👨‍💻
Overview: I used the Flex widget to center text on my page using the MainAxisAlignment.center along the horizontal axis. I use the container padding to create a margin space around my text.
Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(20),
child:
Text("No Records found", style: NoRecordFoundStyle))
])
you have to set your Text widget property to set textAlign.center.
center(
child : Text(
'Hello',
textAlign: TextAlign.center))
Wrap your Text widget inside the Align widget and add Alignment.center.
Align(
alignment: Alignment.center,
child: Text(
'Some Text',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
)
maybe u want to provide the same width and height for 2 container
Container(
width: size.width * 0.30, height: size.height * 0.4,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6)
),
child: Center(
child: Text(categoryName, textAlign: TextAlign.center, style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17,
color: Colors.white,
),),
),

Resources