skip to Main Content

I am trying to create an application for university project using flutter. My current code is as follows.

import 'package:flutter/material.dart';
import 'package:mathappcd/constants/widgets_constants.dart';
import 'package:mathappcd/screens/sectionA.dart';
import 'package:mathappcd/screens/sectionB.dart';
import 'package:mathappcd/widgets/app_bar.dart';
import 'package:mathappcd/widgets/square_btn.dart';

extension ContextExtension on BuildContext {
  bool get isSmallScreen => MediaQuery.of(this).size.shortestSide < 500;
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const CustAppBar(
        title: "App",
      ),
      body: ListView(
        children: [
          const SizedBox(
            height: 300,
            child: Center(
              child: Text("Graph"),
            ),
          ),
          GridView.count(
            shrinkWrap: true,
            padding: const EdgeInsets.all(WidgetConstants.sqrBtnPadding),
            mainAxisSpacing: 20,
            crossAxisSpacing: 20,
            crossAxisCount: context.isSmallScreen ? 2 : 4,
            children: const [
              SquareBtn(
                text: "A",
                directTo: SectionA(),
              ),
              SquareBtn(
                text: "B",
                directTo: SectionB(),
              ),
              SquareBtn(
                text: "C",
                directTo: SectionA(),
              ),
              SquareBtn(
                text: "D",
                directTo: SectionB(),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

And it gives the result like this.
But I want the grid view to stick to bottom as in this image.

I achived this using a Column and a spacer but then when the screen height isn’t enough it gives an error like this. I have tried many other ways but failed to get the exact result I needed. I want it to be responsive and also the whole wcreen to be scrollable when the screen height is lesser than the height of SizedBox (Graph) + Grid View.

Please help! Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Finally after with help of ChatGPT and tinkering around I figured out a way.

    body: Align(
            alignment: Alignment.bottomCenter,
            child: SingleChildScrollView(
              physics: const AlwaysScrollableScrollPhysics(),
              child: SizedBox(
                height: MediaQuery.of(context).size.height <
                        WidgetConstants.bannerMinHeight + 5
                    ? WidgetConstants.bannerMinHeight + 100
                    : MediaQuery.of(context).size.height,
                child: Column(
                  children: [
                    Expanded(
                      child: Container(
                        constraints: const BoxConstraints(
                            minHeight: WidgetConstants.bannerMinHeight),
                        color: Colors.grey[200],
                        child: const Center(
                            child: Column(
                          children: [
                            Text("Graph"),
                          ],
                        )),
                      ),
                    ),
                    GridView.count(
                      shrinkWrap: true,
                      padding: const EdgeInsets.all(WidgetConstants.sqrBtnPadding),
                      mainAxisSpacing: 20,
                      crossAxisSpacing: 20,
                      crossAxisCount: context.isSmallScreen ? 2 : 4,
                      children: const [
                        SquareBtn(
                          text: "A",
                          directTo: SectionA(),
                        ),
                        SquareBtn(
                          text: "B",
                          directTo: SectionB(),
                        ),
                        SquareBtn(
                          text: "C",
                          directTo: SectionA(),
                        ),
                        SquareBtn(
                          text: "D",
                          directTo: SectionB(),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
    

    Align -SingleChildScrollView --SizedBox ---Column ----GridView

    This gave somewhat of a result. Not the best way since the scrollable height is a constant now but still it works.


  2. ListView is giving the wrong output as items in listview have a standart size. each item will be the same size and can also be manipulated by setting itemExtent parameter.

    You can implement your code using a singlechild scrollview (to remove the rendeflex issue if the children are big) and column.

    Edited:

    extension ContextExtension on BuildContext {
      bool get isSmallScreen => MediaQuery.of(this).size.shortestSide < 500;
    }
    
    class HomePage extends StatefulWidget {
      const HomePage({Key? key}) : super(key: key);
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      @override
      void didChangeDependencies() {
        super.didChangeDependencies();
      }
    
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: const CustAppBar(
            title: "App",
          ),
          body: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            mainAxisSize: MainAxisSize.min,
            children: [
              const SizedBox(
                height: 300,
                child: Center(
                  child: Text("Graph"),
                ),
              ),
              Flexible(
                child: GridView.count(
                  shrinkWrap: true,
                  padding: const EdgeInsets.all(WidgetConstants.sqrBtnPadding),
                  mainAxisSpacing: 20,
                  crossAxisSpacing: 20,
                  crossAxisCount: context.isSmallScreen ? 2 : 4,
                  children: const [
                    SquareBtn(
                      text: "A",
                      directTo: SectionA(),
                    ),
                    SquareBtn(
                      text: "B",
                      directTo: SectionB(),
                    ),
                    SquareBtn(
                      text: "C",
                      directTo: SectionA(),
                    ),
                    SquareBtn(
                      text: "D",
                      directTo: SectionB(),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search