Home > Back-end >  Flutter: Make all screen scrollable with GridView.builder inside
Flutter: Make all screen scrollable with GridView.builder inside

Time:10-11

In my home screen my app shows carousel first then a vertical list of challenges cards retrieved from Cloud Firestore using GridView.builder as follows:

GridView.builder(
        scrollDirection: Axis.vertical,
        itemCount: _challenges.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 1,
          childAspectRatio: MediaQuery.of(context).size.width /
              (MediaQuery.of(context).size.height / 4),
        ),
        itemBuilder: (context, index) {
          return InkWell(
            onTap: () {
              if (_challenges[index]["isLocked"] == "true") {
                showLockedDialog();
              } else {
                checkParticipation(index);
                if (checkPart == true) {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (_) =>
                              ChallengeDetails(_challenges[index])));
                }
                checkPart = true;
              }
            },
            child: Stack(
              children: [
                Center(
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(15),
                    child: Image(
                      image: NetworkImage(_challenges[index]["image-path"]),
                      fit: BoxFit.cover,
                      height: 150,
                      width: 350,
                      opacity: _challenges[index]["isLocked"] == "true"
                          ? AlwaysStoppedAnimation(.4)
                          : null,
                    ),
                  ),
                ),
                Center(
                  child: Text(
                    "${_challenges[index]["name"]}\n",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 20,
                        fontWeight: FontWeight.bold),
                  ),
                ),
                Center(
                    child: Text(
                  "\n${_challenges[index]["date"]}",
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                      fontWeight: FontWeight.bold),
                  textDirection: TextDirection.ltr,
                )),
                Center(
                  child: SizedBox(
                    height: 130,
                    width: 130,
                    child: _challenges[index]["isLocked"] == "true"
                        ? Image.asset("assets/lock-icon.jpg")
                        : null,
                  ),
                )
              ],
            ),
          );
        });

Everything retrieving fine and it is rendered in my home_screen as follows:

body: Column(
            children: [
              AdsBanner(),
              SizedBox(
                height: 30,
              ),
              Padding(
                padding: const EdgeInsets.only(right: 8, left: 8, bottom: 5),
                child: Row(
                  children: [
                    Text(
                      AppLocalizations.of(context)!.challenges   " ",
                      style: TextStyle(fontSize: 20),
                    ),
                    Text(
                      AppLocalizations.of(context)!.clickToParticipate,
                      style: TextStyle(fontSize: 15),
                    )
                  ],
                ),
              ),
              Expanded(child: ChallengeCard()),
            ],
          ),

The problem is that only the GridView area is scrolling and what am seeking for is to scroll the whole screen with the GridView area, I was trying to use the CustomScrollView() but its not working properly.

I'll be thankful for any help.

CodePudding user response:

First in your GridView.builder add these:

GridView.builder(
   physics: NeverScrollableScrollPhysics(),
   shrinkWrap: true,
   ...
)

then in your home_screen wrap your column with SingleChildScrollView:

SingleChildScrollView(
  child: Column(
            children: [
              AdsBanner(),
              SizedBox(
                height: 30,
              ),
              Padding(
      ...
  ),
),

CodePudding user response:

You can provide physics: NeverScrollableScrollPhysics() on GridView to disable scroll effect. If you want scrollable as secondary widget use primary: false, to have Full Page scrollable, you can use body:SingleChildScrollView(..) or better using body:CustomScrollView(..)

  • Related