How to navigate to different page for each _buildcard function? Below are my code. I want to navigate to different page using _buildcard function but still get error since it is function and not class. Is there any way to do?
_buildCard(3, "Azzahra\nVilla", 1400),
_buildCard(4, "Afsana\nVilla", 1300)
var rating = 0.0;
_buildCard(img, text, price)
{
return Card(
child: Row(children: <Widget>[
Container(
margin: EdgeInsets.all(8.0),
width: 120.0,
height: 180.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/$img.jpeg"), fit: BoxFit.cover),
borderRadius: BorderRadius.circular(10.0)),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"$text",
maxLines: 2,
textAlign: TextAlign.justify,
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 22.0),
),
SizedBox(height: 5.0),
Text(
"Janda Baik",
style:
TextStyle(fontSize: 11.0, color: Colors.grey),
),
SizedBox(height: 5.0),
SmoothStarRating(
rating: rating,
isReadOnly: false,
size: 14,
filledIconData: Icons.star,
halfFilledIconData: Icons.star_half,
defaultIconData: Icons.star_border,
color: Colors.yellow,
starCount: 5,
allowHalfRating: true,
spacing: 2.0,
onRated: (value) {
print("rating value -> $value");
// print("rating value dd -> ${value.truncate()}");
},
),
]),
Column(children: <Widget>[
Text(
"\RM$price",
style: TextStyle(
fontSize: 22.0, fontWeight: FontWeight.bold),
),
Text(
"per night",
style: TextStyle(fontSize: 11.0, color: Colors.grey),
)
Try this code
return InkWell(
onTap: () {
text=='Azzahra\nVilla'?
Navigator.push(context, MaterialPageRoute(builder: (context)=> AzzahraClassName())):
Navigator.push(context, MaterialPageRoute(builder: (context)=> AfsanaClassName()));
},
child: Card(child:....))
Related
Based on the code below, how does one wrap around a row of texts in a container? (as shown in the image). And also, how to make the container slightly rounded as shown in the image?
When I wrap it in a container, why does it takes up the full width of the app from left to right, which is not what I am trying to achieve.
Thank you in advance.
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
child: Expanded(
child: PageView(
controller: _pageController,
onPageChanged: (page) {
setState(() {
currentIndex = page;
});
},
children: [
SingleChildScrollView(
child: Column(
children: [
Container(
padding: EdgeInsets.only(
top: 0, left: 220, right: 30, bottom: 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Expanded(
flex: 7,
child: Text(
'187,177.33',
style: GoogleFonts.openSans(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.w500),
textAlign: TextAlign.right,
),
),
SizedBox(width: 10),
Expanded(
flex: 4,
child: Text(
'82',
style: GoogleFonts.openSans(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.w500),
textAlign: TextAlign.right,
),
),
],
),
),
],
),
),
],
),
),
),
],
),
);
}
Use decoration on Container. Also reduce the padding,
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(12)),
child: Row(
return Scaffold(
body: Column(
children: [
Container(
child: Expanded(
child: PageView(
// controller: _pageController,
onPageChanged: (page) {
setState(() {
// currentIndex = page;
});
},
children: [
SingleChildScrollView(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(12)),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Expanded(
flex: 7,
child: Text(
'187,177.33',
style: GoogleFonts.openSans(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.w500),
textAlign: TextAlign.right,
),
),
SizedBox(width: 10),
Expanded(
flex: 4,
child: Text(
'82',
style: GoogleFonts.openSans(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.w500),
textAlign: TextAlign.right,
),
),
],
),
),
),
],
),
),
],
),
),
),
],
),
);
}
More information about Container and layout
Container has property which called decoration, It would does the job for you, Try this:
Container(
margin: EdgeInsets.all(16),
padding:
EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8)),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'187,177.33',
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w500),
textAlign: TextAlign.right,
),
SizedBox(width: 10),
Text(
'82',
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w500),
textAlign: TextAlign.right,
),
],
),
)
i have a firestore collection named 'orders'. It has user details as field name 'uid'. The field 'uid' is a reference to user data present in 'users' collections. No i have to display username along with order details. So i need to get data from the reference field. i'm using Streambuilder to display data. Here is what i did:
Widget build(BuildContext context) {
Map UserSnapshot = Map();
return Scaffold(
body: StreamBuilder(
stream:_db.collection('users').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> UsersSnapshot) {
UsersSnapshot.data?.docs.forEach((element) {
UserSnapshot[element.id] = element;
});
return StreamBuilder(
stream:_db.collection('orders').where(
'driverId', isEqualTo: sp.getString('uid')).snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> OrdersSnapshot) {
if (!OrdersSnapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemCount: OrdersSnapshot.data!.docs.length,
itemBuilder:(context,index) {
var documentSnapshot= OrdersSnapshot.data!.docs[index].data() as Map<String,dynamic>;
return Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: Container(
width: MediaQuery.of(context).size.width,
height: 260,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.black38, width: 1)),
child: Column(
children: [
Row(
children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Container(
height: 60,
width: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
border: Border.all(color: Colors.black12)),
child: Image.network(
'https://indiaeducationdiary.in/wp-content/uploads/2020/10/IMG-20201024-WA0014.jpg')),
),
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Paradise Multicuisine Restaurant',
style: GoogleFonts.poppins(
fontWeight: FontWeight.bold, fontSize: 14),
overflow: TextOverflow.ellipsis,
),
Text(
'372,Kamarajar Salai, Karaikal',
style: GoogleFonts.poppins(
fontWeight: FontWeight.w400,
fontSize: 11,
color: hexStringToColor('636567')),
overflow: TextOverflow.ellipsis,
),
],
),
),
],
),
DottedLine(
dashColor: Colors.black26,
dashGapLength: 10,
),
Padding(
padding: EdgeInsets.all(20),
child: Container(
width: MediaQuery.of(context).size.height,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.account_circle),
SizedBox(
width: 7,
),
Text(
UserSnapshot[documentSnapshot['uid']['fullname']] ?? 'Deleted User',
style: GoogleFonts.poppins(
fontSize: 12,
fontWeight: FontWeight.w500,
color: hexStringToColor('636567')),
),
SizedBox(width: 3),
Text(
'#'+ documentSnapshot['orderId'],
style: GoogleFonts.poppins(
fontSize: 12,
fontWeight: FontWeight.w500,
color: hexStringToColor('636567')),
)
],
),
Container(
width: MediaQuery.of(context).size.width * 0.90,
child: Row(
children: [
Icon(Icons.location_on_rounded),
SizedBox(
width: 7,
),
Expanded(
child: Text(
'6/8, RR Colony, Ambedkar Street, Karaikal',
style: GoogleFonts.poppins(
fontSize: 12,
fontWeight: FontWeight.w500,
color: hexStringToColor('636567')),
overflow: TextOverflow.clip,
maxLines: 4,
),
),
],
),
),
SizedBox(height: 20,),
Row(
children: [
Container(
decoration: BoxDecoration(
color: hexStringToColor('aeaeae'),
borderRadius: BorderRadius.circular(5)
),
width: 80,
height: 20,
child: Row (
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('3 Items',
style: GoogleFonts.poppins(fontSize: 10, fontWeight: FontWeight.w500),
)
],
),
),
SizedBox(width: 20,),
Container(
decoration: BoxDecoration(
color: hexStringToColor('aeaeae'),
borderRadius: BorderRadius.circular(5)
),
width: 80,
height: 20,
child: Row (
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("₹"+documentSnapshot['grandTotal'].toString(),
style: GoogleFonts.poppins(fontSize: 10, fontWeight: FontWeight.w500),
)
],
),
),
],
),
Align(
child: ElevatedButton(onPressed: ()=>{},
child: Text('View Details'),
style: ElevatedButton.styleFrom(
elevation: 3,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5)
),
textStyle: GoogleFonts.poppins(fontWeight: FontWeight.w400,fontSize: 14)
),
),
alignment: Alignment.centerRight,
)
],
),
),
)
],
),
),
);
}
);
}
);
},
)
);
}}
You can iterate over the snapshots using asyncMap.
For example:
stream:_db.collection('users').snapshots().asyncMap((event) async {
event.docs.map((userDoc) {
var userData = userDoc.data() as Map<String, dynamic>;
var referenceSnapshot = userData["someReference"].get();
});
})
Dialog is not working properly with singlechildscrollview and soft keyboard in flutter.
Problems:
Dialog design is gone under the soft keyboard
Scroll is not properly working when open soft keyboard
My dialog is here:-
Future<void> ShowMessageDetail(
BuildContext context, String name, String message, String date, int id) async {
txtController.text = message;
edit = false;
return showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, void Function(void Function()) setState) {
return AlertDialog(
contentPadding: EdgeInsets.only(left: 25, right: 25),
content: Wrap(
children: [
Column(
children: [
Container(
height: 50,
width: double.infinity,
color: Colors.blue,
child: Center(
child: Text(
"Detail",
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20),
)),
),
Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 10, bottom: 10),
child: Row(
children: [
Text(
"Name : ",
style: TextStyle(
color: Colors.blue,
),
),
Text(name)
],
),
),
Divider(
color: Colors.grey,
),
Padding(
padding: const EdgeInsets.only(top: 10, bottom: 8),
child: Row(
children: [
Text(
"Date : ",
style: TextStyle(
color: Colors.blue,
),
),
Text(date.substring(0, 10))
],
),
),
Divider(
color: Colors.grey,
),
Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
"Message : ",
style: TextStyle(
color: Colors.blue,
),
),
),
Row(
children: [
Container(
decoration: BoxDecoration(
color: edit ? Colors.blue : Colors.white,
borderRadius: BorderRadius.circular(30),
border: Border.all(color: Colors.blue, width: 2),
),
child: IconButton(
onPressed: () {
setState(() {
edit ? edit = false : edit = true;
});
},
icon: Icon(
Icons.edit_outlined,
color: edit ? Colors.white : Colors.blue,
)),
),
Container(
width: 15,
),
Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(30),
),
child: IconButton(
onPressed: () {
copy(message);
},
icon: Icon(
Icons.copy,
color: Colors.white,
)),
)
],
),
],
),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: 300,
),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Padding(
padding: const EdgeInsets.only(top: 10),
child: Container(
child: TextFormField(
controller: txtController,
textAlign: TextAlign.justify,
focusNode: contentFocus,
decoration: InputDecoration.collapsed(
hintText: 'Start typing...',
hintStyle: TextStyle(
color: Colors.grey.shade400,
fontSize: 16,
fontWeight: FontWeight.w500,
),
border: InputBorder.none,
),
enabled: edit ? true : false,
maxLines: null,
keyboardType: TextInputType.multiline,
style: TextStyle(
fontSize: 15,
height: 1.2,
),
),
),
),
),
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Visibility(
visible: edit ? true : false,
child: MaterialButton(
onPressed: () {
updateMessage(name, txtController.text.toString(), id);
},
color: Colors.blue,
child: Text(
"Update",
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
Container(
width: 15,
),
MaterialButton(
onPressed: () {
Navigator.pop(context);
},
color: Colors.blue,
child: Text(
"Cancel",
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
),
],
)
],
),
],
),
],
),
);
},
);
});
}
I use scrollController for scrolling but this does not work.
Please help me to solve my dialog design problem.
wrap your Alert Dialog with SingleChildScrollview
SingleChildScrollView(
child: AlertDialog());
I have a ListView which consists of 3 Cards. Each Card has a Text and different fontSize. How can I write a single code for one Card and call it in the ListView for the remaining 2 Cards with different Text and fontSize.
This will help me save extra lines of Code. The code below
Widget homeBody = Container(
child: ListView(
children: <Widget>[
SizedBox(
height: 200.0,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
margin: EdgeInsets.all(8.0),
child: InkWell(
splashColor: Colors.blueGrey,
onTap: () {},
child: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'About',
style: TextStyle(
fontSize: 32.0, fontWeight: FontWeight.bold),
),
Text(
'Vishwapreneur',
style: TextStyle(
fontSize: 32.0, fontWeight: FontWeight.bold),
),
],
),
),
),
),
SizedBox(
height: 200.0,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
margin: EdgeInsets.all(8.0),
child: InkWell(
splashColor: Colors.blueGrey,
onTap: () {},
child: Row(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Smart Sociothon',
style: TextStyle(
fontSize: 32.0, fontWeight: FontWeight.bold),
),
// Text(
// 'Sociothon',
// style: TextStyle(
// fontSize: 32.0, fontWeight: FontWeight.bold),
// ),
],
),
),
),
),
SizedBox(
height: 200.0,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
margin: EdgeInsets.all(8.0),
child: InkWell(
splashColor: Colors.blueGrey,
onTap: () {},
child: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Entrepreneurship',
style: TextStyle(
fontSize: 30.0, fontWeight: FontWeight.bold),
),
Text(
'Development Cell',
style: TextStyle(
fontSize: 30.0, fontWeight: FontWeight.bold),
),
],
),
),
),
),
],
),
);
Thank you in Advance.
Pass your text along with textSize to your card.
The example below will give you an idea
ListView list(){
Map<String, double> items = new Map();
items["A"] = 14.0;
items["B"] = 24.0;
items["C"] = 32.0;
return new ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return myCard(items.keys.toList()[index], items.values.toList()[index]);
},
);
}
Card myCard(String text, double textSize){
return new Card(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(text, style: TextStyle(fontSize: textSize),),
),
);
}
I have added the below line to my sacffold
resizeToAvoidBottomPadding: false,
even though my banner ad overlaps the main screen.
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('1st Year Results'),
),
body: Center(
child: Container(
margin: new EdgeInsets.all(18.0),
child: Column(
children: <Widget>[
Image.asset('assets/MBAW.png'),
Container(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Flexible(
child: Container(
width: 150.0,
margin: EdgeInsets.all(15.0),
child: new TextField(
keyboardType: TextInputType.number,
decoration: const InputDecoration(
hintText: 'Enter a roll number',
helperText: 'Roll Number',
),
onChanged: (v) => setState(() {
_rolls = int.tryParse(v);
}),
),
)),
new RaisedButton(
key: null,
padding: EdgeInsets.all(10.0),
onPressed: buttonPressed,
color: Colors.black26,
child: new Text(
"Request",
style: new TextStyle(
fontSize: 18.0,
color: Colors.white,
fontWeight: FontWeight.bold,
),
)),
],
),
),
Expanded(
child: createList(_allNumbs),
),
Divider(),
(_allNumbs == null || _allNumbs.length <= 0)
? Container()
: SizedBox(
width: 400,
height: 50.0,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new InkWell(
child: new Text(
'Clear',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25,
color: Colors.blue),
),
onTap: () {
setState(() {
_allNumbs = null;
});
},
),
VerticalDivider(),
new InkWell(
child: Text(
"Share",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25,
color: Colors.blue),
),
onTap: () {
if (_allNumbs != null) {
final RenderBox box =
context.findRenderObject();
var shareMe = _createSharableText();
Share.share(shareMe,
sharePositionOrigin:
box.localToGlobal(Offset.zero) &
box.size);
}
}),
],
),
),
],
),
),
),
);
There's a lot of workaround for this. You might want to try this trick. You can also read some answers from this post.