Flutter how to show array data in gridview? - flutter

I have a simple gridview and static which is working fine now i need to show data from array. I need to know how can i do this. By for loop i think it will not good but i am not sure the other way ?
My code
Container(
color: Color(0xffECF0F1),
child: GridView(
padding: EdgeInsets.all(0),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 1,
mainAxisSpacing: 1,
crossAxisCount: 4,
),
children: <Widget>[
],
),
),
In children i have data like this now
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => BookService1()),
);
},
child: Container(
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: Height * 0.04,
child: Image.asset('images/cat1.png'),
),
SizedBox(
height: Height * 0.008,
),
Text(
'Computer Hardware',
style: TextStyle(
fontFamily: 'UbuntuMedium',
fontSize: 10,
),
textAlign: TextAlign.center,
)
],
),
),
),
Now i need to show data from array not static my array look like this
[
{
"Name": "Computer Software",
"Image": "https://cdn.zeplin.io/5fa90ed1e66d311f086e0115/assets/75b908a5-60c0-4e9a-84a4-ae3ca65c6973.png"
},
{
"Name": "Computer Hardware",
"Image": "https://cdn.zeplin.io/5fa90ed1e66d311f086e0115/assets/75b908a5-60c0-4e9a-84a4-ae3ca65c6973.png"
}
]

You can use the GridView.builder widget
GridView.builder(
itemCount: myArray.length, // The length Of the array
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 0.6,
crossAxisSpacing: 4,
mainAxisSpacing: 4,
), // The size of the grid box
itemBuilder: (context, index) => Container(
child: Text(myArray[index].text),
),
);

I have edited your code so you can try like as this code.
GridView.builder(
shrinkWrap: true,
physics: ScrollPhysics(),
//scrollDirection: Axis.vertical,
itemCount:myArray.length, // Your array item length.
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 1,
mainAxisSpacing: 1,
crossAxisCount: 4,
),
itemBuilder: (context, index) {
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => BookService1()),
);
},
child: Container(
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: Height * 0.04,
child: Image.asset('images/cat1.png'),
),
SizedBox(
height: Height * 0.008,
),
Text(
myArray[index].name, // You can use like as
style: TextStyle(
fontFamily: 'UbuntuMedium',
fontSize: 10,
),
textAlign: TextAlign.center,
)
],
),
),
);
}),
I hope you are understand, So please let me know it's working or not.

Both above answers are correct but for clean code or the better way you can create a widget in which you can your data and show. So the benefit will be you can call that widget again so you can save code and also better structure.
Widget category:
import 'package:flutter/material.dart';
class CategoryBox extends StatefulWidget {
const CategoryBox({Key key, #required this.data}) : super(key: key);
final data;
#override
_CategoryBoxState createState() => _CategoryBoxState();
}
class _CategoryBoxState extends State<CategoryBox> {
#override
Widget build(BuildContext context) {
print(widget.data);
double statusBarHeight = MediaQuery.of(context).padding.top;
double Height = MediaQuery.of(context).size.height;
double Width = MediaQuery.of(context).size.width;
return GestureDetector(
onTap: () {
},
child: Container(
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: Height * 0.04,
child: Image.network(widget.data['Image']),
),
SizedBox(
height: Height * 0.008,
),
Text(
widget.data['Name'],
style: TextStyle(
fontFamily: 'UbuntuMedium',
fontSize: 10,
),
textAlign: TextAlign.center,
)
],
),
),
);
}
}
your grid code
Container(
color: Color(0xffECF0F1),
child: GridView.builder(
itemCount: globalService
.globalServiceArray.length, // The length Of the array
padding: EdgeInsets.all(0),
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 1,
mainAxisSpacing: 1,
crossAxisCount: 4,
),
itemBuilder: (context, index) => Container(
child: CategoryBox(data: globalService.globalServiceArray[index]),
),
),
),
change your array I have just given name. Both above answer is correct also if you are using the widget multiple times so I think this way will be good. Happy Coding :)

Related

GridView infinite scrolling CircularProgressIndicator position

In my gridView there is a condition when to show CircularProgressIndicator(), but it is displayed incorrectly, in the gridView element, I need it to be displayed below the gridView. How can I show CircularProgressIndicator() correctly?
Widget buildCatalog(List<Product> product) => GridView.builder(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
// mainAxisExtent: ,
maxCrossAxisExtent: 300,
childAspectRatio: MediaQuery.of(context).size.width*1.22 /
(MediaQuery.of(context).size.height),
crossAxisSpacing: 10,
mainAxisSpacing: 10),
controller: sController,
itemCount: product.length + 1,
itemBuilder: (context, index) {
if (index < product.length) {
final media = product[index].media?.map((e) => e.toJson()).toList();
final photo = media?[0]['links']['local']['thumbnails']['350'];
final productItem = product[index];
return GestureDetector(
onTap: () {
},
child: Container(
child: Column(
children: <Widget>[
InkWell(
child: Container(
margin: EdgeInsets.only(top: 5),
child: Image.network(...)
),
),
),
SizedBox(
height: 10,
),
Text(
'${productItem.name}\n',
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 14),
),
// Spacer(),
SizedBox(height: 8),
Text('${productItem.price} ₸',
textAlign: TextAlign.start,
style: TextStyle(fontSize: 20, color: Colors.red)),
SizedBox(height: 5),
buildButton(index, productItem.id),
],
),
),
);
}
return const Center(child: CircularProgressIndicator(),);
},
);
wrap Gridview() and CircularProgressIndicator() into a column,
and change shrinkWarp and physics like below
Column(
children: [
Gridview.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: product.length + 1,
itemBuilder: (context, index) {
//return something you want
},
),
const CircularProgressIndicator()
],
)

Flutter Button Placed below GridView Builder

I want to have a big long button below the Gridview just like the logout button in the Roblox app
I manage to create a gridview and button using the Column , however the button is floating above the gridview, how to make it placed nicely below the gridview without floating above the gridview?
Here is my code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final title = 'Grid List';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: MainMenuBody(),
),
);
}
}
class MainMenuBody extends StatelessWidget {
#override
Widget build(BuildContext context) {
final List<MenuData> menu = [
MenuData(Icons.move_to_inbox_outlined, 'Menu 1'),
MenuData(Icons.find_in_page_outlined, 'Menu 2'),
MenuData(Icons.find_in_page_outlined, 'Menu 3'),
MenuData(Icons.upgrade_outlined, 'Menu 4'),
MenuData(Icons.upgrade_outlined, 'Menu 5'),
MenuData(Icons.play_for_work_outlined, 'Menu 6'),
MenuData(Icons.play_for_work_outlined, 'Menu 7'),
MenuData(Icons.assignment_turned_in_outlined, 'Menu 8'),
MenuData(Icons.assignment_turned_in_outlined, 'Menu 9'),
MenuData(Icons.fact_check_outlined, 'Menu 10')
];
return Container(
child: Scrollbar(
thickness: 3,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: SingleChildScrollView(
child: Column(
children: [
GridView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemCount: menu.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 1,
crossAxisCount: 2,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0),
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 0.2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0)),
child: InkWell(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
menu[index].icon,
size: 30,
),
SizedBox(height: 20),
Text(
menu[index].title,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 10,
color: Colors.black87),
)
],
),
),
);
},
),
Center(
child: RaisedButton(
child: Text("Button"),
onPressed: (){},
),
),
],
),
),
),
));
}
}
class MenuData {
MenuData(this.icon, this.title);
final IconData icon;
final String title;
}
I have modified your code a little bit. Please have a look.
Wrap the column inside a SingleChildScrollView, Remove Expanded widget and add shrinkWrap: true inside GridView
return Container(
child: Scrollbar(
thickness: 3,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Column(
children: [
GridView.builder(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemCount: menu.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 1,
crossAxisCount: 2,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0),
itemBuilder: (BuildContext context, int index) {
return Card(
elevation: 0.2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0)),
child: InkWell(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
menu[index].icon,
size: 30,
),
SizedBox(height: 20),
Text(
menu[index].title,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 10,
color: Colors.black87),
)
],
),
),
);
},
),
Center(
child: RaisedButton(
child: Text("Button"),
onPressed: (){},
),
),
],
),
),
),
));
Update: Not sure if this is in general the way to go, but you could create a scrollview wrapping the column wrapping gridview and the button. In grid view you then have to disable scrolling with physics: new NeverScrollableScrollPhysics().
But maybe columns and rows are the way to go for this

Problem adding widget to GridView with button and scroll

At first I had a Wrap that by clicking on the "add" button would add a new card to the Wrap. Then I changed the Wrap to GridView so that I could add scrolling. However, now when I press the button nothing shows on the device, but if I change GridView back to a Wrap and hot reload, the cards show up? I'm not sure what's wrong.
Also I can't scroll, I know I'm doing something wrong, but I have no idea what.
class MainScreen extends StatefulWidget {
#override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
List<Widget> cards = [];
dynamic addReusableCard (String activity, int goal, double elapsed){
return cards.add(ReusableCard(activityText: activity, activityLength: goal, activityTimeElapsed: elapsed,));
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text('TIME CARDS',
style: TextStyle(
fontSize: 20.0,
letterSpacing: 5.0,
),),
Text('You\'re awake for a total of 16h',
style: TextStyle(
fontSize: 16.0,
fontStyle: FontStyle.italic,
),),
Expanded(
flex: 9,
child: GridView.count(
crossAxisCount: 2,
childAspectRatio: (175.0 / 220.0),
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: ScrollPhysics(),
children: cards
),
),
Padding(
padding: const EdgeInsets.all(18.0),
child: Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton(
onPressed: (){
setState(() {
addReusableCard('reading', 2, 0.3);
});
},
backgroundColor: kAccentColor,
elevation: 0.0,
child: Icon(Icons.add),
),
),
),
],
),
),
),
);
}
}
Thanks in advance
For a dynamic list length you should use gridView.builder()
Here is the relevant code:
Expanded(
flex: 9,
child: GridView.builder(
itemCount: cards.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2
),
itemBuilder: (BuildContext context, int index) {
return cards[index];
}
)
),

Flutter Gridview or StaggeredGridview variable width /variable number of items to fill column

I am trying to display a gridview that has variable number and width buttons like this:
But what I have ended up with when trying gridview is a fixed number of buttons and fixed widths like this:
Initially I thought this was a problem with the buttons having too much padding by default but that is not the issue, I was able to fix that and still have the same issue with the grid.
I have tried Gridview builder like this:
GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, childAspectRatio: 3.5, mainAxisSpacing: 4, crossAxisSpacing: 4),
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () {
//Navigator.of(context).pushNamed(HomePageResultsScreen.id);
},
child: ButtonTheme(
minWidth: 16,
height: 30,
child: RaisedButton(
padding: EdgeInsets.all(8.0),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Colors.white,
child:
Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CachedNetworkImage(fit: BoxFit.contain,
height: 40,
width: 40,
placeholder: (context, url) => new CircularProgressIndicator(),
imageUrl: snapshot.data.documents[index]['icon'].toString(),
),
Text(snapshot.data.documents[index]['name'].toString(), textAlign: TextAlign.right, style: TextStyle(fontSize: 10, color: Colors.black,),),
],
),
),
onPressed: (){
},
),
),
);
},
itemCount: snapshot.data.documents.length,
);
I have also edited the attributes of the builder to haveSliverGridDelagateWithMaxCrossAxisExtent and other attributes. I know the buttons as they are if not placed inside the grid will shrink to minimum size but when in the grid they expand to fill up the whole column.
I have also tried numerous ways by replacing the gridview with a staggered gridview like this:
StaggeredGridView.countBuilder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
crossAxisCount: 3,
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 2 : 1),
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () {
//Navigator.of(context).pushNamed(HomePageResultsScreen.id);
},
child: ButtonTheme(
minWidth: 16,
height: 30,
child: RaisedButton(
padding: EdgeInsets.all(8.0),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
color: Colors.white,
child:
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
CachedNetworkImage(fit: BoxFit.contain,
height: 20,
width: 20,
placeholder: (context, url) => new CircularProgressIndicator(),
imageUrl: snapshot.data.documents[index]['icon'].toString(),
),
Text(snapshot.data.documents[index]['name'].toString(), textAlign: TextAlign.center, style: TextStyle(fontSize: 10, color: Colors.black,),),
],
),
onPressed: (){
},
),
),
);
},
itemCount: snapshot.data.documents.length,
),
I have tried both StaggeredGridView.countBuilder and StaggeredGridView.extent but both of these are even further from what I imagined. It just ends up with a single button per gridview row (looks like a listview).
I am not sure what I am doing wrong or if this is even possible with these widgets.
Thanks for your help
you have to use StaggeredGridView and you have to find logic to calculate an item width and set StaggeredTile.count.
final List<String> testing = ['Baby Foods', 'Confectionery', 'Coffee', 'Packets & Units','Packets & Units unis', 'Processed Cheese', 'Factional Care', 'Sauce', 'Sauc'];
Container(
width: width,
child: StaggeredGridView.countBuilder(
itemCount: resting.length,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
crossAxisCount: 8,
staggeredTileBuilder: (int index) {
if (testing[index].length < 5) {
return StaggeredTile.count(2, 1);
} else if (testing[index].length < 16) {
return StaggeredTile.count(3, 1);
} else {
return StaggeredTile.count(4, 1);
}
},
mainAxisSpacing: 12.0,
crossAxisSpacing: 10.0,
itemBuilder: (BuildContext context, int index) {
return TextButton(
child: Text(testing[index]),
onPressed: () {},
);
},
),
),
# # Staggered View
# ScreenShot
![alt text](https://github.com/Anup2712/flutterAnup/blob/master/StaggeredView/Screenshot.jpg)
# What is it?
A Flutter staggered grid view which supports multiple columns with rows of varying sizes.
It’s a grid layout where the cross axis is divided in multiple equally sized parts, and places elements in optimal position based on available main axis space.
You’ve probably came across some app or website design with staggered grid layout like Pinterest:
# For understand the code
To help you to understand the code, you can see that this grid of 10 tiles is divided into 4 columns:
![alt text](https://github.com/Anup2712/flutterAnup/blob/master/StaggeredView/understanding.png)
# Let's Get Started
<H3>1 - Depend on it</h3>
<h6><b>Add it to your package's pubspec.yaml file</b></h6>
<pre>dependencies:
fl_chart: ^0.6.1</pre>
<H3>2 - Install it</h3>
<h6><b>Install packages from the command line</b></h6>
<pre>flutter packages get</pre>
<H3>2 - Getting Started</h3>
<h6><b>Import the package:</b></h6>
<pre>import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';</pre>
<pre>import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:ofsdp/pages/home.dart';
import 'package:ofsdp/util/appdrawer.dart';
class AdministrativeUvit extends StatefulWidget {
#override
_AdministrativeUvitState createState() => _AdministrativeUvitState();
}
class _AdministrativeUvitState extends State<AdministrativeUvit> {
var _scaffoldkey = GlobalKey<ScaffoldState>();
List<StaggeredTile> _staggeredTiles = const <StaggeredTile>[
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1),
const StaggeredTile.count(1, 1.5),
const StaggeredTile.count(1, 1.5),
];
List<Widget> _tiles = const <Widget>[
const _Example01Tile(Colors.green, Icons.widgets),
const _Example01Tile(Colors.lightBlue, Icons.wifi),
const _Example01Tile(Colors.amber, Icons.panorama_wide_angle),
const _Example01Tile(Colors.brown, Icons.map),
const _Example01Tile(Colors.deepOrange, Icons.send),
const _Example01Tile(Colors.indigo, Icons.airline_seat_flat),
const _Example01Tile(Colors.red, Icons.bluetooth),
const _Example01Tile(Colors.pink, Icons.battery_alert),
const _Example01Tile(Colors.purple, Icons.desktop_windows),
const _Example01Tile(Colors.blue, Icons.radio),
];
#override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Image.asset(
"assets/images/footer.jpg",
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Color.fromRGBO(117, 80, 0, 1),
title: const Text("ADMINISTRATIVE UNIT"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.home),
onPressed: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => HomePage(),
),
);
},
)
],
),
drawer: AppDrawer(),
key: _scaffoldkey,
body: new Padding(
padding: const EdgeInsets.only(top: 12.0),
child: new StaggeredGridView.count(
crossAxisCount: 2,
staggeredTiles: _staggeredTiles,
children: _tiles,
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
padding: const EdgeInsets.all(4.0),
)))
],
);
}
}
class _Example01Tile extends StatelessWidget {
const _Example01Tile(this.backgroundColor, this.iconData);
final Color backgroundColor;
final IconData iconData;
#override
Widget build(BuildContext context) {
return new Card(
color: backgroundColor,
child: new InkWell(
onTap: () {},
child: new Center(
child: new Padding(
padding: const EdgeInsets.all(4.0),
child: new Icon(
iconData,
color: Colors.white,
),
),
),
),
);
}
}</pre>
If these containers are not buttons (mean you don't need to click on each of them to do specific something) then you can use Wrap Widget
https://www.youtube.com/watch?v=z5iw2SeFx2M
https://medium.com/flutter-community/flutter-wrap-widget-e1ee0b005b16

Flutter GridView is not scrolling

I am adding a header in the grid view. The header is scrolling but when touching grid view. It is not scrolling. I want to scroll header and gridview.
I have used SingleChildScrollView and Expanded. How to solve the please help me.
My code is shown below
Widget ItemGridview() {
return Container(
color: Colors.white,
padding: EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Expanded(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
new Text(
'Items of products',
style: TextStyle(fontWeight: FontWeight.w700, fontSize: 18.0),
textAlign: TextAlign.left,
),
GridView.count(
shrinkWrap: true,
primary: true,
padding: EdgeInsets.only(top:15.0),
crossAxisCount: 3,
childAspectRatio: 0.60, //1.0
mainAxisSpacing: 0.2, //1.0
crossAxisSpacing: 4.0, //1.0
children: createCategoryList(),
),
],
),
)
)
]
),
);
}
In my code Items of products is the header.
List<Widget> createCategoryList() {
List<Widget> createCategoryList = List<Widget>();
for (int i = 0; i < documents.length; i++) {
createCategoryList
.add(makeGridCell(documents[i].data['title'], "name", 8,documents[i].data['id']));
}
return createCategoryList;
}
Container makeGridCell(String name, String image, int count, String id) {
return Container(
child: new GestureDetector(
onTap: () {
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
verticalDirection: VerticalDirection.down,
children: <Widget>[
new Container(
child: Image.asset('assets/' + image + ".jpg"),
),
new Container(
color: Colors.white,
padding: EdgeInsets.only(left: 5),
child: new Text(name,
style: TextStyle(
fontWeight: FontWeight.w500, fontSize: 18.0)),
),
],
),
));
}
The createCategoryList() is the list of items in grid written in widget.
I had similar widget tree like you
a gridview.count() wrapped in SingleChildScrollView adding
physics: ScrollPhysics(),
to GridView.count() Widget Solved my problem
source:https://github.com/flutter/flutter/issues/19205
Add physics: ScrollPhysics() property to Gridview. it iwll scroll.
just add some property in GridView
Widget _buildFields(BuildContext context) {
return Container(
color: Colors.white,
child: GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 2.0,
mainAxisSpacing: 2.0,
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: NeverScrollableScrollPhysics(),
children: List.generate(choices.length, (index) {
return Center(
child: new Column(
children: [
new Expanded(
child: SelectCard(choice: choices[index]),//your card wight
),
],
),
);
}),
));
}
and use like this
class _Dashboard extends State<Dashboard> {
#override
Widget build(BuildContext context) {
return OrientationBuilder(builder: (context, orientation) {
return ListView(
children: <Widget>[
Container(
height: 200,
child: Image.network(
"https://www.gizbot.com/img/2013/11/23-weekend-deals-top-10-latest-smartphones.jpg"),
),
_buildFields(context),
],
);
});
}
}
You have some issues related to the Scroll of your widgets, you can reduce the amount of Widgets using Wrap, like this :
Container(
color: Colors.white,
padding: EdgeInsets.all(10),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'Items of products',
style: TextStyle(fontWeight: FontWeight.w700, fontSize: 18.0),
textAlign: TextAlign.left,
),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Wrap(
spacing: 20.0,
alignment: WrapAlignment.spaceEvenly,
children:createCategoryList(),
),
],
),
)
)
]
),
);
Add a constraint width or a fixed with to the widget of your item:
return Container(
constraints:
BoxConstraints(maxWidth: MediaQuery.of(context).size.width / 4),
child: new GestureDetector(
I think you need to use some custom scroll view
CustomScrollView(
primary: false,
slivers: <Widget>[
SliverPadding(
padding: const EdgeInsets.all(20.0),
sliver: SliverGrid.count(
crossAxisSpacing: 10.0,
crossAxisCount: 2,
children: <Widget>[
const Text('He\'d have you all unravel at the'),
const Text('Heed not the rabble'),
const Text('Sound of screams but the'),
const Text('Who scream'),
const Text('Revolution is coming...'),
const Text('Revolution, they...'),
],
),
),
],
)
Just ran into this myself, change your primary parameter for the GridView to false, give that a try.
In Gridview.builder scrolling is not working for smaller resolutions like tablet mode,mobile mode then just wrap the Gridview.builder under the listview.builder widget.
SizedBox(
width: screenSize.width,
height: screenSize.height * entry.contentHeightFactor,
child: ListView.builder(
itemCount: 1,
itemBuilder: (context, index) {
return Card(
child: Container(
width: screenSize.width * 0.8,
height: screenSize.height * 0.72,
padding: const EdgeInsets.all(10),
child: GridView.builder(
scrollDirection: Axis.vertical,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
),
padding: const EdgeInsets.all(5),
itemCount: 30,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child:Card(....),
);
},
),
),
);
},
),
),