skip to Main Content

I have a question. Please see the image to understand better what I mean.
image

I want to create the yellow Containers like in the given image.

Here is my code:

import "package:flutter/material.dart";

void main(List<String> args) => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Mypage(),
    );
  }
}

class Mypage extends StatefulWidget {
  const Mypage({super.key});

  @override
  State<Mypage> createState() => _MypageState();
}

class _MypageState extends State<Mypage> {
  TextEditingController height_controller = TextEditingController(text: '');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.shade800,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.grey.shade800,
        title: const Text(
          'BMI Calculator',
          style: TextStyle(color: Colors.amber),
        ),
      ),
   body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: [
            Row(
              children: [
                Align(
                  alignment: Alignment.topLeft,
                  child: SizedBox(
                    height: 150,
                    width: 150,
                    child: TextField(
                      controller: height_controller,
                      keyboardType: const TextInputType.numberWithOptions(),
                      decoration: const InputDecoration(
                        floatingLabelAlignment: FloatingLabelAlignment.center,
                        label: Text(
                          'Height',
                          style: TextStyle(color: Colors.amber, fontSize: 30),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                  ),
                ),
                const SizedBox(
                  width: 70,
                ),
                const Align(
                  alignment: Alignment.topRight,
                  child: SizedBox(
                    height: 150,
                    width: 150,
                    child: TextField(
                      keyboardType: TextInputType.numberWithOptions(),
                      decoration: InputDecoration(
                        floatingLabelAlignment: FloatingLabelAlignment.center,
                        label: Text('Weight',
                            style:
                                TextStyle(color: Colors.amber, fontSize: 30)),
                      ),
                    ),
                  ),
                )
              ],
            ),
            TextButton(
              onPressed: () {},
              child: const Text(
                'Calculate',
                style: TextStyle(color: Colors.amber, fontSize: 30),
              ),
            ),
            const SizedBox(
              height: 300,
            ),
            Container(
              alignment: Alignment.center,
              height: 30,
              width: 300,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(30), color: Colors.amber),
            ),
            const SizedBox(
              height: 30,
            ),
            Container(
              height: 30,
              width: 300,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(30), color: Colors.amber),
            ),
          ],
        ),
      ),
    );
  }
}

2

Answers


  1. SizedBox.expand > Column > [Expanded, Expanded] should give you a full-page with two equal vertical halves. Like this:

    class Body extends StatelessWidget {
      const Body({
        super.key,
      });
    
      @override
      Widget build(BuildContext context) {
        return const SizedBox.expand(
          child: Column(
            children: [
              Expanded(child: Placeholder()),
              Expanded(child: Placeholder()),
            ],
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. Wrap column widget with Stack and add column with all the top widget as a first one and in the second one add those containers wrapped in Align widget. this is how you can achieve what you want. You can take reference from below code snippet.

       @override
          Widget build(BuildContext context) {
            return Scaffold(
              body: Stack(
                 children: <Widget>[
                  const Align(
                    alignment: Alignment.bottomCenter,
                    child: Column(
                      children: [
                     Row(
                  children: [
                    Align(
                      alignment: Alignment.topLeft,
                      child: SizedBox(
                        height: 150,
                        width: 150,
                        child: TextField(
                          controller: height_controller,
                          keyboardType: const 
                          TextInputType.numberWithOptions(),
                          decoration: const InputDecoration(
                            floatingLabelAlignment: 
                              FloatingLabelAlignment.center,
                            label: Text(
                              'Height',
                              style: TextStyle(color: Colors.amber, fontSize: 30),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(
                      width: 70,
                    ),
                    const Align(
                      alignment: Alignment.topRight,
                      child: SizedBox(
                        height: 150,
                        width: 150,
                        child: TextField(
                          keyboardType: TextInputType.numberWithOptions(),
                          decoration: InputDecoration(
                            floatingLabelAlignment: 
                               FloatingLabelAlignment.center,
                            label: Text('Weight',
                                style:
                                    TextStyle(color: Colors.amber, fontSize: 30)),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
                TextButton(
                  onPressed: () {},
                  child: const Text(
                    'Calculate',
                    style: TextStyle(color: Colors.amber, fontSize: 30),
                  ),
                ),
                  ]
                    ),
                  ),
                 
                Container(
                  alignment: Alignment.center,
                  height: 30,
                  width: 300,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(30), color: Colors.amber),
                ),
                
                Container(
                  height: 30,
                  width: 300,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(30), color: Colors.amber),
                ),
    
                ],
              ),
            );
          }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search