The child of the gridView is not appearing and I ignore why that’s the case. This kept bugging me when working on my main code, that’s why I decided to create this fast code to test why it’s not showing up and still couldn’t come up to a conclusion. It works fine with List.builder mind you, can someone help me make the content show up?
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'dart:io';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
@override
bool _click = false;
void clicker() {
setState(() {
_click = !_click;
});
}
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
child: GridView.builder(
itemCount: xer.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
itemBuilder: (BuildContext, index) {
return GridTile(
child: SizedBox(
child: Card(
child: Column(
children: [
Expanded(
child: Container(
child: Text("text"),
),
),
],
),
),
),
header: GridTileBar(
backgroundColor: Colors.green,
title: Text("return"),
),
footer: Text("footer text"));
}))),
);
}
}
List xer = ["orange", "banana", "strawberry"];
3
Answers
The answer is very obvious, it's just the gridtile child not being centered.
Make sure that the GridView is placed inside a widget that can scroll. If the GridView is not inside a ListView, NestedScrollView, or another scrolling widget, it may not appear on the screen
I think I get what you’re trying to do. My solution would be this:
For including multiple widgets inside the Card-like column would look like this:
Also this is the documentation for GridTile child parameter:
The widget that fills the tile.
This widget can only have one child. To lay out multiple children, let this widget’s child be a widget such as [Row], [Column], or [Stack], which have a children property, and then provide the children to that widget.
Also also fixed some needless containers, and added const keywords for optimalization 😉