Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having issues dividing the app screen into halves and/or fractions

the below code is one of my many pages for my app. I’m trying to set this page up with the LayoutBuilder so that it’ll work like a quiz page with the bottom half/section would hold 3 to 4 TextButtons and the top half/section would hold the question and possibly display a picture.
I did some research and I found that you’re able to use the LayoutBuilder to set up containers to be half of the screen. However, when I tried doing that here with this code, I got A LOT of exceptions thrown. So I was wondering if there was a better way to divide the screen in half (or possibly 1/4, 1/4, & 1/2) for a quiz-like layout.
Thank you for your help!

quiz.dart

class Quiz extends StatelessWidget {
const Quiz({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Quiz'),
),
body: Column(
children: [
Align(alignment: Alignment.topCenter,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
height: constraints.maxHeight / 2,
// widthdouble.infinity,
);
},
)),
Align(alignment: Alignment.topCenter,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
height: constraints.maxHeight / 2,
// widthdouble.infinity,
color: const Color.fromRGBO(155, 205, 255, 0.8),
child: Column(children: [
/* TextButton(onPressed: onPressed, child: child),
TextButton(onPressed: onPressed, child: child),
TextButton(onPressed: onPressed, child: child), */
Text('Option A'),
Text('Option B'),
Text('Option C'),
],),
);
},
)),

],
));
}
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try to move the LayoutBuilder up, so that you can use the constraints for the children of your Column:

enter image description here

class Quiz extends StatelessWidget {
  const Quiz({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Quiz')),
      body: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return Column(
            children: [
              Container(
                height: constraints.maxHeight / 2,
                color: Colors.green.shade300,
              ),
              Container(
                height: constraints.maxHeight / 2,
                color: Colors.blue.shade300,
              ),
            ],
          );
        },
      ),
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading