How to increase Card size inside GridView - flutter

Here is my code:
GridView.count(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
crossAxisCount: 3,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
primary: false,
shrinkWrap: true,
children: tempSearchStore.map((element) {
return InkWell(
onTap: (){
print(element['ID'].toString());
Navigator.push(context, MaterialPageRoute(
builder: (context) => SearchResultScreen(ID: element['ID'],Pin:widget.pin),
));
},
child: Container(
height: 20,
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
elevation: 5.0,
child: Container(
height: 20,
child: Center(
child: Text(element['Name'],
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
),
)
)
)
),
),
);
}).toList()
)
I want to change the height and width of my card but I'm not able to do so, even I also tried wrapping Card into Container and give height and width but failed. Only I'm able to change by changing the value of CrossAxisCount in GridView and that also changes according to itself.
Here is my Image:
I want it's height to be more and width is Okay.

The key is childAspectRatio. This value is used to determine the layout in GridView. In order to get the desired aspect, you have to set it to the (itemWidth / itemHeight).
GridView.count(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
crossAxisCount: 3,
childAspectRatio: (itemWidth / itemHeight),
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
primary: false,
shrinkWrap: true,
children: tempSearchStore.map((element) {
return InkWell(
onTap: (){
print(element['ID'].toString());
Navigator.push(context, MaterialPageRoute(
builder: (context) => SearchResultScreen(ID: element['ID'],Pin:widget.pin),
));
},
child: Container(
height: 20,
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
elevation: 5.0,
child: Container(
height: 20,
child: Center(
child: Text(element['Name'],
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
),
)
)
)
),
),
);
}).toList()
)
and if this doesn't work for you look at this package: https://pub.dartlang.org/packages/flutter_staggered_grid_view

Related

Null check operator use on a null value

class _HomePageState extends State<HomePage> {
const SizedBox(height: 10),
Flexible(
child: StreamBuilder( //here the error i'm not so sure
stream: ref.snapshots(),
builder: (context,AsyncSnapshot<QuerySnapshot> snapshot){
if(snapshot.hasError){
return Center(
child: Text("Error:${snapshot.error}"),
);
}
return Scrollbar(
child: GridView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 15,
crossAxisSpacing: 15,
childAspectRatio: 2/3,
),
itemCount: snapshot.data!.docs.length,
itemBuilder: (context,index) {
final DocumentSnapshot documentSnapshot = snapshot.data!.docs[index];
return Card(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
elevation: 0,
child: InkWell(
onTap: (){
Navigator.push(context, PageTransition(
type:PageTransitionType.leftToRight,
child: DetailPage(
image:documentSnapshot['image'], category:documentSnapshot['category'], name:documentSnapshot['name'], description:documentSnapshot['description'], price:documentSnapshot['price'],
)));
},
child: Container(
margin: const EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Ink.image(image: NetworkImage(documentSnapshot['image'][0]),
height: 90,
width: double.infinity,),
const SizedBox(height: 5,),
RichText(
textAlign: TextAlign.start,
text: TextSpan(
text: documentSnapshot['category'],
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 12.0)),
),
i used this code to display product on the homepage but it just glimpse appear the error of null value but i still can run and display the product then how i should fix the code. Logic error.
This is my full code of the streamBuilder

GridView Flutter

i'm trying to do this page
but what i get is this
this is my code
SingleChildScrollView(
child: Column(
children: [
Center(
child: Container(
margin: EdgeInsets.all(20),
width: 230,
height: 40,
child: ElevatedButton(
onPressed: () {
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Color(0xff28CC61)),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
),
),
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text("Add a book", style: TextStyle(
fontSize: 20
),),
),
),
),
),
Container(
child: Text("Explore between 10 books", style: TextStyle(
fontSize: 20
),),
),
Container(
//adding: EdgeInsets.all(12),
child: SingleChildScrollView(
child: GridView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 1,
crossAxisSpacing: 20,
crossAxisCount: 2,
mainAxisSpacing: 8),
itemCount: books.length,
itemBuilder: (BuildContext ctx, index) {
return Container(
width: 50,
height: 100,
child: Card(
color: Color(0xFFEDEDED),
child: Column(
children: [
Image.asset(books[index].image, height: 150
,
width: 150,fit: BoxFit.cover,),
],
),
),
);
}),
),
),
],
),
),
ignore the colors and styles i want how to do the page
i stucked on gridview and how to write text below the picture + How can I fix my code to do it? So each image has same size as others? i tried to see the grid view documentation and i could not find anything.
Change childAspectRatio : 1 to childAspectRatio:0.7. childaspectRatio defines the ratio between width and height of children. If you set it as 1 you will get a square shaped widget. More than 1 is horizontal rectangle and less than 1 gives vertical rectangle.

flutter charts: Resize charts when resize the browser screen

I'm a new flutter developer and I am using fl_chart to make some dashboards.
My charts are well displayed with the web window on full screen (screen 2) , and once I try to resize my window, at a certain time, pie charts become bigger than the parent container (screen 1)
Any idea how should I solve the issue ?
Expanded(
child: Scaffold(
appBar: AppBar(
automaticallyImplyLeading:
MediaQuery.of(context).size.width < 600,
leading: IconButton(
color: Color(0xff2A3F54),
icon: Icon(FontAwesomeIcons.chartLine),
onPressed: () {
//
}),
title: Text('Dashboard 2022', style: TextStyle(
color: Color(0xff2A3F54)
)),
),
body: Container(
padding: EdgeInsets.all(12.0),
child: GridView.builder(
itemCount: 4,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
childAspectRatio: 2,
),
itemBuilder: (BuildContext context, int index){
return Padding(
padding: const EdgeInsets.all(12.0),
child: _pages[index],
);
},
),
),
),
),
piechart.dart build method :
Widget build(BuildContext context) {
return FutureBuilder(
future: _getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Card(
color: Colors.white,
child: Column(
children: <Widget>[
SizedBox(
width: double.infinity,
child: Container(
padding:
EdgeInsets.only(left: 20.0, top: 8.0, bottom: 8.0),
child: //(this.title!= null) ?
Text(
getTitle(),
style:
TextStyle(color: Color(0xff2A3F54), fontSize: 16),
textAlign: TextAlign.left,
),
),
),
const Divider(
height: 4,
thickness: 1,
indent: 20,
endIndent: 20,
color: Color(0xffB0BEC5),
),
Expanded(
child: Row(children: <Widget>[
Expanded(
child: PieChart(
PieChartData(
startDegreeOffset: 180,
borderData: FlBorderData(
show: false,
),
sectionsSpace: 1,
centerSpaceRadius: 0,
sections: showingSections(snapshot.data)),
),
),
const SizedBox(
width: 40,
),
showingPieChartIndicator(snapshot.data),
]),
)
],
),
//),
);
} else
return Center(child: CircularProgressIndicator());
});
}

Could not load Images in GridView builder()

i am beginner in Flutter , i am trying to create a gridview builder with a card that contains an image, the gridview. builder in the bottom works fine but the gridview builder on the top do not load any image, the difference of the both gridviews is that the one that have problem : the image is inside a card,
I do not have any idea why the second works and the one on the top do not do that even they use the same assets
the error i am getting is about Invalid argument(s): No host specified in URI for every asset
import 'package:flutter/material.dart';
import 'package:footyappp/clubs/Clubs.dart';
import 'package:footyappp/results/results.dart';
import 'package:footyappp/schedules/schedules.dart';
import 'package:footyappp/stats/player_goals.dart';
import 'package:footyappp/tables%20copy/league_board.dart';
import 'package:footyappp/stats/player_assists.dart';
import 'package:footyappp/stats/player_red_cards.dart';
import 'package:footyappp/stats/player_yellow_cards.dart';
class Statss extends StatelessWidget {
List<String> images = [
"Assets/tt.jpg",
"Assets/qatarairways.jpg",
"Assets/LOGO_Danao.jpg",
"Assets/delice.jpg"
];
List<String> menu = [
"Assets/tt.jpg",
"Assets/qatarairways.jpg",
"Assets/LOGO_Danao.jpg",
"Assets/delice.jpg"
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Stats"),
backgroundColor: Colors.blue[300],
elevation: 0.0,
),
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.purple, Colors.blue])),
child: ListView(
children: [
SizedBox(
height: 20,
),
Container(
margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
child: Text(
"Statistiques",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.w900,
color: Colors.white),
),
),
SizedBox(
height: 30,
),
Container(
padding: EdgeInsets.all(12.0),
child: GridView.builder(
shrinkWrap: true,
itemCount: menu.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 1.0,
mainAxisSpacing: 1.0),
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 4.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child:
Column(
children: <Widget>[
Container(
height: 60,
width: 60,
child:
Image.network(menu[index]),
),
const SizedBox(width:10.0),
],
),
),
);
},
),
),
Container(
margin: EdgeInsets.fromLTRB(15, 30, 15, 0),
child: Column(
children: [
SizedBox(
height: 1,
),
ButtonTheme(
minWidth: double.infinity,
height: 40,
child: RaisedButton(
child: Align(
child: Text(
"Top Scorers",
style: TextStyle(fontSize: 17),
textAlign: TextAlign.right,
),
alignment: Alignment.centerLeft,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Scorers()),
);
},
color: Colors.grey.shade300,
textColor: Colors.black,
padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
splashColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0))),
),
SizedBox(
height: 1,
),
ButtonTheme(
minWidth: double.infinity,
height: 40,
child: RaisedButton(
child: Align(
child: Text(
"Top Assists",
style: TextStyle(fontSize: 17),
textAlign: TextAlign.right,
),
alignment: Alignment.centerLeft,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Assists()),
);
},
color: Colors.grey.shade300,
textColor: Colors.black,
padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
splashColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0))),
),
SizedBox(
height: 1,
),
ButtonTheme(
minWidth: double.infinity,
height: 40,
child: RaisedButton(
child: Align(
child: Text(
"Yellow Cards",
style: TextStyle(fontSize: 17),
textAlign: TextAlign.right,
),
alignment: Alignment.centerLeft,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => YellowCards()),
);
},
color: Colors.grey.shade300,
textColor: Colors.black,
padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
splashColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0))),
),
SizedBox(
height: 1,
),
ButtonTheme(
minWidth: double.infinity,
height: 40,
child: RaisedButton(
child: Align(
child: Text(
"Red Cards",
style: TextStyle(fontSize: 17),
textAlign: TextAlign.right,
),
alignment: Alignment.centerLeft,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RedCards()),
);
},
color: Colors.grey.shade300,
textColor: Colors.black,
padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
splashColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0))),
),
SizedBox(
height: 30,
)
],
),
),
Container(
padding: EdgeInsets.all(12.0),
child: GridView.builder(
shrinkWrap: true,
itemCount: images.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0),
itemBuilder: (BuildContext context, int index) {
return Image.asset(images[index]);
},
),
),
SizedBox(
height: 30,
)
],
),
));
}
}
here how it looks:
i am tryin to make it works
This is because you are using Image.network(menu[index]) for the top gridview.
change it to
Image.asset(menu[index]), it'll work, because you are loading your images from your assets, and not anything from the network.

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(),
),
);
}