Flutter: How to get a value to use for reference - flutter

I'm just new to android programming. and now I have a problem in flutter / dart. So, I have a list of cards that has data showing in it, and that data is coming from the firestore database, and in the card there's also a button named "view". So I want to do is, If I click the "view" button it will show a dialog with the details of the card that I clicked. Im having a problem in getting the details and show it in a dialog. Please help :(
Here is my UI:
My UI
Here is my code when retrieving the data Im using a stream builder:
StreamBuilder<QuerySnapshot>(
stream: db.collection('HELP REQUEST').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Column(
children: snapshot.data.documents
.map((doc) => buildItem(doc))
.toList());
} else {
return SizedBox();
}
})
and Here I put the data in a card and output it:
return Card(
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Name of Requestor: ${doc.data['Name_ofUser']}',
style: TextStyle(fontSize: 20),
),
Text(
'Help description: ${doc.data['Help_Description']}',
style: TextStyle(fontSize: 20),
),
Text(
'Type of help needed: ${doc.data['Help_TypeNeeded']}',
style: TextStyle(fontSize: 20),
),
Text(
'Help location: ${doc.data['Help_Location']}',
style: TextStyle(fontSize: 20),
),
SizedBox(
height: 12,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {},
child: Text('View'),
)
],
),
],
),
),
);
Please help :(

Let's say you have a list of Card called list
you can use #mertcan answer to display a dialog like this.
return Card(
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Name of Requestor: ${doc.data['Name_ofUser']}',
style: TextStyle(fontSize: 20),
),
Text(
'Help description: ${doc.data['Help_Description']}',
style: TextStyle(fontSize: 20),
),
Text(
'Type of help needed: ${Text('Help description: $help')}',
style: TextStyle(fontSize: 20),
),
Text(
'Help location: ${doc.data['Help_Location']}',
style: TextStyle(fontSize: 20),
),
SizedBox(
height: 12,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {
//Note change here
_showMydialog(doc.data)
},
child: Text('View'),
)
],
),
],
),
),
)
void _showMydialog(dynamic data){
showDialog<void>(
context: context,
builder: (context) {
return SimpleDialog(
children: [
Text(Name of Requestor: ${data['Name_ofUser']),
Text('Help description: ${doc.data['Help_Description'])
],
);
},
);
}

There is a built-in function in flutter called showDialog and various dialog widgets that you can use according to your need(https://api.flutter.dev/flutter/material/Dialog-class.html). So it needs to look something like this
showDialog<void>(
context: context,
builder: (context) {
return SimpleDialog(
children: [
Text('Name of requester: $name'),
Text('Help description: $help')
],
);
},
);
Your View button click should call this function.

Related

How to get images and videos from firebase and show in flutter app?

I am developing a quiz app which is calling questions and options from firebase. It is currently only showing texts in questions and options from the firebase documents. I want it to get the images and videos from firebase and display it on my flutter application. Is it possible? If yes, please guide me through it. Thank you!
This is the code where I am showing the questions from..
class QuestionPage extends StatelessWidget {
final Question question;
const QuestionPage({super.key, required this.question});
#override
Widget build(BuildContext context) {
var state = Provider.of<QuizState>(context);
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Expanded(
child: Container(
padding: const EdgeInsets.all(16),
alignment: Alignment.center,
child: Text(question.text),
),
),
Container(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: question.options.map((opt) {
return Container(
height: 90,
margin: const EdgeInsets.only(bottom: 10),
color: Colors.black26,
child: InkWell(
onTap: () {
state.selected = opt;
_bottomSheet(context, opt, state);
},
child: Container(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Icon(
state.selected == opt
? FontAwesomeIcons.circleCheck
: FontAwesomeIcons.circle,
size: 30),
Expanded(
child: Container(
margin: const EdgeInsets.only(left: 16),
child: Text(
opt.value,
style: Theme.of(context).textTheme.bodyText2,
),
),
)
],
),
),
),
);
}).toList(),
),
)
],
);
}
/// Bottom sheet shown when Question is answered
_bottomSheet(BuildContext context, Option opt, QuizState state) {
bool correct = opt.correct;
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 250,
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(correct ? 'Good Job!' : 'Wrong'),
Text(
opt.detail,
style: const TextStyle(fontSize: 18, color: Colors.white54),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: correct ? Colors.green : Colors.red),
child: Text(
correct ? 'Onward!' : 'Try Again',
style: const TextStyle(
color: Colors.white,
letterSpacing: 1.5,
fontWeight: FontWeight.bold,
),
),
onPressed: () {
if (correct) {
state.nextPage();
}
Navigator.pop(context);
},
),
],
),
);
},
);
}
}
From the looks of things it seems you manually stored the images and videos ( Not programmatically). This means you have to create a collection (if you do not have one already) and input the url for each file you have stored in the database).
For example, If a question document from your Question collection has an image, a question text, and the list of options, you have to store the string url of the image in that particular question document.
To get the image string url, click and copy the link:
Then add it into your collection:
After doing this, you access your documents in your flutter app and then open the image/video using:
Extended Image (for image).
Better Player. (for video).

Flutter Getx arguments is always returning null

I am using Getx package in flutter to pass data to another page.
But I am getting null data from the page.
this is my code to get.to
Get.to(xreadArticlePage(),transition: Transition.rightToLeft, arguments: 'dataExample');
this my code to get data from previous page. data is my data variable. xreadArticlePage is my page to get data.
Text(data.toString()),
this is to get previous page data. it has a to string because see the data without error for now
class xreadArticlePage extends StatelessWidget {
#override
var data = Get.arguments;
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
InkWell(
onTap: () {
Get.back();
},
child: Icon(Icons.arrow_back_ios)),
Icon(Icons.abc_outlined),
],
),
),
Expanded(
child: SingleChildScrollView(
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
child: Container(
child: FutureBuilder<List<dynamic>>(
future: fetch1WpPosts(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Text(
snapshot.data![0]["title"],
style: TextStyle(
fontSize: 25, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Image.network(
snapshot.data![0]["featured_image_large"]),
SizedBox(
height: 5,
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
snapshot.data![0]["date"],
),
Text(data.toString()),
],
),
Html(data: snapshot.data![0]["content"]),
],
),
);
}
return CircularProgressIndicator();
}),
),
),
),
],
),
);
}
}
Use below code:
Get.toNamed(Routes.ADD_ADDRESS_SCREEN, arguments: {
'address': 'abc',
'city': 'xyz',
});

How to put space in a text loop

I want to give some width or space in "Your Likes" section between the users. How can I put some space, please give some suggestion and example.
children: [
UserImageSmall(
height: 64,
width: 64,
url: inactiveMatches[index].matchedUser.imageUrls),
Text(
inactiveMatches[index].matchedUser.name,
style: Theme.of(context).textTheme.bodyText1,
),
],
How can I remove the error showed in the "Your Chats" section here I want some help. Please let me know how can I do it. Please check the code and image below.
Text('Your Chats',
style: Theme.of(context).textTheme.headline5),
ListView.builder(
shrinkWrap: true,
itemCount: activeMatches.length,
itemBuilder: (context, index) {
return InkWell (
onTap: () {
Navigator.push(
context, MaterialPageRoute(
builder: (context) => ChatScreen(userMatch: userMatch,),
),
);
},
child: Row(children: [
UserImageSmall(
height: 60,
width: 60,
url: activeMatches[index].matchedUser.imageUrls,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
activeMatches[index].matchedUser.name,
style: Theme.of(context).textTheme.headline6,
),
SizedBox(height: 10),
Text(
activeMatches[index].chat![0].messages[0].message,
style: Theme.of(context).textTheme.bodyText1,
),
],
),
],
),
);
}
)
This is the exact emulator screen running with this given code.
In Your Likes section, give width to UserImageSmall Widget such as:
Container( width: 16, child: UserImageSmall())
Check index 0 exists or not, as in the image seems like null on index: 0
activeMatches[index].chat![0].messages[0].message
I have modified the code, hope this helps!
Text('Your Chats',
style: Theme.of(context).textTheme.headline5),
ListView.builder(
shrinkWrap: true,
itemCount: activeMatches.length,
itemBuilder: (context, index) {
return InkWell (
onTap: () {
Navigator.push(
context, MaterialPageRoute(
builder: (context) => ChatScreen(userMatch: userMatch,),
),
);
},
child: Row(children: [
UserImageSmall(
height: 60,
width: 60,
url: activeMatches[index].matchedUser.imageUrls,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
activeMatches[index].matchedUser.name,
style: Theme.of(context).textTheme.headline6,
),
SizedBox(height: 10),
Text(
activeMatches[index].chat![0]==null ? ""
: activeMatches[index].chat![0].messages[0].message,
style: Theme.of(context).textTheme.bodyText1,
),
],
),
],
),
);
}
)

How to make paginated page on listview like datatable flutter

I'm a new flutter user, here I'm learning how to create an attractive display. One of them is I want to add a paginated page to the listview, how do I add the paginated page? I have tried to make like using datatable but can't and error occurs. with the source code that I have I hope my friends can help me. thank you very much good friend
sample image i want
display the program that I have
Source Code :
Widget nasabahListView(List<Nasabah> listnasabah) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
Nasabah nasabah = listnasabah[index];
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
nasabah.nama_debitur,
style: Theme.of(context).textTheme.bodyText1,
),
Text(nasabah.alamat),
Text(nasabah.no_ktp),
Text(nasabah.no_telp),
Text(nasabah.no_selular),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {
apiService
.deleteNasabah(nasabah.id)
.whenComplete(() {
Alert(
context: context,
type: AlertType.success,
title: "Sukses",
desc: "Data berhasil dihapus",
buttons: [
DialogButton(
child: Text(
"OK",
style: TextStyle(
color: Colors.white, fontSize: 20),
),
onPressed: () {
Navigator.pop(context);
},
width: 120,
)
],
).show();
});
},
child: Text(
"Hapus",
style: TextStyle(color: Colors.red),
),
),
TextButton(
onPressed: () async {
var result = await Navigator.push(context,
MaterialPageRoute(builder: (context) {
return FormAddNasabah(nasabah: nasabah);
}));
},
child: Text(
'Edit',
style: TextStyle(color: Colors.blue),
),
),
],
)
],
),
),
),
);
},
itemCount: listnasabah.length,
),
);
}

Flutter Alertdialog overflow cutoff

I have a working Alertdialog but I get an overflow error and I can't get rid of it. I have tried flexible and expanded but maybe on the wrong levels.
builder: (BuildContext context) => AlertDialog(
title: const Center(child: Text('Completing task')),
content: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
UnconstrainedBox(
child: Flexible(
child: Column(
children: [
Text('Tag: ' + task.tag),
Text("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"),
const SizedBox(height: 10),
const Text('Waiting for NFC'),
const SizedBox(height: 20),
const Center(
child: CircularProgressIndicator(
color: Colors.grey,
),
),
],
),),
),
],
),
The issue here is the long text. If you would like all of the text to display without overflowing the layout, then Flexible is an option. Wrap the Text element with a Flexible when the displayed text could potentially be longer than the width of the view.
Notice that I had to remove some unnecessary elements from the view. Those elements were misplaced/misused for this scenario and were adding to the problem.
There are other ways to handle this, but I like how flexible (pun intended) this option is without having to calculate or guess width/height.
Here is a solution with the code cleaned up a little and using the Flexible on the long text element.
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Center(child: Text('Completing task')),
content: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Tag: P1348'),
Flexible(child: Text("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"),),
const SizedBox(height: 10),
const Text('Waiting for NFC'),
const SizedBox(height: 20),
const Center(
child: CircularProgressIndicator(
color: Colors.grey,
),
),
],
)
);
}
);
Container(
child: Text('text',
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
),
width: 50,
),
SizedBox(
width: YourOwnWidth,
child: Text("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr",style: TextStyle(
overflow: TextOverflow.clip
),),
);
an Easy solution that you can use