I have a column with a container that acts as a header and a gridview of widgets below.
I want only the gridview to be scrollable and that the header will not move and always be displayed.
This is my code:
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Container(
margin: EdgeInsets.only(left: 24, right: 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Message("Hello, Guest!", size: 32),
Icon(
CustomIcons.statistics,
color: AppColors.blue,
size: 32.0,
),
],
),
SizedBox(height: 14),
Message("What worries you now? Select or Add.", size: 14),
],
),
),
SizedBox(height: 16),
GridView.count(
primary: true,
shrinkWrap: true,
crossAxisCount: 2,
crossAxisSpacing: 40.0,
mainAxisSpacing: 40.0,
padding: const EdgeInsets.all(20.0),
children: _items,
),
],
),
),
);
}
}
When it runs I get a renderflex overflowed error:
When I wrap my gridview in an expanded widget as some answers suggested, the scroll gets out of bounds:
Appreciate the help!
Remove expnaded and wrap your safe area With SingleChildScrollView, and also add phyiscs:ScrollPhysics() in gridview
Wrap your Column with a SingleChildScrollView, A Column will always try to shrink-wrap its children, using Expanded tells the GridView to only take up the available space.
Another alternative is if your GridView doesn't grow in too much size is to use the shrink-wrap: true and use physics: const NeverScrollablePhyscis(), that way the GridView won't try to grow to infinite size.
You can wrap your Column inside a SingleChildScrollView widget to make the whole column scrollable. The Code Should be :
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child:Column(
children: [
Container(
margin: EdgeInsets.only(left: 24, right: 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Message("Hello, Guest!", size: 32),
Icon(
CustomIcons.statistics,
color: AppColors.blue,
size: 32.0,
),
],
),
SizedBox(height: 14),
Message("What worries you now? Select or Add.", size: 14),
],
),
),
SizedBox(height: 16),
SingleChildScrollView(
child: GridView.count(
primary: true,
shrinkWrap: true,
crossAxisCount: 2,
crossAxisSpacing: 40.0,
mainAxisSpacing: 40.0,
padding: const EdgeInsets.all(20.0),
children: _items,
),
),
],
),
),
);
}
}
You need to use a combination of Stack, ListView and Column widgets. Here's how to do it:
return Scaffold(
body: SafeArea(
child: Stack(
children: [
SizedBox(height: 16),
ListView(
shrinkWrap: true,
children: [
GridView.count(
primary: true,
shrinkWrap: true,
crossAxisCount: 2,
crossAxisSpacing: 40.0,
mainAxisSpacing: 40.0,
padding: const EdgeInsets.all(20.0),
children: [
Container(
margin: EdgeInsets.all(20),
color: Colors.red,
height: 200,
width: 200,
),
Container(
margin: EdgeInsets.all(20),
color: Colors.amber,
height: 200,
width: 200,
),
Container(
margin: EdgeInsets.all(20),
color: Colors.blue,
height: 200,
width: 200,
),
Container(
margin: EdgeInsets.all(20),
color: Colors.green,
height: 200,
width: 200,
),
],
),
],
),
Container(
color: Colors.white,
height: MediaQuery.of(context).size.height * 0.2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Hello, Guest!"),
Icon(
Icons.ac_unit,
color: Colors.blue,
size: 32.0,
),
],
),
SizedBox(height: 14),
Text("What worries you now? Select or Add."),
],
),
),
],
),
),
);
Related
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
The same 2 errors appear in 2 different files when I run my flutter program, does anyone know how to fix this? this is my code
code for list restaurant.dart
import 'package:flutter/material.dart';
import 'package:resto_app/data/model/restaurant_list.dart';
import 'package:resto_app/ui/detail_page.dart';
class CardRestaurant extends StatelessWidget {
final Restaurant restaurant;
const CardRestaurant({Key? key, required this.restaurant}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return RestaurantDetailPage(idrestaurant: restaurant.id);
}));
},
child: Stack(
children: <Widget>[
Container(
margin:
const EdgeInsets.only(left: 40, top: 5, right: 20, bottom: 5),
height: 170.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.lime.shade100,
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: const EdgeInsets.only(
left: 200, top: 20, right: 20, bottom: 20),
child: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
width: 120.0,
child: Expanded(child: Text(restaurant.name)),
),
_sizebox(10),
Row(
children: [
_icon(Icons.star_rate, 20, Colors.yellow),
Text(
' ${restaurant.rating}',
),
],
)
],
),
),
),
),
Positioned(
left: 20.0,
top: 15.0,
bottom: 15.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Hero(
tag: restaurant.pictureId,
child: Image.network(
"https://restaurant-api.dicoding.dev/images/small/" +
restaurant.pictureId,
width: 200,
fit: BoxFit.cover,
),
),
),
),
],
),
),
);
}
}
Widget _sizebox(double height) {
return SizedBox(
height: height,
);
}
Widget _icon(IconData icon, double size, Color color) {
return Icon(
icon,
size: size,
color: color,
);
}
and my detailrestaurant.dart code
`
`import 'package:flutter/material.dart';
import 'package:resto_app/data/model/restaurants_detail.dart';
import 'package:resto_app/common/constant.dart';
class RestaurantDetail extends StatelessWidget {
static const routeName = '/restaurant_detail';
final Restaurant restaurant;
const RestaurantDetail({Key? key, required this.restaurant})
: super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Hero(
tag: restaurant.pictureId,
child: Image.network(
"https://restaurant-api.dicoding.dev/images/medium/" +
restaurant.pictureId,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(restaurant.name),
_sizebox(10),
Row(
children: [
_icon(Icons.location_city, 20, Colors.grey),
const SizedBox(
width: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(restaurant.city),
Text(restaurant.address),
],
)
],
),
],
),
Row(
children: [
_icon(Icons.star_rate, 20, Colors.yellow),
Text(
' ${restaurant.rating}',
),
],
)
],
),
),
_barrierContent(),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding:
const EdgeInsets.only(top: 10, right: 20, left: 20),
child: Text('Description'),
),
Container(
padding: const EdgeInsets.only(
top: 10, right: 20, left: 20, bottom: 20),
width: double.infinity,
child: Text(restaurant.description)),
],
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 20),
child: Column(
children: [
Text('Category'),
ListBody(
children: restaurant.categories.map((food) {
return Text(
'- ${food.name}',
);
}).toList(),
),
],
),
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 20),
child: Column(
children: [
Text('Menu Food'),
ListBody(
children: restaurant.menus.foods.map((categori) {
return Text(
'- ${categori.name}',
);
}).toList(),
),
],
),
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 20),
child: Column(
children: [
Text('Menu Drink'),
ListBody(
children: restaurant.menus.drinks.map((drink) {
return Text('- ${drink.name}');
}).toList(),
),
],
),
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 10),
child: Column(
children: [
Text('Comment'),
ListBody(
children: restaurant.customerReviews.map((review) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Expanded(
child: Row(children: [
Container(
width: 40,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue),
child: Center(
child: Text(
review.name.characters.elementAt(0),
style: const TextStyle(
color: Colors.white, fontSize: 20),
)),
),
const SizedBox(
width: 15,
),
Expanded(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
review.name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold),
),
Text(
' ${review.date}',
style: TextStyle(
fontSize: 14,
color: Colors.grey.shade500),
)
],
),
SizedBox(
width: 270,
child: Text(
review.review,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
)
],
),
)
]),
),
);
}).toList(),
),
],
),
),
_barrierContent()
],
),
],
),
),
),
);
}
}
Widget _barrierContent() {
return Container(
height: 10,
color: Colors.grey.shade200,
);
}
Widget _sizebox(double height) {
return SizedBox(
height: height,
);
}
Widget _icon(IconData icon, double size, Color color) {
return Icon(
icon,
size: size,
color: color,
);
}
`
``
error
The same 2 errors appear in 2 different files when I run my flutter program, does anyone know how to fix this?
child: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
width: 120.0,
child: Expanded(child: Text(restaurant.name)),
),
parent of Expanded must be Stack, Column, Row but in your example it's wrong
You should use Expanded only within a Column, Row or Flex & that to its top most child only
Here you have use Expanded widget inside padding widget which is inside stack and also you can't use Expanded inside stack, so it causing this ParentDataWidget Error
code for list restaurant.dart
class CardRestaurant extends StatelessWidget {
final Restaurant restaurant;
const CardRestaurant({Key? key, required this.restaurant}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: InkWell(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return RestaurantDetailPage(idrestaurant: restaurant.id);
}));
},
child: Stack( //---------------
children: <Widget>[
Container(
margin:
const EdgeInsets.only(left: 40, top: 5, right: 20, bottom: 5),
height: 170.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.lime.shade100,
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(//---------
padding: const EdgeInsets.only(
left: 200, top: 20, right: 20, bottom: 20),
child: Expanded(//--------- you can't use expanded inside Stack
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
width: 120.0,
child: Expanded(child: Text(restaurant.name)),
),
_sizebox(10),
and my detailrestaurant.dart code
class RestaurantDetail extends StatelessWidget {
static const routeName = '/restaurant_detail';
final Restaurant restaurant;
const RestaurantDetail({Key? key, required this.restaurant})
: super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Hero(
tag: restaurant.pictureId,
child: Image.network(
"https://restaurant-api.dicoding.dev/images/medium/" +
restaurant.pictureId,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(restaurant.name),
_sizebox(10),
Row(
children: [
_icon(Icons.location_city, 20, Colors.grey),
const SizedBox(
width: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(restaurant.city),
Text(restaurant.address),
],
)
],
),
],
),
Row(
children: [
_icon(Icons.star_rate, 20, Colors.yellow),
Text(
' ${restaurant.rating}',
),
],
)
],
),
),
_barrierContent(),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding:
const EdgeInsets.only(top: 10, right: 20, left: 20),
child: Text('Description'),
),
Container(
padding: const EdgeInsets.only(
top: 10, right: 20, left: 20, bottom: 20),
width: double.infinity,
child: Text(restaurant.description)),
],
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 20),
child: Column(
children: [
Text('Category'),
ListBody(
children: restaurant.categories.map((food) {
return Text(
'- ${food.name}',
);
}).toList(),
),
],
),
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 20),
child: Column(
children: [
Text('Menu Food'),
ListBody(
children: restaurant.menus.foods.map((categori) {
return Text(
'- ${categori.name}',
);
}).toList(),
),
],
),
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 20),
child: Column(
children: [
Text('Menu Drink'),
ListBody(
children: restaurant.menus.drinks.map((drink) {
return Text('- ${drink.name}');
}).toList(),
),
],
),
),
_barrierContent(),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 10),
child: Column(
children: [
Text('Comment'),
ListBody(//--- this is top most child
children: restaurant.customerReviews.map((review) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Expanded(//--------- This causing problem here, you can only use Exapnded to column top most child
child: Row(children: [
Container(
width: 40,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue),
child: Center(
child: Text(
review.name.characters.elementAt(0),
style: const TextStyle(
color: Colors.white, fontSize: 20),
)),
),
const SizedBox(
width: 15,
),
Expanded(//---- This one also causing Problem
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
review.name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold),
),
Text(
' ${review.date}',
style: TextStyle(
fontSize: 14,
color: Colors.grey.shade500),
)
],
),
SizedBox(
width: 270,
child: Text(
review.review,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
)
],
),
)
]),
),
);
}).toList(),
),
],
),
),
_barrierContent()
],
),
],
),
),
),
);
}
}
Keep this point in mind to avoid basic error
Most common one Expanded can only use to a descendant of Column,Row & Flex
Don't use Positioned under Row or Column instead use Align
Under ListView don't use Spacer instead use SizedBox
Hope this might help you to solve your Error
Happy Learning!!!
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: [ .....
How to increase Container height dynamically based on content in card widget inside ListView.builder. I want the container to increase its height based on content but problem is scrollDirection: Axis.horizontal, inside listview.builder. How to solve this issue.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
return Container(
height: height * 0.200, // Here i have to put some height to show card
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: 2,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
child: Card(
elevation: 0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.only(top: 5.0, right: 5.0),
child: Row(
children: [
Icon(
Icons.near_me,
size: 10.0,
),
Text(
'1 km',
style: TextStyle(fontSize: 10.0),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 5.0),
child: Container(
height: 80.0,
width: 80.0,
),
),
Padding(
padding: const EdgeInsets.only(
left: 10.0, right: 35.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: width * 0.40,
child: Text(
'test',
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: TextStyle(fontSize: 18.0),
),
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child: Row(
children: [
Text(
'test',
style: TextStyle(fontSize: 10.5),
)
],
),
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child: Row(
children: [
Icon(
Icons.location_on_outlined,
size: 12.0,
),
Container(
width: width * 0.40,
child: Text(
'test',
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: TextStyle(fontSize: 11.0),
),
),
],
),
)
],
),
),
],
),
),
],
)),
);
}),
);
}
}
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),
),
),
);
},
),
),
),
),
],
)
],
),
);