GridView is not working inside SingleChildScrollView - flutter

I have a Column and inside that Column, I have SingleChildScrollView and inside it I have another Column. This is not working:
Column(
children: <Widget>[
SliderWidget(SliderList, "text", "text"),
const SizedBox(height: 20),
SingleChildScrollView(
child: Column(
children: [
text("Some text"),
Expanded(
child: Padding(
padding: const EdgeInsets.all(24.0),
child:
GridListing(FavouriteList, false),
),
),
],
),
),
],
),
Please suggest me alternate.

SingleChildScrollView and Expanded both are trying to get infinite height. Providing height on GridView will solve the issue.
To understand the overflow, we need to check how SingleChildScrollView is getting size and setting on children. In order to get available space, we need wrap SingleChildScrollView with Expanded widget.
Now we can wrap GridView with SizedBox(height:x
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) => Column(
children: <Widget>[
// SliderWidget(SliderList, "text", "text"),
const SizedBox(height: 20),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Text("Some text"),
SizedBox(
height: constraints.maxHeight * .6,
child: GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 4,
mainAxisSpacing: 4,
children: List.generate(
222,
(index) => Container(
color: Colors.red,
width: 200,
height: 200,
child: Text(" $index"),
),
),
),
),
Container(
height: 500,
color: Colors.pink,
),
],
),
),
),
],
),
),
);
A better way of doing it will be using CustomScrollView

If you use the IntrinsicHeight widget around the inner column flutter can display your widget tree.
Column(
children: <Widget>[
SliderWidget(SliderList, "text", "text"),
const SizedBox(height: 20),
SingleChildScrollView(
child: IntrinsicHeight( // added widget
child: Column(
children: [
text("Some text"),
Expanded(
child: Padding(
padding: const EdgeInsets.all(24.0),
child:
GridListing(FavouriteList, false),
),
),
],
),
),
),
],
),

Related

How make something fixed position during scroll in Flutter

I'm up to making a screen like on the pic
I'd like to add scroll for gridview, but the trouble now is I don't really understand how to achieve that.
When I wrap Grid with SingleChildGridView, I've got an error that bottom overflowed. Example is on the second screen:
Obviously, it's happening as the GridView is a part of Column which causes the error. But how can I find a wayaround to avoid wrapping the column with let's say singlechildscrollview and at the same time making scrollable only GridView ?
Here is my code:
Scaffold(
appBar: HomeAppBar(),
bottomNavigationBar: CustomNavBar(),
backgroundColor: Colors.white,
body: Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Hemendra',
style: Theme.of(context).textTheme.displaySmall
),
Padding(
padding: EdgeInsets.only(top: 5),
child: Text(
'Welcome to Laza.',
style: Theme.of(context).textTheme.bodyMedium
),
),
Searchbox(),
BlocBuilder<ProductBloc, ProductState>(
builder: (context, state) {
if (state is ProductLoaded) {
return Padding(
padding: const EdgeInsets.only(top: 15.0),
child: SingleChildScrollView(
child: GridView.builder(
shrinkWrap: true,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
mainAxisSpacing: 10,
crossAxisSpacing: 10,
mainAxisExtent: 300,
crossAxisCount: 2,
),
itemCount: state.products.length,
itemBuilder: (BuildContext ctx, index) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Stack(
children: [
GestureDetector(
onTap: () {
BlocProvider.of<ProductDetailsBloc>(
context)
.add(ProductDetailsEvent(
state.products[index]));
Navigator.pushNamed(
context, '/product_details');
},
child: Container(
height: 240,
child: Image.network(
state.products[index].imageUrl, fit: BoxFit.fill,),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
IconButton(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
icon: Image(
image: AssetImage('heart.png'),
),
onPressed: () {},
),
],
),
],
),
Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 5),
child: Row(
children: [
Expanded(
child: Text(
maxLines: 2,
state.products[index].name,
style: Theme.of(context).textTheme.bodySmall
),
),
],
),
),
Padding(
padding: EdgeInsets.only(top: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
maxLines: 2,
"\$" +
state.products[index].price
.toString(),
style: TextStyle(
color: HexColor('1D1E20'),
fontWeight: FontWeight.w600),
),
],
))
],
),
],
);
},
),
),
);
}
return Center(
child: CircularProgressIndicator(),
);
},
)
],
),
),
);
}
}
remove SingleChildScrollView and warp you BlocBuilder with Expanded widget .
so you code widget inside Scaffold body like this
Column(
children: [
searchBox(),
SizedBox(height:70, child: horizontalBrandList()),
Expanded(child: BlocBuilder(...))
],
)
created code similar UI code for better understanding update your code accordingly:
Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
//SEARCH BAR
const TextField(decoration: InputDecoration(hintText: "Search")),
const SizedBox(height: 12),
// HORIZONTAL LIST VIEW
SizedBox(
height: 50,
child: ListView(
scrollDirection: Axis.horizontal,
children: List.generate(
10,
(i) => Container(
width: 50,
color: Colors.accents[i % 16],
alignment: Alignment.center,
child: Text('$i'),
),
),
),
),
const SizedBox(height: 12),
// GRID LISTVIEW
Expanded(
child: GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: MediaQuery.of(context).size.width * 0.5,
childAspectRatio: 0.8,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
),
itemCount: 20,
itemBuilder: (BuildContext ctx, index) {
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.amber,
borderRadius: BorderRadius.circular(15)),
child: Center(child: Text(index.toString())),
);
},
),
)
],
),
)
You are adding ListView inside Column so You need to wrap with with Expanded Widget. in ListView contains own ScrollController so no need to wrap it with external SingleChildScrollView

flutter: RenderFlex children have non-zero flex but incoming height constraints are unbounded. in GridView widget

I want to create a grid view with four columns in two rows if I add a widget GridView() it shows an error on compile time.
it shows an error multiple times when the app is hot reload after saving file.
Error: RenderFlex children have non-zero flex but incoming height constraints are unbounded.
Error:
Image of Error shows in terminal please look into this
Here is my code:-
import 'package:flutter/material.dart';
import 'package:dateapp/utils/widget_functions.dart';
import 'package:dateapp/custom/BorderIcon.dart';
class Gender extends StatelessWidget{
#override
Widget build(BuildContext context){
final Size size = MediaQuery.of(context).size;
final ThemeData themeData = Theme.of(context);
final double padding = 25;
final sidePadding = EdgeInsets.symmetric(horizontal: padding);
//return SafeArea(
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomRight,
colors: const [Color.fromRGBO(132,105,211,1), Color.fromRGBO(93,181,233,1), Color.fromRGBO(86,129,233,1)],
),
),
width: size.width,
height: size.height,
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
addVerticalSpace(padding),
Padding(
padding: sidePadding,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children:[
InkWell(
onTap: (){
Navigator.pop(context);
},
child: BorderIcon(
padding: new EdgeInsets.all(0.0),
height: 100,
width: 0,
child: Icon(
Icons.arrow_back_ios_outlined,
color:Colors.white,
),
),
),
]
),
),
addVerticalSpace(padding),
Padding(
padding: sidePadding,
child: Text(
'Your profile info',
style: TextStyle(
color: Colors.white,
fontSize: 20,
),),
),
addVerticalSpace(20),
Padding(
padding: sidePadding,
child: Column(
children: <Widget>[
addVerticalSpace(15),
Column(
children: <Widget>[
Expanded(
child: Column(
children: [
GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
children: [
Text('one'),
Text('two'),
Text('Three'),
Text('Four'),
],
),
],
),
),
],
),
addVerticalSpace(10),
],
),
),
],
),
],
)
),
floatingActionButton: Container(
child: Padding(
padding: sidePadding,
child: Align(
alignment: Alignment.bottomCenter,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.deepPurpleAccent,
minimumSize: const Size.fromHeight(50), // NEW
),
child: const Text(
'Next',
style: TextStyle(
fontSize: 18,
),
),
onPressed: () {},
),
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
//);
}
}
Please help me out with this error I don't know how to fix this
This is what I want
I change the code of
Padding(
padding: sidePadding,
child: Column(
children: <Widget>[
addVerticalSpace(15),
Column(
children: <Widget>[
Expanded(
child: Column(
children: [
GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
children: [
Text('one'),
Text('two'),
Text('Three'),
Text('Four'),
],
),
],
),
),
],
),
To
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Expanded(
child: GridView(
padding: const EdgeInsets.all(8.0),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
children: [
Text('one'),
Text('two'),
Text('Three'),
Text('Four'),
],
),
),
],
),
)
),
But now it shows another error
image of another error
Use Padding widget as child on Expanded.
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: GridView(....)
You can also use GridView's padding:. If you want to have multiple column wrap Column and GridView with Expanded.
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Expanded(
child: GridView(
padding: const EdgeInsets.all(8.0),
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
children: [ .....

Flutter/Dart - Vertically Centre Gridview

I am trying to centre a gridview in the centre of my scaffold but I am struggling to achieve this. I have tried wrapping in a Container with the alignment arguments as well as in a column and centre widgets. I have only been able to align my gridview in the centre horizontally. Can someone help? Here is my code:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("My gridview"),
),
body: Center(
child: Padding(
padding: const EdgeInsets.only(left: 50, right: 50),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: MediaQuery.of(context).size.height * 0.4,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: Container(
height: double.infinity,
child: Center(
child: Text("Row Item 1"),
),
),
),
Expanded(
child: Container(
height: double.infinity,
child: Center(
child: Text("RowItem2"),
),
),
),
],
),
),
),
//CENTRE THE BELOW WIDGET IN MIDDLE OF SCREEN
Expanded(
child: Container(
child: GridView.builder(
shrinkWrap: true,
itemCount: 10,
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
//The maximum horizontal width of a single child Widget
maxCrossAxisExtent: 50,
// horizontal spacing between individual child widgets
mainAxisSpacing: 10.0,
//Vertical vertical spacing between individual child widgets
crossAxisSpacing: 10.0,
),
itemBuilder: (context, index) {
return Card(
child: Center(
child: Text(
index.toString(),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
),);
},
),
),
)
],
),
),
),
);
}
Try the following, maybe your widget is taking constraints from its parents
SizedBox(
width:MediaQuery.of(context).size.width,
child:Wrap(
alignment: WrapAlignment.start
children:[
//CENTRE THE BELOW WIDGET IN MIDDLE OF SCREEN
Expanded(
child: Container(
child: GridView.builder(
shrinkWrap: true,
itemCount: 10,
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
//The maximum horizontal width of a single child Widget
maxCrossAxisExtent: 50,
// horizontal spacing between individual child widgets
mainAxisSpacing: 10.0,
//Vertical vertical spacing between individual child widgets
crossAxisSpacing: 10.0,
),
itemBuilder: (context, index) {
return Card(
child: Center(
child: Text(
index.toString(),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
),);
},
),
),
)
],
),
),
Kindly try using a stack, As there is not pictorial representation of your expected result, I have tried according to your question statement, the code is here as follows (The colors have been changed for reference).
return Scaffold(
appBar: AppBar(
title: Text("My gridview"),
),
body: Stack(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: Container(
child: Center(
child: Text("Row Item 1"),
),
),
),
Expanded(
child: Container(
child: Center(
child: Text("RowItem2"),
),
),
),
],
),
SizedBox(
height: 30,
),
Container(
height: MediaQuery.of(context).size.height * 0.2,
padding: EdgeInsets.symmetric(
horizontal: MediaQuery.of(context).size.width * 0.05),
child: Expanded(
child: Container(
color: Colors.amber,
child: GridView.builder(
shrinkWrap: true,
itemCount: 100,
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
//The maximum horizontal width of a single child Widget
maxCrossAxisExtent: 50,
// horizontal spacing between individual child widgets
mainAxisSpacing: 10.0,
//Vertical vertical spacing between individual child widgets
crossAxisSpacing: 10.0,
),
itemBuilder: (context, index) {
return Card(
child: Center(
child: Text(
index.toString(),
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold),
),
),
);
},
),
),
),
),
],
)
],
),
);

How to space between rows?

I have an expanded widget for my ClipRRect, which I think I need, but I also need to spacebetween my row, but I can't manage it. If I wrap it in an expanded widget it changes the size of my image in ClipRRect, which I don't want to do. This is my code:
#override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(6),
child: GestureDetector(
onTap: function,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
flex: 1,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Stack(
children: <Widget>[
assetImage,
],
),
),
),
Row(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text('$myText -', style: myTextStyle),
SizedBox(width: 5.0,),
Text('$locationText', style: myTextStyle),
],
),
Card(
elevation: 1,
color: Colors.white,
shape: cardBorder,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('$myText - $endmyText mins', style: myTextStyle,),
),
),
],
),
],
),
),
),
);
}
Assuming your question says you want to divide widgets with equal spaces, for it replaces Row with Wrap widget
Wrap(
direction: Axis.horizontal,
spacing: 30, // add space in between widget
children: <Widget>[
Wrap(
spacing: 30, // add space in between widget
direction: Axis.horizontal,

Flutter set height automatically for TabBarView

My problem is i have to set fixed height for TabBarView to solve RenderBox error, unfortunately this size is fixed and it couldn't expanded by child heigh. how can i resolve that?
i tested Expanded, SizedBox.expanded but it doesn't any change
Scaffold <----------------parent
SingleChildScrollView <---child
Stack <-----------------child
Padding <-----------------child
Column <-----------------child
Container( <--------------child
height: 250.0,
child: TabBarView(
children: [
Column(
),
Column(
),
Column(
),
],
controller: tabController,
),
),
);
I am not sure how you want your UI to look like, but you can try these 2 solutions:
Use LimitedBox and give some maxHeight
SingleChildScrollView(
physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
LimitedBox( // use this
maxHeight: 200, // give some maxHeight
child: TabBarView(
children: [
FlutterLogo(colors: Colors.orange, size: 100,),
FlutterLogo(colors: Colors.red, size: 100),
],
),
),
SizedBox(height: 50),
FlutterLogo(size: 100),
SizedBox(height: 100),
FlutterLogo(size: 100),
],
),
],
),
)
Remove SingleChildScrollView and add Expanded
Stack( // remove SingleChildScrollView
children: <Widget>[
Column(
children: <Widget>[
Expanded( // use Expanded
child: TabBarView(
children: [
FlutterLogo(colors: Colors.orange),
FlutterLogo(colors: Colors.red),
],
),
),
SizedBox(height: 50),
FlutterLogo(size: 100),
SizedBox(height: 100),
FlutterLogo(size: 100),
],
),
],
)