Emir Bolat
Emir Bolat

Reputation: 1049

How to add text on top of Dart Flutter gridView?

I made an application like this:

enter image description here

I want to add a little info text on top of this gridView structure. How can I do it? I'm a beginner and I just couldn't do it.

Codes:

body: Container(
    padding: EdgeInsets.all(7),
    child: GridView.builder(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        crossAxisSpacing: 10,
        mainAxisSpacing: 10,

      ),
      itemCount: subjects.length,
      itemBuilder: (BuildContext context, int index) {
        return Padding(
          
          padding: const EdgeInsets.all(2.0),
          child: Container(
            child: InkWell(
              
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  border: Border.all(color: Colors.black),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  
                    children: [
                      Image.asset(subjects[index].subjectImage, fit: BoxFit.cover, height: 50, width: 50,),
                      SizedBox(height: 15,),
                      Text(subjects[index].subjectName, style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),),
                      
                    ],
                    
                  ),
                  
                ),
                onTap: () {},
            ),
          ),
        );
      },
    ),

How can I do it? Thanks in advance.

Upvotes: 1

Views: 2537

Answers (2)

DholaHardik
DholaHardik

Reputation: 502

Try below code. I have added text like Text("1234567890")

body: Container(
  padding: EdgeInsets.all(7),
  child: Column(
    children: [
      Text("1234567890"), //<------------


      Expanded(
        child: GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
          ),
          itemCount: subjects.length,
          itemBuilder: (BuildContext context, int index) {
            return Padding(
              padding: const EdgeInsets.all(2.0),
              child: Container(
                child: InkWell(
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      border: Border.all(color: Colors.black),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset(
                          subjects[index].subjectImage,
                          fit: BoxFit.cover,
                          height: 50,
                          width: 50,
                        ),
                        SizedBox(
                          height: 15,
                        ),
                        Text(
                          subjects[index].subjectName,
                          style: TextStyle(
                              fontSize: 20, fontWeight: FontWeight.w500),
                        ),
                      ],
                    ),
                  ),
                  onTap: () {},
                ),
              ),
            );
          },
        ),
      ),
    ],
  ),
);

Upvotes: 2

Yash Bhansali
Yash Bhansali

Reputation: 450

You can use @DholaHardik's answer. Just need to wrap the Grid View Builder to a Sized Box and give it a height let's say height = MediaQuery.of(context).size.height and if the error says Overyflow by say 45 pixels.. You can change height to MediaQuery.of(context).size.height - 45;

Upvotes: 1

Related Questions