How can i include navigation for a listview in flutter , - flutter

enter image description here
I want each list item to have its own route .I have attached a screenshot. My list view is customized, each item has its own name

Your question doesn't explain much, but rather than listview use listview.builder and then you can return any widget and pass your route based on itemindex.
ListView.builder(
itemCount: 10, *//Number of items in your static or dynamic*
itemBuilder: (context, index) {
return YourNewCommonWidgetForEveryItem(index);
},
),
For example I've to navigate the first item to somewhere else
Widget YourNewCommonWidgetForEveryItem(int Index){
return GestureDetector(
onTap:()=> index==0 ? locationtofirstItem : defaullt,
child: yourDesign(),
),
}
OfCourse the Above method won't work if you don't know where to navigate which item.

body: new ListView(
padding: const EdgeInsets.all(20.0),
children: List.generate(choices.length, (index) {
return Center(
child: ChoiceCard(
choice: choices[index],
item: choices[index],
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Detail(choice: choices[index])),
);},),);}))

Related

What widget should I use?

This is what I am doing now.
Main Page:
I would like to make it same like this picture.Example:
I have tried couple ways and widget to build it but couldn't figure it out. Also, I want to retrieve the data from the Firebase and show them as the content.
Code 1: https://pastebin.com/A0nK1riQ
Code 2: https://pastebin.com/i1T7gBNy
Widget build(BuildContext context) {
return Container(
child: StreamBuilder(
stream: _products.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
if (streamSnapshot.hasData) {
return ListView.builder(
itemCount: streamSnapshot.data!.docs.length,
itemBuilder: (context, index) {
final DocumentSnapshot documentSnapshot =
streamSnapshot.data!.docs[index];
return Container(
margin: const EdgeInsets.all(10),
child: ListTile(
title: Text(documentSnapshot['name']),
subtitle: Text(documentSnapshot['price'].toString()),
trailing: SizedBox(
width: 100,
),
),
);
});
}
return SizedBox.shrink();
}),
);
}
You may use GridView.builder
https://api.flutter.dev/flutter/widgets/GridView-class.html
and in gridview build use column
According to me, first you have to check which NavigationRail icon clicked then put the condition on it's GestureDetector like
// global variable
String itemClickedValue = "";
then set the value in it according to user click
itemClickedValue = "first";
then check the condition while fetching data like
if(itemClickedValue.compareTo("first") == 0){
// pass that documentId or api and then show in list
}

how to use two future builder in one page in flutter

In my Flutter app, I need to display two lists that are coming from the database, but I am having trouble getting both lists to display on the same screen. I am using two FutureBuilder widgets, but the first list is displaying correctly while the second list is still loading.
Here is the code I am using:
var future1 = FutureBuilder<List<QuranTextModel>>(
future: database.getQuranText(),
builder: (context, snapshot) {
if(snapshot.hasData){
return ScrollablePositionedList.builder(
itemScrollController: scrollToIndex,
itemCount: snapshot.data!.length,
initialScrollIndex: widget.position,
itemBuilder: (context, index) {
// Build the list item widget here
});
}else{
return const Center(child: CircularProgressIndicator(),);
}
}
);
var future2 = FutureBuilder<List<UrduTextModel>>(
future: database.getUrduTranlation(),
builder: (context, snapshot) {
if(snapshot.hasData){
return ScrollablePositionedList.builder(
itemScrollController: scrollToIndex,
itemCount: snapshot.data!.length,
initialScrollIndex: widget.position,
itemBuilder: (context, index) {
// Build the list item widget here
});
}else{
return const Center(child: CircularProgressIndicator(),);
}
}
);
Column(
children: [
SizedBox(
height: 200,
child: future1,
),
SizedBox(
height: 200,
child: future2,
),
],
)
The first FutureBuilder is used to build a list of QuranTextModel objects, and the second FutureBuilder is used to build a list of UrduTextModel objects. I am using a Column widget to display both lists, with each list contained within a SizedBox widget to give it a fixed height.
The issue I am having is that only the first list is displaying correctly, while the second list is still loading. How can I get both lists to display on the same screen?
Thank you for any help you can provide!
SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: 200,
child: future1),
SizedBox(height: 200,child: future2,)
],
),
),
Try this.
also you have to check your future status before populate you can check that by using
if (snap.connectionState == ConnectionState.done) { your code. you can check does snpa has data in it. }
connection state has deferent states that can help you to make your UI more interactive

showModalBottomSheet how can i navigate with data

When the item in the listview is clicked, I want to move the information to the bottom sheet, but I couldn't find the way.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => customBottomSheet(context),
),i tried but it didn't work how can i solve this problem
return Expanded(
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: coinListForDisplay.length,
itemBuilder: (context, index) {
return InkWell(
onTap: (() {
customBottomSheet(context);
}),
child: ListViewCard(
name: coinListForDisplay[index].name,
symbol: coinListForDisplay[index].symbol,
imageUrl: coinListForDisplay[index].imageUrl,
price:
coinListForDisplay[index].price.toDouble(),
change:
coinListForDisplay[index].change.toDouble(),
changePercentage: coinListForDisplay[index]
.changePercentage
.toDouble(),
),
);
}),
);```
You can pass data in parameters like this.
In you custom bottom sheet, use
CustomBottomSheet(BuildContext context,yourData){
//Your code
}
And in your onTap function use
CustomBottomSheet(context,yourData);

Flutter Getx multiple lists in body

Got the basic listview working with this code:
body: GetX<LokationListeCtrl>(
builder: (ctrl) => ListView.separated(
separatorBuilder: _separatorBuilder,
itemCount: ctrl.lokationListe.length,
itemBuilder: (BuildContext context, int index) {
return _lokationTile(ctrl.lokationListe.elementAt(index));
},
),
),
Ok great, so now I'll just add another list by wrapping the whole thing in a multiple child widget, but even before doing that, I'm already stuck trying to get the first list working:
body: Column(
children: [
GetX<LokationListeCtrl>(
builder: (ctrl) => ListView.separated(
separatorBuilder: _separatorBuilder,
itemCount: ctrl.lokationListe.length,
itemBuilder: (BuildContext context, int index) {
return _lokationTile(ctrl.lokationListe.elementAt(index));
},
),
),
],
),
Here using a Column, but it applies to all widgets with multiple children. The itemBuilder code is simply newer executed.
It seems like a simple and usefull thing to be able to do - the question is HOW to have multiple list in body, the "GetX way" ?
Found a solution. If we introduce a new Scaffold and wrap it in an Expanded, then GetX stops beeing an insulted little boy, and starts smiling again:
body: Column(
children: [
Expanded(
child: Scaffold(
body: MyBodyPadding(
// add space between appbar and first ListTile
child: GetX<LokationListeCtrl>(
builder: (ctrl) => ListView.separated(
separatorBuilder: _separatorBuilder,
itemCount: ctrl.lokationListe.length,
itemBuilder: (BuildContext context, int index) {
return _lokationTile(
ctrl.lokationListe.elementAt(index));
},
),
),
),
),
),
],
),

Append/Prepend items to a ListView

I'm using a ListView to populate a list of items, followed by a button:
ListView(
padding: EdgeInsets.all(10),
shrinkWrap: true,
children: <Widget>[
budgetEntry(snapshot.data[0]),
budgetEntry(snapshot.data[1]),
budgetEntry(snapshot.data[2]),
FlatButton(
child: Text('Manage budgets'),
onPressed: () { },
)
); // ListView
This works, but I want to know how I can achieve the same result with a dynamic number of items (snapshot.data.length) instead of manually using snapshot.data[0], snapshot.data[1], ...
I've tried using ListView.builder like so, but now I can't append a button at the end of the ListView:
ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return budgetEntry(snapshot.data[index]);
});