How should I do to add text inside a widget, so that in this way I can notify the user of my app that they can change their profile picture by pressing that camera button I want to insert the text "Change your profile picture here" into the widget _Foto?
my goal is that under the profile image there is a text so that the user knows that he can change his profile image by clicking on the button
Widget _Foto() {
return new GestureDetector(
onTap: () => imagePicker.showDialog(context),
child: new Center(
child: _image == null
? FutureBuilder<String>(
future: globals.SharedPreferencesHelper
.getDataAgentUserProfileImage(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return new Stack(children: [
Container(
margin: EdgeInsets.only(top: 48),
height: 0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.0),
),
),
Align(
alignment: Alignment.topCenter,
child: SizedBox(
child: CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 25.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: new NetworkImage(
systemProfilesUsersImagesRepository +
snapshot.data!),
),
),
)),
]);
} else if (snapshot.hasError) {
return Stack(
children: <Widget>[
new Center(
child: new CircleAvatar(
radius: 50.0,
backgroundColor: Colors.white,
),
),
new Center(
child: Container(
width: 100.0,
height: 100.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: new AssetImage(
"images/userpic.png")))),
),
],
);
} else {
return Container(
width: 100.0,
height: 100.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image:
new AssetImage("images/userpic.png"))));
}
})
: new Container(
height: 100.0,
width: 100.0,
child: new CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 23.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: FileImage(_image!),
),
),
)),
);
You can wrap the Center widget Column and include another Text child.
Widget _Foto() {
return GestureDetector(
onTap: () => imagePicker.showDialog(context),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Center(
child: _image == null
? FutureBuilder<String>(
future: globals.SharedPreferencesHelper
.getDataAgentUserProfileImage(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Stack(children: [
Container(
margin: EdgeInsets.only(top: 48),
height: 0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.0),
),
),
Align(
alignment: Alignment.topCenter,
child: SizedBox(
child: CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 25.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: NetworkImage(
systemProfilesUsersImagesRepository +
snapshot.data!),
),
),
)),
]);
} else if (snapshot.hasError) {
return Stack(
children: <Widget>[
Center(
child: CircleAvatar(
radius: 50.0,
backgroundColor: Colors.white,
),
),
Center(
child: Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage(
"images/userpic.png")))),
),
],
);
} else {
return Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image:
AssetImage("images/userpic.png"))));
}
})
: Container(
height: 100.0,
width: 100.0,
child: CircleAvatar(
radius: 40.0,
backgroundColor: Colors.white,
child: CircleAvatar(
child: Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 20.0,
child: Icon(
Icons.camera_alt,
size: 23.0,
color: Color(0xFF404040),
),
),
),
radius: 38.0,
backgroundImage: FileImage(_image!),
),
),
)),
Text("Change your profile picture here")
],
),
);
}
You can add a column and display it
CircleAvatar(
child: Column(
mainAxisSize: MainAxisSize.min,
children : [
Icon(Icons.camera),
Flexible(
child: Text("click here to change the profile pic")
),
]
)
)
Related
I want to add a different persons image as attached(The images inside circle avatar stacked over one another).How to achieve this.
I tried the following code but didn't get the result as expected
Padding(
padding: EdgeInsets.only(top: 7),
child: Stack(
children: <Widget>[
Positioned(
left: 0,
child: CircleAvatar(
radius: radius,
// backgroundColor: Colors.red,
backgroundImage: AssetImage(AssetImages.CHALLENGEPERSON1),
backgroundColor: Colors
.transparent, // Provide your custom image // Provide your custom image
),
),
Positioned(
left: 12,
child: CircleAvatar(
radius: radius,
// backgroundColor: Colors.red,
backgroundImage: AssetImage(
AssetImages.CHALLENGEPERSON1), // Provide your custom image
backgroundColor:
Colors.transparent, // Provide your custom image \
),
),
Positioned(
left: 28,
child: CircleAvatar(
radius: radius,
// backgroundColor: Colors.red,
backgroundImage: AssetImage(AssetImages.CHALLENGEPERSON1),
backgroundColor: Colors.transparent, // Provide your custom image
),
),
Positioned(
left: 35,
child: CircleAvatar(
radius: radius - 5,
// backgroundColor: Colors.red,
backgroundImage: AssetImage(AssetImages.ADDITIONAL),
backgroundColor:
Colors.transparent, // Provide your custom image
),
)
],
),
you can create this UI like this
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
const Home({super.key});
#override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
List imagesList = [//your images list];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (int i = 0; i < imagesList.length; i++)
Container(
margin: EdgeInsets.symmetric(vertical: 0),
child: Align(
widthFactor: 0.5,
child: CircleAvatar(
radius: 50,
backgroundColor: Colors.white,
child: CircleAvatar(
radius: 40,
backgroundImage: NetworkImage(
imagesList[i],
),
),
)),
)
],
),
SizedBox(
width: 20,
),
CircleAvatar(
backgroundColor: Colors.grey.shade200,
radius: 40,
child: Center(child: Icon(Icons.add)),
),
],
),
),
),
);
}
}
Try below code I am try your provided Image, change my images with your image
Container(
padding: const EdgeInsets.all(8.0),
height: 200.0,
width: 500.0,
child: Stack(
children: <Widget>[
Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
border: Border.all(),
image: DecorationImage(
image: NetworkImage(
'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png',
),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
),
Positioned(
left: 40.0,
child: Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
border: Border.all(),
image: DecorationImage(
image: NetworkImage(
'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png',
),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
),
),
Positioned(
left: 80.0,
child: Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
border: Border.all(),
image: DecorationImage(
image: NetworkImage(
'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png',
),
fit: BoxFit.fill,
),
shape: BoxShape.circle,
),
),
),
Positioned(
left: 120.0,
child: Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
border: Border.all(),
shape: BoxShape.circle,
),
child: Icon(Icons.add,size: 40,),
),
),
],
),
),
Result->
I want my image resize relatively from parent Containerwidget and defined scale: 0.8,//here in AssetImage() but it doesn't work ? How Can I fix this ?
Container(
margin: const EdgeInsets.only(top: 30),
alignment: Alignment.centerLeft,
child: Wrap(
children: [
GestureDetector(
onTap: () {
print("test");
},
child: Container(
width: 50,
height: 50,
// color: Colors.green,
decoration: const BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
image:
DecorationImage(
fit: BoxFit.fill,
image: AssetImage('images/prop/lock.png'),
scale: 0.8,//here
),
),
),
),
],
),
),
Additional comment
I think LayoutBuilder() doesn't work appropriately.
Container(
margin: const EdgeInsets.only(top: 30),
alignment: Alignment.centerLeft,
child: Wrap(
children: [
GestureDetector(
onTap: () {
print("test");
},
child: Container(
width: 50,
height: 50,
// color: Colors.green,
decoration: const BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
child: LayoutBuilder(
builder: (context, constraints) {
return Image.asset(
'images/prop/lock.png',
fit: BoxFit.none,
height: constraints.maxHeight * 0.1,
width: constraints.maxWidth * 0.1,
);
},
),
),
),
],
),
),
You can use LayoutBuilder like this:
Container(
margin: const EdgeInsets.only(top: 30),
alignment: Alignment.centerLeft,
child: Wrap(
children: [
GestureDetector(
onTap: () {
print("test");
},
child: Container(
width: 50,
height: 50,
alignment: Alignment.center,// important part
decoration: const BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
child: LayoutBuilder(
builder: (context, constraints) {
return Image.asset(
'images/prop/lock.png',
fit: BoxFit.contain,
height: constraints.maxHeight * 0.3,
width: constraints.maxWidth * 0.3,
);
},
),
),
),
],
),
)
You can Use AspectRatio Class for that use, like this
final double size = MediaQuery.of(context).size.aspectRatio;
You must change fit type to BoxFit.none, like this:
Container(
margin: const EdgeInsets.only(top: 30),
alignment: Alignment.centerLeft,
child: Wrap(
children: [
GestureDetector(
onTap: () {
print("test");
},
child: Container(
width: 100,
height: 100,
// color: Colors.green,
decoration: const BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.none, // change this line
image: AssetImage('images/prop/lock.png'),
scale: 0.8,
),
),
),
),
],
),
)
I am trying to use the below code to create a circular border around an image and align an icon on top of the circular border. What I am looking at is as the image below:
And my code is as below but it didn't work out perfectly:
Stack(
children: [
CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
child: Container(
padding: EdgeInsets.all(2),
child: CircleAvatar(
radius: 70,
backgroundImage: AssetImage('assets/person_icon.png'),
backgroundColor: Colors.white,
//
)),
),
Positioned(
bottom: 100,
right: 50,
child: InkWell(
onTap: () {},
child: Container(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Icon(Icons.add_a_photo, color: colorBlue),
),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.white,
),
borderRadius: BorderRadius.all(
Radius.circular(
50,
),
),
color: Colors.white,
boxShadow: [
BoxShadow(
offset: Offset(2, 4),
color: Colors.black.withOpacity(
0.3,
),
blurRadius: 3,
),
]),
),
)),
],
),
As you can above I am trying to use stack to lay each widget on top of each other but couldn't achieve that. I don't if anyone can help out where I missed it or give me a good idea of how to come about this.
Thanks in advance.
Default clipBehavior on Stack is hardEdge.
Use clipBehavior: Clip.none on Stack.
And to have circle shape
use customBorder: CircleBorder(), on InkWell.
use shape: BoxShape.circle instead of circular radius on container.
For better alignment use
Positioned(
top: -12,//half of icon size
left: 0,
right: 0,
Also better providing size on Stack like here.
/// fixing top widget size
SizedBox.square(
dimension: squareSize,
child: Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
clipBehavior: Clip.hardEdge,
decoration: const ShapeDecoration(
shape: CircleBorder(),
),
child: Image.asset(
'assets/images/image01.png',
fit: BoxFit.cover,
),
),
),
),
///background circle, you also do it on image widget
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(width: 5, color: Colors.green),
),
),
Positioned(
top: -12, // half of icon size
left: 0,
right: 0,
child: InkWell(
onTap: () {},
customBorder: const CircleBorder(),
child: Container(
width: 24 + 12, //icon size+padding
height: 24 + 12,
alignment: Alignment.center,
decoration: const ShapeDecoration(
shape: CircleBorder(),
color: Colors.green,
),
child: Icon(
Icons.add_a_photo,
color: Colors.white,
),
),
),
),
],
),
)
Play with sizes and decoration
Just add to the Stack Widget the Property clipBehavior: Clip.none,.
The clipBehavior will befine how to handle the Clip of the Stack Widget. Standard is hardEdge and cuts your icon off.
So your finished working Code is
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
clipBehavior: Clip.none,
children: [
CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
child: Container(
padding: EdgeInsets.all(2),
child: CircleAvatar(
radius: 70,
backgroundImage: AssetImage('assets/person_icon.png'),
backgroundColor: Colors.white,
//
),
),
),
Positioned(
bottom: 100,
right: 50,
child: InkWell(
onTap: () {},
child: Container(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Icon(Icons.add_a_photo, color: Colors.blue),
),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.white,
),
borderRadius: BorderRadius.all(
Radius.circular(
50,
),
),
color: Colors.white,
boxShadow: [
BoxShadow(
offset: Offset(2, 4),
color: Colors.black.withOpacity(
0.3,
),
blurRadius: 3,
),
],
),
),
),
),
],
),
),
);
}
}
void main() {
runApp(
const MaterialApp(
home: MyApp(),
),
);
}
Try below code
Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 25),
child: Container(
padding: EdgeInsets.all(8),
height: 270,
width: 270,
decoration: BoxDecoration(
color: Colors.green,
shape: BoxShape.circle,
),
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: ClipOval(
child: Container(
height: 250,
width: 250,
child: Image.network(
'https://miro.medium.com/max/1400/1*-6WdIcd88w3pfphHOYln3Q.png',
fit: BoxFit.cover,
),
),
),
),
),
),
Positioned(
top: 0,
left: .0,
right: .0,
child: Center(
child: CircleAvatar(
backgroundColor: Colors.green,
radius: 30.0,
child: Icon(
Icons.check,
color: Colors.white,
size: 40,
),
),
),
)
],
),
Result Screen->
user this below code to achieve
Center(
child: Stack(
children: [
Container(
height: 200,
child: CircleAvatar(
radius: 75,
backgroundColor: Colors.green,
child: CircleAvatar(
radius: 70,
backgroundColor: Colors.white,
child: Container(
padding: EdgeInsets.all(2),
child: const CircleAvatar(
radius: 60,
backgroundImage: NetworkImage("https://helostatus.com/wp-content/uploads/2021/09/pic-for-WhatsApp-HD.jpg"),
backgroundColor: Colors.white,
//
)),
),
),
),
Positioned(
left: 16,
right: 16,
top: 8,
child: InkWell(
onTap: () {},
child: const CircleAvatar(
backgroundColor: Colors.green,
radius: 16,
child: Icon(
Icons.check,
color: Colors.white,
),
)),
),
],
),
)[enter image description here][1]
Thanks all for the contribution but here is what works perfectly for what I'm looking at. I added clipBehavior and set Clip to noneclipBehavior: Clip.none, added ClipOval in a Container and set it margin and padding respectively to give the space between the green circular border and the profile image. I also, added another Container inside my ClipOval so as to add my image as a child to it. Find below my final code:
Center(
child: Stack(
clipBehavior: Clip.none,
children: [
CircleAvatar(
radius: 60,
backgroundColor: colorGreen,
child:Container(
padding: EdgeInsets.all(4),
margin: EdgeInsets.all(3),
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
child: ClipOval(
child: Container(
height: 250,
width: 250,
child: Image.network(
'my image link',
fit: BoxFit.cover,
),
),
)
)),
Positioned(
bottom: 100,
right: 45,
child: InkWell(
onTap: () {},
child: Center(
child: CircleAvatar(
backgroundColor: colorGreen,
radius: 15.0,
child: Icon(
Icons.check,
color: Colors.white,
size: 25,
),
),
),
),
),
],
),
),
I have like this code to build youtube player thumbnail with icon:
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://img.youtube.com/vi/_uOgXpEHNbc/0.jpg"),
fit: BoxFit.fitHeight),
),
),
Container(
color: Colors.black.withOpacity(0.7),
),
Container(
alignment: Alignment.center,
child: Center(
child: Icon(
Icons.smart_display,
color: Colors.red,
size: 100.0,
),
),
),
],
),
This is result of code:
How to make sure that the transparent background of the picture does not affect the central icon? I just need the icon to stay light.
If you want to keep white color of the Play icon in the center then you can try like this
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
"https://img.youtube.com/vi/_uOgXpEHNbc/0.jpg"),
fit: BoxFit.fill),
),
),
Container(
color: Colors.black.withOpacity(0.7),
),
Container(
alignment: Alignment.center,
child: Center(
child: Container(
height: 50,
width: 60,
color: Colors.white,
),
),
),
Container(
alignment: Alignment.center,
child: Center(
child: Icon(
Icons.smart_display,
color: Colors.red,
size: 100.0,
),
),
),
],
)
You can use Color(0xFF000000).withOpacity(0.7) inside your parent container so it will affect only your parent. You can try here
return Center(
child: Stack(
children: [
Center(
child: Container(
height: 200,
width: 200,
decoration: new BoxDecoration(
image: new DecorationImage(
image: new NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRVuX218dvGRwSyGhdr1KeTrN6wocBci6xWWQ&usqp=CAU'),
fit: BoxFit.cover,
),
),
child: Container(
color: Color(0xFF000000).withOpacity(0.7),
),
),
),
Center(
child: Container(
child: IconButton(
onPressed: () {},
icon: Image(
image: NetworkImage(
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTRtN8lufTjm-VwUnxm5uAwsW5dCrZqzFFWxg&usqp=CAU"),
),
iconSize: 80,
splashColor: Colors.red,
),
),
),
],
),
);
I use the worst possible images to showcase the example. Don't mind
Output
So im trying to build a page with lots of images in it, right now im using Stack widget to show the images,
this is my code:
import 'image_description.dart';
class Gallery extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.white10,
elevation: 0,
leading: Container(
padding: EdgeInsets.fromLTRB(20, 20, 0, 0),
child: InkWell(
child: Hero(
tag: 'back',
child: Image.asset(
'assets/images/wp_back_button_icon.png',
height: 250,
),
),
onTap: () {
Navigator.pop(context);
},
),
),
actions: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(0, 20, 20, 0),
child: Hero(
tag: 'logo',
child: Image.asset(
'assets/images/wp_logo.png',
height: 250,
),
),
),
],
),
body: SafeArea(
child: Column(
children: <Widget>[
Container(
child: Text(
'AR Gallery',
style: TextStyle(fontSize: 30),
),
),
Container(
child: Text("Let's educate in the fun way"),
),
Expanded(
child: SizedBox(
height: double.infinity,
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 3,
children: <Widget>[
InkWell(
child: Stack(
children: <Widget>[
Container(
height: 120,
width: 200,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image1',
child: Image.asset(
"assets/images/gallery/Image1.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImageDescription()));
},
),
Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image2',
child: Image.asset(
"assets/images/gallery/Image2.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image3',
child: Image.asset(
"assets/images/gallery/Image3.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image4',
child: Image.asset(
"assets/images/gallery/Image4.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image5',
child: Image.asset(
"assets/images/gallery/Image5.png",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image6',
child: Image.asset(
"assets/images/gallery/Image6.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image7',
child: Image.asset(
"assets/images/gallery/Image7.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image8',
child: Image.asset(
"assets/images/gallery/Image8.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text('Title',
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
],
),
),
),
],
),
),
);
}
}
As you can see, i'm using a lot of repeated Stack widget manually.
Can I make that automated? like creating a Stack widget or something similar once, and then call it where it should be with different Title.
Regards, Slim
It is indeed possible, this is what we call a reusable widget.
You can create it in the same file or in a separate one. I would suggest creating a widgets folder containing a dart file (whatever you want to name it) that'll contain your reusable widget.
1- If you only want the title to be different each time it should be something like this:
class SpecialStack extends StatelessWidget{
final String title;
SpecialStack({this.title});
#override
Widget build(BuildContext context){
return Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image2',
child: Image.asset(
"assets/images/gallery/Image2.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text(title,
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
}
}
2- If you want the image to be different whenever you use your reusable widget, it should be something like this. (I provided this case because I saw you were using different image for each Stack):
class SpecialStack extends StatelessWidget{
final String title, image;
SpecialStack({this.title, this.image});
#override
Widget build(BuildContext context){
return Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: image,
child: Image.asset(
"assets/images/gallery/$image.jpg",
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text(title,
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
}
}
For the first case (only title is different),
You can just replace wherever you're using the Stack by this:
SpecialStack(title: "Your desired title")
For the second case (image and title are different):
SpecialStack(title: "Your desired title", image: "your_image_name")
I hope this helps. I tried to be as clear as possible.
keep your images in a list then map the list. and inside map define a Stack widget. it will automatically create the stacks of your list of images.
You should write one function return widget type. Every time you want to show image you can call it.
Ex
Widget showImage(String url, String text){
return Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Hero(
tag: 'Image8',
child: Image.asset(
"url,
fit: BoxFit.fitHeight),
),
),
),
Container(
padding: const EdgeInsets.all(5),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Text(text,
style: TextStyle(
backgroundColor: Colors.deepOrange,
color: Colors.white)),
),
)
],
),
}