Flutter ListView Text show on multiLine if overflow - flutter

I have A listView in which I have another listview to show simple text in horizontal but the issue is its overflow on the left side If I give width to listview it's going to scroll list. I want if it's overflow then it will simply go to the next line instead of overflow or scroll.
My code
Expanded(
child: new ListView.builder(
itemCount: respondedData3['data'].length,
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: ScrollPhysics(),
itemBuilder: (context, index) {
String TimeFormat = DateFormat("yyyy-MM-dd").format(DateTime.parse(respondedData3['data'][index]['appointmentDate']));
return Padding(
padding: const EdgeInsets.only(left: 15, right: 15),
child: Container(
child: GestureDetector(
onTap: () async {
Navigator.pop(context);
},
// onTap: widget.onPressed,
child: Stack(children: [
Container(
height: 80,
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Column(
children: [
Text(TimeFormat, style: TextStyle(fontSize: 14, fontFamily: 'SegoeUI-Bold', color: kPrimaryColor)),
Text(respondedData3['data'][index]['appointmentTime'].toString(), style: TextStyle(fontSize: 13, fontFamily: 'SegoeUI', color: textGreyColor))
],
),
SizedBox(
width: 10,
),
Container(
color: Colors.grey,
height: 40,
width: 1.5,
),
SizedBox(
width: 10,
),
Column(
children: [
Text(respondedData3['data'][index]['serviceProviderName'].toString(), style: TextStyle(fontSize: 13, fontFamily: 'SegoeUI-Bold', color: textGreyColor)),
Container(
height: 20.0,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: serviceShow.length,
itemBuilder: (BuildContext context, int index) => Text('${serviceShow[index]["Name"]} ${serviceShow[index]["Time"]} ${serviceShow[index]["Price"]}'),
),
),
],
)
],
),
],
),
),
]),
),
),
);
},
),
)
You can see in the image it's showing an overflow error I need to show it in the second line.

ListView.builder(
itemCount: 3,
// respondedData3['data'].length,
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: ScrollPhysics(),
itemBuilder: (context, index) {
String TimeFormat = "yyyy-MM-dd";
// DateFormat("yyyy-MM-dd").format(
// DateTime.parse(respondedData3['data'][index]['appointmentDate']));
return Padding(
padding: const EdgeInsets.only(left: 15, right: 15),
child: Container(
child: GestureDetector(
onTap: () async {
Navigator.pop(context);
},
// onTap: widget.onPressed,
child: Stack(children: [
Container(
height: 80,
// width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Column(
children: [
Text(
TimeFormat,
// style: TextStyle(
// fontSize: 14,
// fontFamily: 'SegoeUI-Bold',
// color: kPrimaryColor,
// ),
),
Text("Time"
// respondedData3['data'][index]
// ['appointmentTime']
// .toString(),
// style: TextStyle(
// fontSize: 13,
// fontFamily: 'SegoeUI',
// color: textGreyColor,
// ),
)
],
),
SizedBox(
width: 10,
),
Container(
color: Colors.grey,
height: 40,
width: 1.5,
),
SizedBox(
width: 10,
),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"serviceProviderName",
// respondedData3['data'][index]
// ['serviceProviderName']
// .toString(),
style: TextStyle(
fontSize: 13,
// fontFamily: 'SegoeUI-Bold',
// color: textGreyColor
),
),
Container(
height: 20.0,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: 33,
itemBuilder:
(BuildContext context, int index) =>
Text(
'{serviceShow[index]["Name"]} {serviceShow[index]["Time"]} {serviceShow[index]["Price"]}',
),
),
),
],
),
),
],
),
],
),
),
]),
),
),
);
},
),

add max lines on your Text
Text(
'Your multiline text',
maxLines: 2, //2 or more line you want
overflow: TextOverflow.ellipsis,
),
),

You get this error because you don't tell specific width For displaying Text widget and your getting text is huge.Always remeber, in Text widget => use 'overflow: TextOverflow.ellipsis' . Because in dynamic application, text can be large
Use FittedBox to when you want to try your text in limited width. And you want not get overflow error, use "overflow: TextOverflow.ellipsis"
FittedBox(
child:
Container(
width: 260,
// height: 50,
child: const Text(
"testing1 testing2 testing3 testing4 testing5 testing6 testing7 testing8 testing9 testing10 testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing",
style: TextStyle(
// color: Colors.lightBlueAccent,
fontSize: 17,
fontWeight: FontWeight.w600,
),
maxLines: 4,
overflow: TextOverflow.ellipsis
),
),
)

The text in my app was overflowing too , and maxLines: 2, overflow: TextOverflow.ellipsis, was not working since the text was part of the Expanded(child: ListView.builder( so what I did was
LimitedBox(
maxWidth: 150,
child: Text(
videoInfo[index]["title"],
overflow: TextOverflow.clip,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
)

use Expanded up your Column example;
Expanded(child:
Column(
children: [
Text ('long text'),
Text ('long text 2')
]
)
);

Related

Flutter: Make all screen scrollable with GridView.builder inside

In my home screen my app shows carousel first then a vertical list of challenges cards retrieved from Cloud Firestore using GridView.builder as follows:
GridView.builder(
scrollDirection: Axis.vertical,
itemCount: _challenges.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 4),
),
itemBuilder: (context, index) {
return InkWell(
onTap: () {
if (_challenges[index]["isLocked"] == "true") {
showLockedDialog();
} else {
checkParticipation(index);
if (checkPart == true) {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) =>
ChallengeDetails(_challenges[index])));
}
checkPart = true;
}
},
child: Stack(
children: [
Center(
child: ClipRRect(
borderRadius: BorderRadius.circular(15),
child: Image(
image: NetworkImage(_challenges[index]["image-path"]),
fit: BoxFit.cover,
height: 150,
width: 350,
opacity: _challenges[index]["isLocked"] == "true"
? AlwaysStoppedAnimation(.4)
: null,
),
),
),
Center(
child: Text(
"${_challenges[index]["name"]}\n",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
),
Center(
child: Text(
"\n${_challenges[index]["date"]}",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
textDirection: TextDirection.ltr,
)),
Center(
child: SizedBox(
height: 130,
width: 130,
child: _challenges[index]["isLocked"] == "true"
? Image.asset("assets/lock-icon.jpg")
: null,
),
)
],
),
);
});
Everything retrieving fine and it is rendered in my home_screen as follows:
body: Column(
children: [
AdsBanner(),
SizedBox(
height: 30,
),
Padding(
padding: const EdgeInsets.only(right: 8, left: 8, bottom: 5),
child: Row(
children: [
Text(
AppLocalizations.of(context)!.challenges + " ",
style: TextStyle(fontSize: 20),
),
Text(
AppLocalizations.of(context)!.clickToParticipate,
style: TextStyle(fontSize: 15),
)
],
),
),
Expanded(child: ChallengeCard()),
],
),
The problem is that only the GridView area is scrolling and what am seeking for is to scroll the whole screen with the GridView area, I was trying to use the CustomScrollView() but its not working properly.
I'll be thankful for any help.
First in your GridView.builder add these:
GridView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
...
)
then in your home_screen wrap your column with SingleChildScrollView:
SingleChildScrollView(
child: Column(
children: [
AdsBanner(),
SizedBox(
height: 30,
),
Padding(
...
),
),
You can provide physics: NeverScrollableScrollPhysics() on GridView to disable scroll effect. If you want scrollable as secondary widget use primary: false, to have Full Page scrollable, you can use body:SingleChildScrollView(..) or better using body:CustomScrollView(..)

Flutter Column with ListView and more Widgets above it

i'm trying a simple title -> search box -> listview thing, but can;t figure out how to fill the screen without tripping the horrible overflow error.
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Text('title', textAlign: TextAlign.center, style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),),
......
TextField(textfield_settings),
SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height * 0.6, // the part i want to get rid of and have a flexible height, based on the total screen size and the other widgets
child: ListView.builder(itemBuilder: (ctx, index) {})
)
)
I Basically want the SingleChildScrollView containing the ListView.builder to take up the rest of the space left in the body.
thanks!
Use Expanded on ListView
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Text(
'title',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
TextField(),
Expanded(
child: ListView.builder(
itemBuilder: (ctx, index) {
return Text("sds");
},
),
),
],
),
),

The Scrollbar's ScrollController has no ScrollPosition attached while using for loop in flutter

I am using a for loop to display some text with a scrollbar. It started throwing me this error - The Scrollbar's ScrollController has no ScrollPosition attached.
When I was not using for lopp, i.e., when I was directly just copy pasted row and sizedbox several time as children of listview, I didn't got the error.
Here is the code -
List<String> list1 = [
"Academic Year",
"Lead Id",
"Lead created date",
"Student name",
"Grade",
"Date of birth",
"Gender",
"Email id",
"Address",
"Counsellor",
"BDA",
"Digital Counsellor"
];
List<String> list2 = [
"2023",
"156388",
"22-03-2019",
"Ankit",
"9th",
"21-04-2008",
"Male",
"ankit#gmail.com",
"None",
"2023",
"156388",
"22-03-2019"
];
.............
Container(
height: MediaQuery.of(context).size.height*0.5,
padding: EdgeInsets.symmetric(
vertical: getProportionateScreenWidth(32),
),
child: Scrollbar(
controller: controller,
isAlwaysShown: true,
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(24),
),
child: ListView(
children: [
for(int i=0;i<list1.length;i++)
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
list1[i],
style: TextStyle(
color: primaryText,
fontSize: 16,
),
),
Text(
list2[i],
style: const TextStyle(
color: Colors.black87,
fontSize: 16,
),
)
],
),
SizedBox(
height: getProportionateScreenHeight(20),
),
],
)
],
),
),
),
),
Use Listview.builder to build a list of items for you, instead of for loop.
Here's the code
Container(
height: MediaQuery.of(context).size.height*0.5,
padding: EdgeInsets.symmetric(
vertical: getProportionateScreenWidth(32),
),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(24),
),
child: ListView.builder(
itemCount: list1.length,
itemBuilder: (BuildContext context, int index) {
return
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
list1[index],
style: TextStyle(
color: primaryText,
fontSize: 16,
),
),
Text(
list2[index],
style: const TextStyle(
color: Colors.black87,
fontSize: 16,
),
)
],
),
],
);
}
),
),
)
You may assign controller to Listview.builder , if you want to scroll it programmatically.

Extra "Space" In ListView and Card Flutter

I'm having this "extra space" on my first listview item that is a card being encapsulated in a container.
How do I remove it?
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(15.0),
height: 300,
child: Column(children: [
Row(children: [
Expanded(
child: Text("Lowest Fuel Price",
style: TextStyle(
fontWeight: FontWeight.w500,
color: Theme.orange3,
fontSize: 16.0)))
]),
Container(
height: 250,
child: Card(
elevation: 3,
child: ListView.builder(
itemCount: cheapest.length,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
final item = cheapest[index];
return Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(
item.petrolType,
style: TextStyle(
color: Theme.gray1, fontSize: 18),
),
Text(
"\$" + item.petrolPrice,
style: TextStyle(
color: Theme.gray1, fontSize: 25),
),
Image(
fit: BoxFit.fill,
image: new AssetImage(
'assets/images/fuel_station/' +
item.petrolStation.toLowerCase() +
'.png')),
]),
index != cheapest.length - 1
? Divider(
color: Colors.grey,
)
: Divider(color: Colors.white)
],
);
})),
)
]));
}
https://api.flutter.dev/flutter/widgets/ListView-class.html
By default, ListView will automatically pad the list's scrollable extremities to avoid partial obstructions indicated by MediaQuery's padding. To avoid this behaviour, override with a zero-padding property.
Card(
elevation: 3,
child: MediaQuery.removePadding(
context: context,
removeTop: true,
child: ListView.builder(),
),
),

How to fix "Could not find the correct Provider<Product> above this ProductItem Widget "?

I am very much new to flutter. This is my first time using providers. I have been through every solution out there but nothing seems to help.
In a shopping app I am developing, I tried to use the provider package to pass data to my widget dynamically.
I am sharing the code and the screenshot of the very big error message. Hope somebody could help
Gridview builder
final productData = Provider.of<ProductsProvider>(context);
final products = productData.items;
return GridView.builder(
padding: EdgeInsets.all(10),
itemCount: products.length,
itemBuilder: (context, index) => ChangeNotifierProvider.value(
value: products[index], child: ProductItem()),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 0.75,
crossAxisSpacing: 10,
mainAxisSpacing: 10),
);
Individual product item in the grid
final product = Provider.of<Product>(context);
return Container(
child: GestureDetector(
onTap: () {
Navigator.of(context)
.pushNamed(ProductDetails.routeName, arguments: product.id);
},
child: Card(
color: Colors.white,
elevation: 6,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
//product image
Image(image: AssetImage(product.imageUrl)),
Padding(
padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
//item name and fav button
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Expanded(
child: Text(
product.name,
style: Theme.of(context)
.textTheme
.headline1
.copyWith(fontSize: 16, color: Colors.black),
)),
IconButton(
icon: Icon(
product.isFav
? Icons.favorite
: Icons.favorite_border,
color: Colors.red,
),
onPressed: () {
product.toggleFav();
})
],
),
),
// price and buy button
Padding(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Expanded(
child: Text(
"\$ " + product.price.toString(),
style: Theme.of(context).textTheme.headline1.copyWith(
color: Theme.of(context).primaryColor, fontSize: 15),
)),
SizedBox(
width: 60,
child: RaisedButton(
onPressed: () {},
color: Colors.orange,
child: Text(
"Buy",
style: TextStyle(fontFamily: 'Prompt', fontSize: 14),
),
),
)
],
),
),
],
),
),
),
);
screenshot of error
You need to declare the provider in you main material widget. Atm you are using it but you haven't told flutter that you will use it.
https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple#changenotifierprovider