skip to Main Content

I have one constraint widget in center of screen so i want to add new widget which is above of center widget.

I have added red box in center of the screen and i want to place blue box above of red box.
enter image description here

I have tried with constraint and column widget in stack widget but new widget not set on above of center widget.

body: Container(
  width: double.infinity,
  child: Stack(
    alignment: Alignment.center,
    children: [
      Container(
        height: 150,
        width: 100,
        color: Colors.blue,
      ),
      Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            height: 100,
            width: 200,
            color: Colors.red,
          )
        ],
      )
    ],
  ),
),

3

Answers


  1. To center a blue box above a red box, use a Column with MainAxisAlignment.center and CrossAxisAlignment.center. Here’s the updated code:

        body: Container(
          width: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                height: 100,
                width: 100,
                color: Colors.blue,
              ),
              SizedBox(height: 20),
              Container(
                height: 200,
                width: 200,
                color: Colors.red,
              )
            ],
          ),
        ),
    

    Here is the Example

    Login or Signup to reply.
  2. enter image description here

    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
      static const String _title = 'Flutter Stateful Clicker Counter';
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _title,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key});
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Flutter Demo Click Counter'),
          ),
          body: Container(
            width: double.infinity,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      height: 100,
                      width: 200,
                      color: Colors.red,
                    )
                  ],
                ),
                Container(
                  height: 150,
                  width: 100,
                  color: Colors.blue,
                ),
              ],
            ),
          ),
        );
      }
    }
    

    I Hope this may help you

    Login or Signup to reply.
  3. To place a widget above the center widget, you can use the Stack widget to layer your widgets. Here’s a simple example based on your code:

          body: Center(
            child: Stack(
              alignment: Alignment.bottomCenter,
              children: [
                Container(
                  height: 150,
                  width: 100,
                  color: Colors.blue,
                ),
                Container(
                  height: 100,
                  width: 200,
                  color: Colors.red,
                )
              ],
            ),
          ),
    

    Example

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search