The provided ScrollController is currently attached to more than one ScrollPosition - flutter

I have a container with ScrollBar and SingleChildScrollView widgets inside a PageView.builder. When Change pages, I get the following message:
The provided ScrollController is currently attached to more than one ScrollPosition.
Here is the code:
body: PageView.builder(
scrollDirection: Axis.horizontal,
controller: _pageController,
itemCount: snapshot.data!.docs.length,
itemBuilder: (ctx, i) {
var output = snapshot.data!.docs[i];
return SingleChildScrollView(
controller: _mainScrollViewController,
child: Column(
children: [
SizedBox(
height: heightWithoutAppBar * 0.18,
child Expanded(
flex: 48,
child: SizedBox(
height: double.infinity,
width: double.infinity,
child: Scrollbar(
controller: _scrollController,
isAlwaysShown: true,
child: SingleChildScrollView(
controller: _scrollController,
scrollDirection: Axis.vertical,
child: Text(
output['title'],
textAlign: TextAlign.justify,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500),
)),
),
)),
),
],
),
);
})

Related

Exception caught by rendering library | Flutter

What I want to do,
first list view scroll horizontally and the second one and full-screen scroll vertically.
(When the user scrolls vertically, the first listview will be hidden)
Like Facebook feed screens
the first list looks like a story row,
the second list looks like posts.
This picture shows what I want to do simply
I tried,
my code here :
Stack(
children: [
SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: 100.0,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) => PostCard(
snap: snapshot.data!.docs[index].data(),
),
),
),
Text(
'Demo Headline 2',
style: TextStyle(fontSize: 18),
),
ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) => PostCard(
snap: snapshot.data!.docs[index].data(),
),
),
],
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(bottom: 80, right: 10),
child: FloatingActionButton(
elevation: 100,
onPressed: () {
Navigator.pushNamed(context, '/add');
},
child: const Icon(
Icons.add,
size: 32,
color: Colors.amber,
),
backgroundColor: Colors.black,
),
),
),
],
);
My code shows this error
Exception caught by rendering library
RenderBox was not laid out: RenderRepaintBoundary#90e8f relayoutBoundary=up16 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
package:flutter/…/rendering/box.dart:1
Failed assertion: line 2001 pos 12: 'hasSize'
Try below code and shrinkWrap: true, in 2nd Listview
Listview Widget:
Expanded(child:
ListView.builder(
shrinkWrap: true,//add this line
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) => PostCard(
snap: snapshot.data!.docs[index].data(),
),
),
),
Full Code:
Scaffold(
body: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: 100.0,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: 5,
itemBuilder: (context, index) => Container(
padding: const EdgeInsets.all(12),
margin: const EdgeInsets.all(12),
height: 150,
width: 150,
color: Colors.pink,
),
),
),
Text(
'Demo Headline 2',
style: TextStyle(fontSize: 18),
),
Expanded(
child: ListView.builder(
itemCount: 10,
shrinkWrap: true,
itemBuilder: (context, index) => Container(
padding: const EdgeInsets.all(12),
margin: const EdgeInsets.all(12),
height: 150,
width: 150,
color: Colors.amber,
),
),
),
],
),
floatingActionButton: FloatingActionButton(
elevation: 100,
onPressed: () {
Navigator.pushNamed(context, '/add');
},
child: const Icon(
Icons.add,
size: 32,
color: Colors.amber,
),
backgroundColor: Colors.black,
),
);
Result->

flutter add widgets top and bottom to list GridView.builder

Tell me how to add widgets to the top and bottom of this list using GridView.builder?
I've tried using Column as well, but it doesn't work the way I wanted. What other options are there?
Widget build(BuildContext context)
{
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: Stack(
children: [
LayoutBuilder(
builder: (context, constraints) {
return GridView.builder(...);
}
),
Padding(child: TextField(...))
],
),
);
}
Try below code:
SingleChildScrollView(
padding: const EdgeInsets.all(10),
child: Column(
children: [
Container(
width: double.infinity,
height: 50,
color: Colors.blue,
child: const Text(
'Your Apper Widget',
textAlign: TextAlign.center,
),
),
GridView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: 9,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3),
itemBuilder: (context, index) {
return const Card(
child: ListTile(
title: Text('userList'),
),
);
},
),
Container(
width: double.infinity,
height: 50,
color: Colors.red,
child: const Text(
'Your Lower Widget',
textAlign: TextAlign.center,
),
),
],
),
),
Try this code :
body: Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(
color: Colors.red,
height: 48.0,
),
Expanded(
child: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemBuilder: (_, index) => const FlutterLogo(),
itemCount: 20,
),
),
Container(
color: Colors.green,
height: 48.0,
)
],
),

When scroll direction is horizontal, text overflow is not working. How can I TextOverflow.ellipsis work properly?

I would like to overflow long text in a column with Card. Text must overflow width of Card. Scroll Direction set to Axis.horizontal for that reason TextOverflow does not pass to second line or overlow. Do you have any suggestions?
SingleChildScrollView(
scrollDirection:
Axis.horizontal,
child: Row(
children: [
Column(
children: [
Flexible(
flex: 5,
child: SizedBox(
width: 80.w,
height: 80.h,
child: Card(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
10.r)),
child: Center(
child: Image
.network(
productsProvider
.randomProducts[
itemIndex]
.photo,
fit: BoxFit
.cover,
),
)),
),
),
Flexible(
child: Text(
productsProvider
.randomProducts[
itemIndex]
.name,
style: TextStyle(
fontSize:
10.sp),
overflow:
TextOverflow
.ellipsis,
maxLines: 2,
),
)
],
),
],
),
);
Here is the output
This is the code that everybody can see what happens. It is not complicated but I did not find a solution to work properly.
ListView.builder(
itemCount: strings.length,
shrinkWrap: true,
controller: _scrollController,
itemBuilder: (BuildContext context, int index) {
return Column(
children: [
Padding(padding: EdgeInsets.only(bottom: 8)),
Container(
alignment: Alignment.centerLeft,
child: Text(strings[index]),
padding: EdgeInsets.only(bottom: 8, left: 8),
),
Container(
decoration: BoxDecoration(color: Colors.white),
child: Padding(
padding: EdgeInsets.all(8),
child: Column(
children: [
SizedBox(
height: 80,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: 2,
itemBuilder: (BuildContext context, int itemIndex) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
Column(
children: [
Flexible(
flex: 5,
child: SizedBox(
width: 80,
height: 80,
child: Card(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
10)),
child: Center(
child: Image.network(
'https://ayb.akinoncdn.com/products/2019/04/24/21736/788e20d1-81ca-42da-a6bb-25c2698fafaa_size780x780_quality60_cropCenter.jpg',
fit: BoxFit.cover,
),
)),
),
),
Flexible(
child: Text(
'Osmanlı şahzade badem ezmesi. Üretimi tecrübeye sabit',
style: TextStyle(fontSize: 10),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
)
],
),
],
),
);
}),
)
],
),
),
),
],
);
});
Actually the reason behind is that you have set the scrollDirection to horizontal and inside it you are using a Row as well which also has a horizontal axis direction and after that you are passing your Text widget inside Flexible and according to Flexible widget property it will stretch it self according to it's child size. In your case Flexible has a unbounded width so it's expanding it self according to it's child which result a full length text to appear and because it's showing full content TextOverflow.ellipsis will not work.
Basically to solve this you have to wrap your Text widget to a SizedBox so that it can have a bounded width.

App bar doesn't hide on scroll in nestedscrollview flutter

So i want to show posts and for that am using listview.builder in the body of nestedscrollview but when i scroll down then appbar doesn't get hided. This is in case of listview.builder only if i assign controller to it. If i don't then it works perfectly.
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
pinned: false,
floating: true,
snap: true,
forceElevated: innerBoxIsScrolled,
backgroundColor: Themes.secondaryColor,
automaticallyImplyLeading: false,
elevation: 0.0,
titleSpacing: 0,
toolbarHeight: 90,
title: Padding(
padding: const EdgeInsets.only(left:8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Trending",
style: GoogleFonts.roboto(
textStyle: const TextStyle(
fontSize: 20,
color: Color(0xff000000),
fontWeight: FontWeight.w500,
),
),
textAlign: TextAlign.left,
),
const SizedBox(height: 10,),
SizedBox(
width: double.infinity,
height: 27,
child: ListView.separated(
scrollDirection: Axis.horizontal,
physics: const BouncingScrollPhysics(),
itemCount: categories.length,
separatorBuilder: (context,index)=>const SizedBox(width: 5,),
itemBuilder: (context,index){
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: selectedIndex==index?Themes.primaryColor:Themes.primaryColor.withOpacity(0.5),
),
padding: const EdgeInsets.symmetric(vertical:5,horizontal: 12),
child: Text(
categories[index],
style: const TextStyle(color: Colors.white,fontSize: 13),
),
);
},
),
),
],
),
),
),
];
},
body: loading?const Center(child: CircularProgressIndicator(),
):ListView.separated(
controller: scrollController,
padding: const EdgeInsets.symmetric(vertical:15),
itemCount: posts.length,
separatorBuilder: (context,index)=>const SizedBox(height: 6,),
itemBuilder: (context,index)=>posts[index],
),
),
),
);
If you only need the controller to do something like pagination or listen to scroll event use NotificationListener it will allow you to do that.

Nested ListView not visibile - Flutter

I have two list view. One is a wrapper of all the widgets in the view, the second one is a child of it.
Here is the code:
ListView(
scrollDirection: Axis.vertical,
physics: AlwaysScrollableScrollPhysics(),
children: [
Container(
................
),
Container(
................
),
ListView.builder(
itemBuilder: (context, index) {
return Expanded(child: Column(children: [
Divider(
color: Colors.black12,
height: 20,
thickness: 1,
indent: 20,
endIndent: 0,
),
Row(children: [
Image.network(
"http://openweathermap.org/img/wn/10d#2x.png"),
Text(Utils.days[index + 1],
style: GoogleFonts.roboto(
fontSize: 16, color: Colors.white))
]),
]));
},
itemCount: 5,
scrollDirection: Axis.vertical,
)
]);
The two containers are visible. I can't see the items inside the second list view. There isn't any error in the console. I have no idea why is this happening.
I fixed it by adding shrinkWrap: true and physics: NeverScrollableScrollPhysics() to the nested ListView
I Hope this code will work for you.
return Scaffold(
body: Container(
child: SingleChildScrollView(
child: Column(mainAxisSize: MainAxisSize.min,
// scrollDirection: Axis.vertical,
// physics: AlwaysScrollableScrollPhysics(),
children: [
Container(
child: Text("one"),
),
Container(
child: Text("two"),
),
Container(
height: MediaQuery.of(context).size.height / 2,
child: ListView.builder(
itemBuilder: (context, index) {
return Column(children: [
Divider(
color: Colors.black12,
height: 20,
thickness: 1,
indent: 20,
endIndent: 0,
),
Row(children: [
Image.network(
"http://openweathermap.org/img/wn/10d#2x.png"),
Text(index.toString(),
style: GoogleFonts.roboto(
fontSize: 16, color: Colors.white))
]),
]);
},
itemCount: 5,
scrollDirection: Axis.vertical,
),
)
]),
)),
);