I am trying to display a list using a list title but would like to add a text with a click event to the top of the list.
How could I do it?
I would be very grateful for the help.
I am trying to display a list using a list title but would like to add a text with a click event to the top of the list.
How could I do it?
I would be very grateful for the help.
Code and image::
Widget StatefulBuilderSuggestions(BuildContext context ,List <SearchDelegateModel> historialMenuPrincipal){
return Container(
child:StatefulBuilder(
builder:(context,setState)
{
return Container(
child: ListView.builder(
itemCount: historialMenuPrincipal.length,
itemBuilder: (context,i)
{
contentPadding: EdgeInsets.symmetric(vertical: 12,horizontal: 16);
return
ListTile(
subtitle: Text(historialMenuPrincipal[i] == null ? "no data" :historialMenuPrincipal[i].email,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20,
fontWeight: FontWeight.bold,
),
),
title: Text(historialMenuPrincipal[i] == null ? "no data" :historialMenuPrincipal[i].contrasena,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20,
fontWeight: FontWeight.bold,
),
),
trailing: historialMenuPrincipal[i] != null || historialMenuPrincipal.isEmpty
? IconButton(
icon: Icon(Icons.cancel,color: Colors.black,),
onPressed: () {
setState(() {
historialMenuPrincipal.remove(historialMenuPrincipal[i]);
});
},): null
);
}
),
);
}
)
);
}
use a column with 2 parts, title as a textbutton and above the list
Container(
child: Column(
children: [
TextButton(
onPressed: () {
hideList = !hideList;
},
child: Text("Title"),
),
hideList ? ListView() : Container(),
],
))
something like this
In order to add a Text widget above a list, both widgets should be inside a column and important thing to note is that ListView widget must be inside a Expanded widget and for the text widget to be clickable, use GestureDetector.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
padding: const EdgeInsets.all(16),
child: Column(
children: [
GestureDetector(
onTap: () {}, child: const Text("Some text above the list")),
Expanded(
child: ListView.builder(
itemCount: 50,
itemBuilder: (context, i) {
return Container(
margin: const EdgeInsets.all(16),
color: Colors.pinkAccent,
child: Text("$i"));
},
),
),
],
),
),
);
}
Here I have a list of id and title,
I would like to create pages. each page contains one sentences of title. Once click ok button it will navigate to next page of the list.
How can I do that within a single page?
I am a newbie but I would like to minimize my code,
it is a bit confusing.
Here are the images of my code.
import 'package:flutter/material.dart';
import 'package:starbucks/appLayout.dart';
import 'package:starbucks/appTheme.dart';
import 'package:starbucks/components/customAppBar.dart';
import 'package:provider/provider.dart';
import 'package:starbucks/pages/payment.dart';
import 'package:starbucks/providers/cartProvider.dart';
import 'package:starbucks/utils/data.dart';
class CartConfirm extends StatelessWidget {
const CartConfirm({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final totalPrice = context.read<CartProvider>().totalPrice;
return Scaffold(
appBar: CustomAppbar(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
alignment: Alignment.center,
width: 1024,
decoration: BoxDecoration(
border: Border.all(color: AppTheme.green),
borderRadius: BorderRadius.circular(5),
),
padding: const EdgeInsets.all(50),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"ราคารวม: " ,
style: TextStyle(color: Colors.white),
),
AppLayout.staticField(content: " $totalPrice "),
Text(
" บาท",
style: TextStyle(color: Colors.white),
),
],
),
),
Container(
width: 1024,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
AppLayout.button(title: "จ่ายด้วยบัตร", onPressed: () {}),
AppLayout.button(title: "จ่ายด้วยเงินสด", onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Payment(id: id, title: title,),
));},),
],
),
),
],
),
),
);
}
}
static List<Payment> payment = [
Payment(
id: 1,
title: "กรุณาใส่เงิน",
),
Payment(
id: 2,
title: "ระบบกำลังตรวจนับเงิน",
),
Payment(
id: 3,
title: "กรุณารับบัตรคิวและเงินทอน",
),
Payment(
id: 4,
title: "ขอบคุณที่ใช้บริการ",
),
];
in my project I want to add the textinput from a Textfield into a List to build a Listview.builder.
The Problem is that I dont know how to add the String to the List (I want to use the string like in a todo app to make serval dates). (for example) time.add(time1) isnt working and I hope someone can help me.
Is there a completly other way to transport the Inputtext to the list, Im open for everything
First Page
class Homescreen extends StatefulWidget {
String time1;
String who1;
String where1;
String when1;
Homescreen({this.time1, this.who1, this.where1, this.when1});
#override
_HomescreenState createState() => _HomescreenState();
}
TextEditingController myControllertime = TextEditingController();
TextEditingController myControllerwho = TextEditingController();
TextEditingController myControllerwhen = TextEditingController();
TextEditingController myControllerwhere = TextEditingController();
class _HomescreenState extends State<Homescreen> {
List<String> time = ["8:00",];
List<String> who = ["Eric", ];
List<String> when = ["Friday 21.4.21",];
List<String> where = ["At McDonalds", ];
ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: where.length,
itemBuilder: (BuildContext context, int Index) {
return Column(children: [
SizedBox(
height: 40,
),
Container(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Meet1()));
},
child: Container(
width: size.width * 0.9,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(70)),
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomRight,
colors: [
Colors.orange,
Colors.purple,
],
),
),
child: Column(children: <Widget>[
SizedBox(
height: 10,
),
Padding(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
Text(
time[Index],
style: TextStyle(
color: Colors.white,
fontSize: 40,
fontWeight:
FontWeight.bold),
),
SizedBox(
height: 10,
),
Text(
who[Index],
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight:
FontWeight.bold),
),
Text(
when[Index],
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight:
FontWeight.bold),
),
Text(
where[Index],
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight:
FontWeight.bold),
),
Second Page
TextButton(
child: Icon(
Icons.check_circle_outline_rounded,
color: Colors.green,
size: 120,
),
onPressed: () {
Navigator.pop(context, MaterialPageRoute(builder: (builder) {
return Homescreen(
time1: myControllertime.text,
who1: myControllerwho.text,
when1: myControllerwhen.text,
where1: myControllerwhere.text
,
);
}));
})
],
));
child: Column(children: <Widget>[
SizedBox(
height: 10,
),
Padding(
padding: EdgeInsets.all(25),
child: Column(
children: <Widget>[
TextField(
controller: myControllertime,
decoration: InputDecoration(hintText: " Time ")),
SizedBox(
height: 10,
),
TextField(
controller: myControllerwho,
decoration: InputDecoration(hintText: " Who "),
),
SizedBox(
height: 10,
),
TextField(
controller: myControllerwhen,
decoration: InputDecoration(hintText: " Date "),
),
SizedBox(
height: 10,
),
TextField(
controller: myControllerwhere,
decoration: InputDecoration(hintText: " Where "),
),
There is too much wrong tbh in the code. Let us help you.
Firstly, time.add(time1) is not working because you are everytime creating New HomeScreen after addition and List time is reinitialised again and again. thus adding a value wont work here.
To save data you have to actually put that data somewhere in another class with static reference or may be persist them using sharedprefs/anything but that different case.
for example you can create a class like this
class TODOData{
static List<String> time = ["8:00",];
static List<String> who = ["Eric", ];
static List<String> when = ["Friday 21.4.21",];
static List<String> where = ["At McDonalds", ];
}
Now whenever you want to save new field for example time1, in your case, just use it TODOData.time.add(time1); You don't need to pass it your home screen.
And you can access that data using TODOData.time / TODOData.who etc.
You can now even remove all those fields time1, etc from HomeScreen Widget. You can add all those values in onPressed method in SecondScreen in the list as mentioned above. and navigate to HomeScreen, it will have that new data.
This will solve your problem temporarily for data addition.
You can remove all those lists from _HomescreenState and use as mentioned above.
Now comes the ideal way.
You should always create a model class to simplyfy that data. It makes things more readable, accesable and scalable on large projects.
For example instead of creating 4 different list of data you can actually create a model class like this.
class TODOModel{(
String time,
String who,
String when,
String where
)}
And then create a list of it in the same TODOData class.
I have a program that uses a List, but I get this error, how to fix it?
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(children: [
CustomScrollView(physics: BouncingScrollPhysics(), slivers: [
_list(),
]),
]),
);
}
_list() {
return List.generate(
actions.length,
(i) => _card(actions[i]),
);
}
_card(
String phrase,
) {
return SliverToBoxAdapter(
child: Card(
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.black26, width: 0.1),
borderRadius: BorderRadius.circular(10),
),
margin: EdgeInsets.only(right: 50, left: 50, top: 20),
child: InkWell(
splashColor: Colors.black12,
borderRadius: BorderRadius.circular(10),
splashFactory: InkRipple.splashFactory,
onTap: () => _speak(phrase),
child: Column(children: <Widget>[
SizedBox(height: 15.0),
Padding(
padding: EdgeInsets.only(left: 15, right: 15),
child: Text(
phrase,
style: TextStyle(
fontFamily: 'Circular',
fontSize: 17.0,
color: Colors.grey[800]),
),
),
SizedBox(height: 15.0),
]),
),
),
);
}
Your method _list() returns a List<Widget>.
Therefore, you should remove the brackets in your CustomScrollView:
CustomScrollView(physics: BouncingScrollPhysics(), slivers: _list()),
I am trying to fetch a list from API that is two methods fetchImages and fetchCategories. the first time it is showing a red screen error and then after 2 seconds automatically it is loading the list. Can you please tell me what's the issue with my code and how to avoid showing that red screen error in my app?
Widget build(context) {
try{
if (isFirst == true) {
fetchImage();
fetchCategories(context);
isFirst = false;
}
}catch(Exception){
}
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text('Lets see images!'),
),
body: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(
catimages[0],
width: 60.0,
height: 60.0,
),
),
new Text(
categoriesText[0],
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint("on tv clikced");
widget.fetchApI.fetchSubCategories(context, 6);
}),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(
catimages[1],
width: 60.0,
height: 60.0,
),
),
new Text(
categoriesText[1],
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint("on moview clicked");
widget. fetchApI.fetchSubCategories(context, 7);
},
),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(
catimages[2],
width: 60.0,
height: 60.0,
),
),
new Text(
categoriesText[2],
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint("on news clicked");
widget.fetchApI.fetchSubCategories(context, 10);
},
),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(catimages[3],
width: 60.0, height: 60.0),
),
new Text(
categoriesText[3],
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint('on shows clicked');
widget.fetchApI.fetchSubCategories(context, 8);
},
),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset('assets/live_icon.png',
width: 60.0, height: 60.0),
),
new Text(
'Live',
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint('on live clicked');
},
),
],
),
ImageList(images,widget.fetchApI),
],
),
),
);
}
Make sure specifying the length of the list of data. For example, if you're using ListView.builder give proper value to the attribute itemCount.
ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (ctx, index) {
return WidgetItem();
});
The problem can be that you are trying to access a variable/array that is not ready yet (maybe because the future/api call is not finished)
A quick workaround could be to check the length of the array or check for null, example:
Text( (myArray?.length > 0 ? myArray[0] : '') );
There are quick-and-dirty answer, and proper answer
Quick-and-dirty
Use list?.elementAt(<index>) ?? "" for safe access to element of a list
Widget build(context) {
try{
if (isFirst == true) {
fetchImage();
fetchCategories(context);
isFirst = false;
}
}catch(Exception){
}
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text('Lets see images!'),
),
body: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(
catimages?.elementAt(0) ?? "",
width: 60.0,
height: 60.0,
),
),
new Text(
categoriesText?.elementAt(0) ?? "",
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint("on tv clikced");
widget.fetchApI.fetchSubCategories(context, 6);
}),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(
catimages?.elementAt(1) ?? "",
width: 60.0,
height: 60.0,
),
),
new Text(
categoriesText?.elementAt(1) ?? "",
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint("on moview clicked");
widget. fetchApI.fetchSubCategories(context, 7);
},
),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(
catimages?.elementAt(2) ?? "",
width: 60.0,
height: 60.0,
),
),
new Text(
categoriesText?.elementAt(2) ?? "",
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint("on news clicked");
widget.fetchApI.fetchSubCategories(context, 10);
},
),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset(catimages?.elementAt(3) ?? "",
width: 60.0, height: 60.0),
),
new Text(
categoriesText?.elementAt(3) ?? "",
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint('on shows clicked');
widget.fetchApI.fetchSubCategories(context, 8);
},
),
new InkResponse(
child: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: new Image.asset('assets/live_icon.png',
width: 60.0, height: 60.0),
),
new Text(
'Live',
style: TextStyle(color: Colors.white),
),
],
),
onTap: () {
debugPrint('on live clicked');
},
),
],
),
ImageList(images,widget.fetchApI),
],
),
),
);
}
}
Proper answer
Frankly, if I were to review this code, even if it works seamlessly, I would reject this change, because of the structure/pattern this code is using is quite bad.
Please use FutureBuilder, StreamBuilder or ValueListenableBuilder instead, but you need to provide more code (especially fetchImage and fetchCategories) for us to help.
Null safe
Reason for error:
This error occurs on retrieving the value for an index that doesn't exist in the List. For example:
List<int> list = [];
list[0]; // <-- Error since there's no element at index 0 in the list.
Solution:
Check if the the List is not null and has the element at index:
var myList = nullableList;
var index = 0;
if (myList != null && myList.length > index) {
myList[index]; // You can safely access the element here.
}
You are not getting the data. The data folder from or data source is missing. The same happened for me. Later, I created the json file for data and pointed to that location. And it got fixed simply!
I got same issue when tried to access a array which was empty. This was as part of null safety.
my earlier code was
TextBox(response.customerDetails!.address![0].city),
which caused me error so I changed the code to
Text(
(response.cutomerDetails.address.isNotEmpty)
? response.customerDetails!.address![0].city
: "N/A",
),
add a check when accessing arrays. This helped me remove the error.
It happens when you are going to fetch some data but it is not available on that index/position
So, you have to check the index/position value where it is null or not
In my case Listview -> itemcount was perfect but showing this error And then solved it by following checking code
Text("${(widget.topSellItem.subjects.isEmpty) ? "" : widget.topSellItem!.subjects[0].subject.name}"),
I have solved this issue in flutter null safety version by following way.
Reason : It happened when value is not available for that index.
You can check itemCount item value is available or not at builder,
Solution with Null Safety would be like :
ListView.builder(
itemCount: snapshot.data!.items.length, //OR snapshot.data!.length
itemBuilder: (context, index) {
return (index > 0) ? YourWidget() : Container();
});
In case the other methods don't work, check if your database contains any conflicting data entries. If so, fix them.
First, declare the array of objects.
late Map<String, dynamic> product={};
the HTTP answer is:
{
"id": "1",
"codigo": "mw9wcsABvk",
"nombre": "Router TPLink Gaming 5G",
"portada": [
{
"url": "/php/assets/producto/mw9wcsABvk/2729233.png",
"name": "2729233.png"
}
]
}
In Widget build
body: Center(
child: Column(
children: [
if(producto.isNotEmpty)
Expanded(
child: Column(
children: [
ConstrainedBox(
constraints: BoxConstraints.tight(Size(double.infinity, 256)),
child: Stack(
alignment: AlignmentDirectional.center,
children: [
Positioned(
child: Image.network("${host}${producto["portada"][0]["url"]}"),
),
],
),
),
],
),
),
],
),
),
Had same problem when accessing empty arrays, and fix it this ways : data.allData[index].reviews!.isEmpty ? 0 : data.allData[index].reviews![0].rating
when there's data in it, it will access first index.
You must specify the length of the list of data. For example, if you're using ListView along with builder function then you must provide its item length count as itemCount.
ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return //your widget
});
This error comes because of these reasons.
Not using a builder in a screen.
While using a builder we have to provide a condition that checking the list was empty or not. If the list is empty we have to show a circular progress indicator and the list is not empty we can show the list.
If you are fetching data from the API consider using FutureBuilder.
To me, going to the project directory and running the command flutter clean fixed the error