How to add a close icon to a container flutter. Appreciate your help on this.
showDialogFunc(context, img, cal, index, id, cid, status) {
final Size = MediaQuery.of(context).size;
return showDialog(
context: context,
builder: (context) {
return Center(
child: Material(
type: MaterialType.transparency,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
// color: backgroundBlue,
gradient: boxGradient,
),
padding: const EdgeInsets.only(top: 5, bottom: 5),
width: 350,
height: 350,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.network(
img,
height: 250,
width: 250,
fit: BoxFit.contain,
),
],
),
),
),
),
);
},
);
}
You could use the AlertDialog widget and put the close button as part of the title by using a Column. You can place the close button in the top right corner using the alignment property in a Container and setting the titlePadding to zero.
Here is a very simple example:
void _showDialog() {
showDialog(
context: context,
builder: (context) => AlertDialog(
titlePadding: EdgeInsets.zero,
title: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
alignment: FractionalOffset.topRight,
child: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(Icons.clear),
),
),
const Text('Title'),
],
),
content: const Text('Message here'),
),
);
}
More info in Flutter docs:
AlertDialog
Alignment using FractionalOffset
You should have a look at the Stack widget. It lets you put more widgets, one over the others.
For instance:
Stack(
children: [
Align(
alignment: Alignment.topRight,
child: IconButton(
child: Icon(Icons.close),
onPressed: () => Navigator.of(context).pop()
)
),
Material(
//your code
),
]
)
Something like that should work
Please check the updated code as below with the output. If you want to make cancel button inside, change margin: const EdgeInsets.all(25)
showDialogFunc(context, img, cal, index, id, cid, status) {
return showDialog(
context: context,
builder: (context) {
return Center(
child: Material(
type: MaterialType.transparency,
child: Stack(
alignment: Alignment.topRight,
children: [
Container(
margin: const EdgeInsets.all(25),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.red,
// gradient: boxGradient,
),
padding: const EdgeInsets.only(top: 5, bottom: 5),
width: 350,
height: 350,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.network(
img,
height: 250,
width: 250,
fit: BoxFit.contain,
),
],
),
),
),
InkWell(
onTap: (){
//perform action here.
},
child: Icon(
Icons.cancel,
color: Colors.white,
size: 50,
))
],
),
),
);
},
);
}
code snippet :
Stack(
children: [
//your code,
Positioned(
right: 10,
top: 10,
child: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Align(
alignment: Alignment.topRight,
child: CircleAvatar(
key: const Key('closeIconKey'),
radius: 15,
backgroundColor: Colors.white,
child: Icon(
Icons.close,
color: Colors.red,
),
),
),
),
),
],
),
Related
I am new in Flutter and trying to put SizedBox and container which had "validate" text EVENLY. But mainAxisAlignment after Column didn't work. There is still no space. Should i use sth else rather than mainAxis, could you please help me
body: SingleChildScrollView(
child: new Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
createPaymentPage(),
SizedBox(width:400,height:200, child: Image.network('https://www.vippng.com/png/detail/424-4249192_credit-card-design-and-collaterals-black-credit-card.png'),
),
new GestureDetector(
child: Container(
alignment: Alignment.center,
width: 150,
height: 40,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.amber,
),
child: Text(
' Validate',
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MapScreen()),
);
},
),
],
),
),
);
}
}
I use from Future Builder to get some of my needed data like images for my listview but the getimage functaion called multiple times, how coud i prevent it?
the getimage functaion is called from build i know its wrong but i must pass the carUserId to function and i cant pass it if i call get image
outside the build so what shoud i do?
i called getimage function in initState but it didn't solve my problem in this way the images are not appears at all
heare is my code
my getimage function
Future<String> getimage(carUserId) async {
var res = await carobj.getAdData(carUserId);
return (res.data() as Map)['imgPro'].toString();
}
I get image heare:
child: FutureBuilder<String>(
future: _imageFuture,
builder: (context, AsyncSnapshot<String> snapshot) {
if (snapshot.data == null) {
return Container(
width: 60,
height: 60,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage(
"https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg",
scale: 0.9),
fit: BoxFit.fill)),
);
} else {
String usersImg = snapshot.data.toString();
return Container(
width: 60,
height: 60,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage(
usersImg != null
? (usersImg)
: ("https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg"),
scale: 0.9),
fit: BoxFit.fill)),
);
}
}),
my list widget:
Widget build(BuildContext context) {
Widget? showCarsList() {
if (cars != null) {
return ListView.builder(
itemCount: cars!.docs.length,
padding: EdgeInsets.all(8),
itemBuilder: (context, index) {
carUserId = (cars!.docs[index].data() as Map)['uId'];
_mFuture = getNumber();
_imageFuture = getimage(carUserId);
return Card(
child: Column(
children: [
ListTile(
leading: GestureDetector(
onTap: () {},
child: FutureBuilder<String>(
future: _imageFuture,
builder: (context, AsyncSnapshot<String> snapshot) {
if (snapshot.data == null) {
return Container(
width: 60,
height: 60,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage(
"https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg",
scale: 0.9),
fit: BoxFit.fill)),
);
} else {
String usersImg = snapshot.data.toString();
return Container(
width: 60,
height: 60,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage(
usersImg != null
? (usersImg)
: ("https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg"),
scale: 0.9),
fit: BoxFit.fill)),
);
}
}),
),
title: GestureDetector(
onTap: () {},
child: Text((cars!.docs[index].data() as Map)['username']
.toString()),
),
subtitle: GestureDetector(
onTap: () {},
child: Row(
children: [
((cars!.docs[index].data() as Map)['carLocation'] !=
null)
? Text(
(cars!.docs[index].data()
as Map)['carLocation'],
style: TextStyle(
color: Colors.black.withOpacity(0.6)))
: Text("unknown",
style: TextStyle(
color: Colors.black.withOpacity(0.6))),
SizedBox(
width: 4.0,
),
Icon(
Icons.location_pin,
color: Colors.grey,
)
],
),
),
trailing: (cars!.docs[index].data() as Map)['uId'] == userId
? Row(
mainAxisSize: MainAxisSize.min,
children: [
GestureDetector(
onTap: () {},
child: Icon(Icons.edit),
),
SizedBox(
width: 20,
),
GestureDetector(
onDoubleTap: () {},
child: Icon(Icons.delete_forever),
),
],
)
: Row(
mainAxisSize: MainAxisSize.min,
children: [],
),
),
Padding(
padding: EdgeInsets.all(16),
child: Image.network((cars!.docs[index].data() as Map)['urlImage']!=null
? ((cars!.docs[index].data() as Map)['urlImage'].toString()): ("https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg"),
fit: BoxFit.fill,
),
),
Padding(
padding: EdgeInsets.only(left: 10),
child: Text('\$'+(cars!.docs[index].data() as Map)['carPrice'].toString()
,style:TextStyle(fontFamily: "oleo",letterSpacing: 2,
fontSize: 24) ,
),
),
Padding(padding: EdgeInsets.only(left: 15,right: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(Icons.directions_car),
Padding(padding: EdgeInsets.only(left: 10),
child: Align(
child: Text((cars!.docs[index].data() as Map)['carModel'].toString(),
),
alignment: Alignment.topLeft,
),
)
],
),
Row(
children: [
Icon(Icons.watch_later_outlined),
Padding(padding: EdgeInsets.only(left: 10),
child: Align(
child: Text(tAgo.format((cars!.docs[index].data() as Map)['time'].toDate()),
),
alignment: Alignment.topLeft,
),
),
],
),
],),
),
SizedBox(height: 10,),
Padding(padding: EdgeInsets.only(left: 15,right: 15,bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(Icons.brush_outlined),
Padding(padding: EdgeInsets.only(left: 10),
child: Align(
child: Text((cars!.docs[index].data() as Map)['carColor'].toString()),
alignment: Alignment.topLeft,
),
),
],
),
FutureBuilder( future: _mFuture,
builder: (context, AsyncSnapshot snapshot){
if (snapshot.hasData) {
String userNumbers = snapshot.data.toString();
return Row(
children: [
Icon(Icons.phone_android),
Padding(padding: EdgeInsets.only(left: 10),
child: Align(
child: Text(userNumbers.toString()),
alignment: Alignment.topLeft,
),
), ],
);
}else{
return Text("loading");
}
}
)
],
),
),
SizedBox(height: 10,),
Padding(padding: EdgeInsets.only(left: 15,right: 15),
child: Text((cars!.docs[index].data() as Map)['description'].toString(),
textAlign: TextAlign.justify,
style: TextStyle(
color: Colors.black.withOpacity(0.8)
),),
),
SizedBox(height: 10,)
],
),
);
},
);
}else{
return Text('Loading.....');
}
}
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.refresh, color: Colors.white),
onPressed: () {},
),
actions: [
TextButton(
onPressed: () {},
child: Padding(
padding: EdgeInsets.all(10),
child: Icon(
Icons.person,
color: Colors.white,
),
)),
TextButton(
onPressed: () {},
child: Padding(
padding: EdgeInsets.all(10),
child: Icon(
Icons.search,
color: Colors.white,
),
)),
TextButton(
onPressed: () {},
child: Padding(
padding: EdgeInsets.all(10),
child: Icon(
Icons.login_outlined,
color: Colors.white,
),
))
],
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blueAccent, Colors.redAccent])),
),
title: Text("home page"),
),
body: Center(
child: showCarsList(),
),
floatingActionButton: FloatingActionButton(
tooltip: 'add post',
child: Icon(Icons.add),
onPressed: () {
showDialogForAddingData();
},
),
);
}
I know the question is simple but I am in doubt about creating a simple button, which directs to the initial page of user registration.
I'm inserting the code below, there are no errors but when compiling nothing appears, I have already tested it by changing other elements and they update but the new button created does not appear on the screen.
New Button:
Container(
height: 30,
alignment: Alignment.center,
child: SizedBox.expand(
child: TextButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Cadastre-se", style: TextStyle(color: Colors.black))
],
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return RegisterName();
},
),
);
})));
Current Code:
final button = Container(
height: 50,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Color(0xFFF18A00),
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(8), bottomLeft: Radius.circular(8)),
),
child: SizedBox.expand(
child: TextButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Entrar", style: TextStyle(color: Color(0xFFFFFFFF)))
],
),
onPressed: _submitEmail)));
final forgotPassword = Container(
height: 40,
alignment: Alignment.center,
//child: FlatButton(
//style: TextStyle(color: Color(0xFF45CAD0))),
// onPressed: () => {},
);
return Scaffold(
backgroundColor: Color.fromRGBO(248, 248, 248, 1),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ListView(
//physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.only(left: 70, right: 70),
children: <Widget>[
logo,
SizedBox(height: 30),
email,
password,
button,
SizedBox(height: 20),
forgotPassword,
],
),
],
),
);
}
}
I tried to insert below the "Enter" button and nothing happened.
Where am I going wrong?
If I understand correctly, you want to figure out where to place the code for "New Button". In that case, assign your new button container to a variable like this:
final newButton = Container(
height: 30,
alignment: Alignment.center,
child: SizedBox.expand(
child: TextButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Cadastre-se", style: TextStyle(color: Colors.black))
],
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return RegisterName();
},
),
);
})));
Then, in your Current Code, you can put this variable inside the ListView like the other variables:
return Scaffold(
backgroundColor: Color.fromRGBO(248, 248, 248, 1),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ListView(
//physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.only(left: 70, right: 70),
children: <Widget>[
logo,
SizedBox(height: 30),
email,
password,
button,
newButton,
SizedBox(height: 20),
forgotPassword,
],
),
],
),
);
So your final "Current Code" will be:
final button = Container(
height: 50,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Color(0xFFF18A00),
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(8), bottomLeft: Radius.circular(8)),
),
child: SizedBox.expand(
child: TextButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Entrar", style: TextStyle(color: Color(0xFFFFFFFF)))
],
),
onPressed: _submitEmail)));
final newButton = Container(
height: 30,
alignment: Alignment.center,
child: SizedBox.expand(
child: TextButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Cadastre-se", style: TextStyle(color: Colors.black))
],
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return RegisterName();
},
),
);
})));
final forgotPassword = Container(
height: 40,
alignment: Alignment.center,
//child: FlatButton(
//style: TextStyle(color: Color(0xFF45CAD0))),
// onPressed: () => {},
);
return Scaffold(
backgroundColor: Color.fromRGBO(248, 248, 248, 1),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ListView(
//physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.only(left: 70, right: 70),
children: <Widget>[
logo,
SizedBox(height: 30),
email,
password,
button,
newButton,
SizedBox(height: 20),
forgotPassword,
],
),
],
),
);
replace the Column with Center(child:... and give your scaffold background static color
Flutter & AlertDialog : How do I align it to bottom? How I make 2 Alert Dialogs like this pictures?Please have a lot at this picture.
showDialog(
context: context,
builder: (BuildContext context) {
double width =
MediaQuery.of(context).size.width;
double height =
MediaQuery.of(context).size.height;
return AlertDialog(
backgroundColor: Colors.transparent,
contentPadding: EdgeInsets.zero,
title: Center(
child: Text("Evaluation our APP")),
content: Container(
// What Should I write here?
)
},
);
Here is one of the solutions:
showDialog(
context: context,
builder: (BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return AlertDialog(
backgroundColor: Colors.transparent,
contentPadding: EdgeInsets.zero,
elevation: 0.0,
// title: Center(child: Text("Evaluation our APP")),
content: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
const BorderRadius.all(Radius.circular(10.0))),
child: Column(
children: [
Text("a"),
Divider(),
Text("b"),
Divider(),
Text("c"),
],
),
),
SizedBox(
height: 10,
),
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
const BorderRadius.all(Radius.circular(10.0))),
child: Center(child: Text("d")),
)
],
));
},
);
here is my answer, pure widget without create AlertDialog:
final content = Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(Radius.circular(10.0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextButton(onPressed: () {}, child: Text("a"),),
Divider(height: 1,),
TextButton(onPressed: () {}, child: Text("b"),),
Divider(height: 1,),
TextButton(onPressed: () {}, child: Text("c"),),
],
),
),
SizedBox(
height: 10,
),
Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
const BorderRadius.all(Radius.circular(10.0)),
),
child: TextButton(onPressed: () {}, child: Text("d"),),
),
SizedBox(
height: 40,
),
],
);
showDialog(
context: context,
builder: (ctx) {
return FractionallySizedBox(
widthFactor: 0.9,
child: Material(
type: MaterialType.transparency,
child: content,
),
);
}
);
have to add Material because of text drawing error.
I did a Gridview.count for rendering 6 buttons in 2 columns and 3 rows, it's not rendering and instead of the components, it shows yellow and black stripes. I already set the height and the width, all wrapped in a container.
return Scaffold(
body: Container(
height: 450.0,
width: double.infinity,
child: GridView.count(
shrinkWrap: true,
primary: true,
crossAxisCount: 2,
children: <Widget>[
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Wifi')
]),
),
),
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Key/Access')
]),
),
),
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Key/Access')
]),
),
),
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Key/Access')
]),
),
),
],
)));
}
}
Please, someone knows why it's not rendering properly? or have an alternative widget that might work better? Thanks.
This widget is imported in this one:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF88B4E0),
appBar: AppBar(
leading: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: Icon(Icons.arrow_back_ios),
color: Colors.white,
),
backgroundColor: Colors.transparent,
elevation: 0.0,
title: Text('Help and FAQ',
style: TextStyle(fontSize: 18.0, color: Colors.white)),
centerTitle: true,
actions: <Widget>[
SearchButton(),
],
),
body: ListView(children: [
Stack(children: [
Container(
height: MediaQuery.of(context).size.height - 82.0,
width: MediaQuery.of(context).size.width,
color: Colors.transparent),
Positioned(
top: 75.0,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(45.0),
topRight: Radius.circular(45.0),
),
color: Colors.white),
height: MediaQuery.of(context).size.height - 100.0,
width: MediaQuery.of(context).size.width,
child: Column(
children: [
SizedBox(height: 20.0),
Testing(),
],
),
)),
]),
]),
);
}
void moveBack() {
Navigator.pop(context);
}
}