Flutter GridView.builder how to put children symmetric - flutter

i use GridView.builder to build my catalog and my catalog looks bad, how can i put my grid symmetrically :
i want my catalog to look like this :
it is my GridView.builder code:
Widget buildCatalog(List<Product> product) => GridView.builder(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 300,
childAspectRatio: 3 / 5,
crossAxisSpacing: 5,
mainAxisSpacing: 10),
itemCount: product.length,
itemBuilder: (context, index) {
final productItem = product[index];
final media = product[index].media?.map((e) => e.toJson()).toList();
final photo = media?[0]['links']['local']['thumbnails']['350'];
return Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 10),
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 10),
decoration: BoxDecoration(color: Colors.white,
borderRadius: BorderRadius.circular(20)),
child: Column(
children: <Widget>[
InkWell(
child: Container(
margin: EdgeInsets.all(8),
child:
// Image.network(productItem.media),
Image.network(media != null ? 'https://cdn.yiwumart.org/${photo}' : 'https://yiwumart.org/images/shop/products/no-image-ru.jpg'),
),
),
SizedBox(height: 10,),
Text(productItem.name, textAlign: TextAlign.center, style: TextStyle(fontSize: 20),),
Container(
width: MediaQuery.of(context).size.width * 0.8,
child: ElevatedButton(onPressed: () {}, child: Text('5000 тг')))
],
),
);
},
);

Try this:
Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 10),
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 10),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(20)),
child: Column(
children: <Widget>[
InkWell(
child: Container(
margin: EdgeInsets.all(8),
child:
// Image.network(productItem.media),
Image.network(media != null
? 'https://cdn.yiwumart.org/${photo}'
: 'https://yiwumart.org/images/shop/products/no-image-ru.jpg'),
),
),
SizedBox(
height: 10,
),
Text(
productItem.name,
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20),
),
Spacer(),
Container(
width: MediaQuery.of(context).size.width * 0.8,
child: ElevatedButton(
onPressed: () {}, child: Text('5000 тг')))
],
),
),

Related

How to set children heights in GridView to the maximum height among children?

I'd like to set the heights of the children of a GridView to the maximum height among all the children. I don't want to type a fixed value in childrenAspectRatio or in mainAxisExtent, I need the heights to adapt to the biggest child.
Code:
GridView.builder(
shrinkWrap: true,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisExtent: 256, // don't want to set this because could cause overflow or waste of blank space
),
itemCount: prizes.length,
itemBuilder: (context, index){
return Card(
elevation: 3.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Stack(
children: [
Image.asset(prizes[index]["image"]),
]
),
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Text(
prizes[index]["name"],
style: const TextStyle(fontWeight: FontWeight.bold, ),
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
prizes[index]["cost"].toString(),
style: const TextStyle(fontWeight: FontWeight.normal),
),
Padding(
padding: const EdgeInsets.only(left : 4.0),
child: SizedBox(
width: 14,
height: 14,
child: svg),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
)
)
),
onPressed: () async {
},
child: Padding(
padding: EdgeInsets.all(15.0),
child: Text(AppLocalizations.of(context)!.riscuoti,
style: TextStyle(
fontSize: 16
),
),
),
),
),
],
),
),
);
},
),

Another exception was thrown: RenderBox was not laid out: RenderFlex#6ac69

I want to display list and the data come from API. And I got error
Another exception was thrown: RenderBox was not laid out: RenderPadding#64692 relayoutBoundary=up5
NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I don't know what kind of error is this..
Bellow is my issue code
return AlertDialog(
insetPadding: EdgeInsets.only(bottom: 520),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
title: Container(
color: Color.fromARGB(255, 253, 253, 253),
child: Text('Comment', style: TextStyle(color: Color.fromARGB(255, 4, 4, 4))),
padding: const EdgeInsets.all(17),
margin: const EdgeInsets.all(0),
),
titlePadding: const EdgeInsets.all(0),
content: SingleChildScrollView(
child: Column(mainAxisSize: MainAxisSize.min, children: < Widget > [
GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
),
itemCount: commentList.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(2.0),
child: Container(
child: InkWell(
child: Container(
padding: EdgeInsets.only(bottom: 10, top: 10, left: 20, right: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Color(0xffD4D4D4), width: 2.0)),
child: Column(
children: < Widget > [
SizedBox(height: 15),
Expanded(
child: Text(
"test",
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 15, fontWeight: FontWeight.normal),
))
],
)),
)),
);
},
),
])),
);
The SingleChildScrollView can only have one child, and the GridView expands to fill the available space.
To fix the problem, wrap your GridView with a SizedBox and give it some height/width:
AlertDialog(
insetPadding: EdgeInsets.only(bottom: 520),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
title: Container(
color: Color.fromARGB(255, 253, 253, 253),
child: Text('Comment',
style: TextStyle(color: Color.fromARGB(255, 4, 4, 4))),
padding: const EdgeInsets.all(17),
margin: const EdgeInsets.all(0),
),
titlePadding: const EdgeInsets.all(0),
content: SingleChildScrollView(
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
// <--- Add SizedBox here
SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.5,
child: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
),
itemCount: commentList.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(2.0),
child: Container(
child: InkWell(
child: Container(
padding: EdgeInsets.only(
bottom: 10, top: 10, left: 20, right: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border:
Border.all(color: Color(0xffD4D4D4), width: 2.0)),
child: Column(
children: <Widget>[
SizedBox(height: 15),
Expanded(
child: Text(
"test",
textAlign: TextAlign.justify,
style: TextStyle(
fontSize: 15, fontWeight: FontWeight.normal),
))
],
)),
)),
);
},
),
),
])),
);

Flutter - RangeError (index)

I am getting "RangeError (index): Invalid value: Not in inclusive range 0..4: 5" error in my flutter app.
Error
This is my code and I hope somebody is able to help me fixing the error:
import 'package:fluttertravelapp/models/beach_model.dart';
import 'package:fluttertravelapp/models/popular_model.dart';
import 'package:fluttertravelapp/screens/selected_place_screen.dart';
import 'package:fluttertravelapp/widgets/bottom_navigation_bar.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluttertravelapp/models/recommended_model.dart';
import 'package:fluttertravelapp/widgets/custom_tab_indicator.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:cached_network_image/cached_network_image.dart';
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
// Page Controller
final _pageController = PageController(viewportFraction: 0.877);
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBarTravel(),
body: SafeArea(
child: Container(
child: ListView(
physics: BouncingScrollPhysics(),
// Custom Navigation Drawer and Search Button
children: [
Container(
height: 57.6,
margin: EdgeInsets.only(top: 28.8, left: 28.8, right: 28.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
height: 57.6,
width: 57.6,
padding: EdgeInsets.all(18),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
color: Color(0x080a0928),
),
child: SvgPicture.asset('assets/svg/icon_drawer.svg'),
),
Container(
height: 57.6,
width: 57.6,
padding: EdgeInsets.all(18),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
color: Color(0x080a0928),
),
child: SvgPicture.asset('assets/svg/icon_search.svg'),
),
],
),
),
// Text Widget for Title
Padding(
padding: EdgeInsets.only(top: 48, left: 28.8),
child: Text('Explore\nthe Nature', style: GoogleFonts.playfairDisplay(
fontSize: 45.6,
fontWeight: FontWeight.w700,
),),
),
//Custom Tab bar with Custom Indicator
Container(
height: 30,
margin: EdgeInsets.only(left: 14.4, top: 28.8),
child: DefaultTabController(
length: 4,
child: TabBar(labelPadding: EdgeInsets.only(left: 14.4, right: 14.4),
indicatorPadding: EdgeInsets.only(left: 14.4, right: 14.4),
isScrollable: true,
labelColor: Color(0xFF000000),
unselectedLabelColor: Color(0xFF8a8a8a),
labelStyle: GoogleFonts.lato(
fontSize: 14,
fontWeight: FontWeight.w700,
),
unselectedLabelStyle: GoogleFonts.lato(
fontSize: 14,
fontWeight: FontWeight.w700,
),
indicator: RoundedRectangleTabIndicator(
color: Color(0xFF000000), weight: 2.4, width: 14.4
),
tabs: [
Tab(
child: Container(
child: Text('Recommended'),
),
),
Tab(
child: Container(
child: Text('Popular'),
),
),
Tab(
child: Container(
child: Text('New Destination'),
),
),
Tab(
child: Container(
child: Text('Hidden Gems'),
),
),
],),),
),
//ListView Widget with PageView
//Recommendations section
Container(
height: 218.4,
margin: EdgeInsets.only(top: 16,),
child: PageView(
physics: BouncingScrollPhysics(),
controller: _pageController,
scrollDirection: Axis.horizontal,
children: List.generate(
recommendations.length,
(int index) => GestureDetector(
onTap: (){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SelectedPlaceScreen(
recommendedModel: recommendations[index])));
},
child: Container(
margin: EdgeInsets.only(right: 28.8),
width: 333.6,
height: 218.4,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
image: DecorationImage(
fit: BoxFit.cover,
image: CachedNetworkImageProvider(recommendations[index].image),
),
),
child: Stack(
children: [
Positioned(
bottom: 19.2,
left: 19.2,
child: ClipRRect(
borderRadius: BorderRadius.circular(4.8),
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaY: 19.2, sigmaX: 19.2),
child: Container(
height: 36,
padding: EdgeInsets.only(
left: 16.72, right: 14.4),
alignment: Alignment.centerLeft,
child: Row(
children: [
SvgPicture.asset('assets/svg/icon_location.svg'),
SizedBox(width: 9.52,),
Text(recommendations[index].name,
style: GoogleFonts.lato(
fontWeight: FontWeight.w700,
color: Colors.white,
fontSize: 16.8,),
),
],
),
),
),
),
)
],
),
),
)),
),
),
//Dots Indicator
//Using SmoothPageIndicator Library
Padding(padding: EdgeInsets.only(left: 28.8, top: 28.8),
child: SmoothPageIndicator(
controller: _pageController,
count: recommendations.length,
effect: ExpandingDotsEffect(
activeDotColor: Color(0xFF8a8a8a),
dotColor: Color(0xFFabababab),
dotHeight: 4.8,
dotWidth: 6,
spacing: 4.8,
),
),
),
//Text Widget for Popular Categories
Padding(
padding: EdgeInsets.only(top: 48, left: 28.8, right: 28.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Popular Categories',
style: GoogleFonts.playfairDisplay(
fontSize: 28,
fontWeight: FontWeight.w700,
color: Color(0xFF000000),
),
),
Text(
'Show All',
style: GoogleFonts.lato(
fontSize: 16.8,
fontWeight: FontWeight.w500,
color: Color(0xFF8a8a8a),
),
),
],
),
),
//ListView for Popular Categories Section
Container(
margin: EdgeInsets.only(top: 33.6,),
height: 45.6,
child: ListView.builder(
itemCount: populars.length,
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.only(left: 28.8, right: 9.6),
itemBuilder: (context, index){
return Container(
margin: EdgeInsets.only(right: 19.2),
height: 45.6,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
color: Color(populars[index].color),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(width: 19.2,),
Image.asset(populars[index].image,height: 16.8,),
SizedBox(width: 19.2,),
],
),
);
},
),
),
//ListView for Beach Section
Container(
margin: EdgeInsets.only(top: 28.8, bottom: 16.8),
height: 124.8,
child: ListView.builder(
itemCount: beaches.length,
padding: EdgeInsets.only(left: 28.8, right: 12),
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index){
return Container(
height: 124,
width: 188.4,
margin: EdgeInsets.only(right: 16.8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9.6),
image: DecorationImage(
fit: BoxFit.cover,
image: CachedNetworkImageProvider(beaches[index].image),
)
),
);
},
),
)
],
),
),
),
);
}
}
If you want the full code you can see the code from https://github.com/abdulazizahwan/flutter_travel_app
I tried the same code.

gridview builder with random width of its items in flutter

i need to show something like this
can some one help on this?
I can able to achieve following out put
i am using following code ::
GridView.builder(
scrollDirection: Axis.vertical,
physics: ScrollPhysics(),
shrinkWrap: true,
itemCount: 6,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 4),),
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
child: Container(
padding: EdgeInsets.all(0.0),
margin: EdgeInsets.only(
left: 10.0, right: 10.0, top: 10.0, bottom: 10.0),
decoration: BoxDecoration(
border: Border.all(color: Color(0xFF282f61),width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(
14.0) // <--- border radius here
),
),
child: Center(
child: Text(
'Avg-project',
style: TextStyle(
color: Color(0xFF17b01b),
fontSize: 14.0,
),
),
),
),
onTap: () {},
);
},
))
how can i achieve gridview with random width in flutter?
gridview is not support random width so instead of gridview you can use wrap widget to achieve your desired output.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: BackAppBar("Notifications", context),
body: Wrap(
children: [
"Help Moviing",
"Furniture Assembly",
"Mounting",
"Home Repairs",
"Personal Assistant",
"Delivery",
"Hard Work",
"Practice & Unpacking",
"Painting",
"Cleaning",
"Heavy Liffing"
].map((f) => GestureDetector(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
margin: EdgeInsets.only(
left: 5.0, right: 5.0, top: 10.0, bottom: 10.0),
decoration: BoxDecoration(
border: Border.all(color: Color(0xFF282f61), width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(
10.0) // <--- border radius here
),
),
child: Text(f,
style: TextStyle(
color: Color(0xFF17b01b),
fontSize: 16.0,
),
),
),
onTap: () {},
))
.toList(),
),
);
}

space between items in gridview flutter

I have a gridview with 6 items (cards with images and text). I want those 6 items to fit me on the screen but the gridview leaves all the space I can between items by jumping 2 items off the screen.
I leave a picture of what I want and what I have.
Thank you.
return MaterialApp(
home: Scaffold(
body: Container(
margin: EdgeInsets.only(top: 0),
child: Stack(
alignment: AlignmentDirectional.topCenter,
children: <Widget>[
Container(
height: 200,
color: Colors.black,
),
Container(
margin: EdgeInsets.only(top: 200),
decoration: BoxDecoration(
image: new DecorationImage(
image: AssetImage("assets/fondo.png"), fit: BoxFit.fill)),
),
Image.asset("assets/cara_simio_banner.png"),
Padding(
padding: EdgeInsets.only(top: 220),
child: Text(
"{CodeJaVu}",
style: TextStyle(
color: Colors.white,
fontSize: 25,
fontWeight: FontWeight.bold),
),
),
Container(
margin: EdgeInsets.only(top: 230),
child: GridView.count(
crossAxisCount: 2,
children: items.map((item) {
return GestureDetector(
onTap: () {},
child: CardItem(item),
);
}).toList(),
),
)
],
),
)));
}
Widget CardItem(Item item) {
return Card(
margin: EdgeInsets.all(40),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child:
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.asset(
item.image,
width: 50,
height: 50,
),
Text(item.name)
],
)
);
}
class Item {
String _name;
String _image;
Item(this._name, this._image);
String get name => _name;
set name(String name) => _name = name;
String get image => _image;
set image(String image) => _image = image;
}
what I have,
what I want,
add childAspectRatio parametr to GridView.cout counstructor,
and change Card margin.
I've used:
..
GridView.count(
childAspectRatio: 3/2,
..
Card(
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
..
You can also use mainAxisSpacing and crossAxisSpacing like below.
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, crossAxisSpacing: 8, mainAxisSpacing: 4),
padding: EdgeInsets.all(8),
shrinkWrap: true,
itemCount: widgetList.length,
itemBuilder: (context, index) {
return widgetList[index];
},
);
Just add these 2 lines in your GridView widgets:
mainAxisSpacing: 20,
crossAxisSpacing: 20,
children:
space.photos.map((item) {
return Container(
margin: EdgeInsets.only(
left: 24,
),
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image.network(
item,
width: 110,
height: 88,
fit: BoxFit.cover,
),
),
);
}).toList()