Stack Layout In Flutter

Stack Layout In Flutter : this flutter tutorial is explaining us how to design stack layout in flutter.

There are two main types of stack

  • stack
  • Indexed Stack

stack

  • Stack widget allows us to put up multiple layers of widgets into screen
  • the widget takes the multiple children and orders them from bottom to top.
  • so the last is the topmost and the first is the bottommost.
  • each member which is in the stack need an Alignment.
  • By Default it is align in the top left corner.

below is an example of stack layout in flutter (with output)

Stack Layout In Flutter

Stack Layout In Flutter :

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(title: "Flutter Tutorial", home: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return HomePage();
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Stack Layout"),
        ),
        body: Center(child: Stack(
          children: <Widget>[
            Padding(
              padding:
              const EdgeInsets.only(top: 32, left: 8, right: 8, bottom: 16),
              child: Container(
                width: 150,
                height: 200,
                decoration: BoxDecoration(
                  boxShadow: <BoxShadow>[
                    BoxShadow(
                        color:
                        HexColor("#738AE6").withOpacity(0.6),
                        offset: Offset(1.1, 4.0),
                        blurRadius: 8.0),
                  ],
                  gradient: LinearGradient(
                    colors: [
                      HexColor("#738AE6"),
                      HexColor("#738AE6"),
                    ],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  ),
                ),
                child: Padding(
                  padding: const EdgeInsets.only(
                      top: 54, left: 16, right: 16, bottom: 8),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        'Flutter',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                          letterSpacing: 0.2,
                          color: Colors.white,
                        ),
                      ),
                      Expanded(
                        child: Padding(
                          padding: const EdgeInsets.only(top: 8, bottom: 8),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                'Flutter Tutorial',
                                style: TextStyle(
                                  fontWeight: FontWeight.w500,
                                  fontSize: 10,
                                  letterSpacing: 0.2,
                                  color: Colors.white,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      1 != 0
                          ? Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: <Widget>[
                          Text(
                            'Price',
                            textAlign: TextAlign.center,
                            style: TextStyle(

                              fontWeight: FontWeight.w500,
                              fontSize: 24,
                              letterSpacing: 0.2,
                              color: Colors.white,
                            ),
                          ),
                          Padding(
                            padding:
                            const EdgeInsets.only(left: 4, bottom: 3),
                            child: Text(
                              '1',
                              style: TextStyle(
                                fontWeight: FontWeight.w500,
                                fontSize: 10,
                                letterSpacing: 0.2,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],
                      )
                          : Container(
                        decoration: BoxDecoration(
                          color:  Color(0xFFFAFAFA),
                          shape: BoxShape.circle,
                          boxShadow: <BoxShadow>[
                            BoxShadow(
                                color:  Color(0xFF213333)
                                    .withOpacity(0.4),
                                offset: Offset(8.0, 8.0),
                                blurRadius: 8.0),
                          ],
                        ),
                        child: Padding(
                          padding: const EdgeInsets.all(6.0),
                          child: Icon(
                            Icons.add,
                            color: HexColor("#FFB295"),
                            size: 24,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            Positioned(
              top: 0,
              left: 0,
              child: Container(
                width: 84,
                height: 84,
                decoration: BoxDecoration(
                  color: Color(0xFFFAFAFA).withOpacity(0.2),
                  shape: BoxShape.circle,
                ),
              ),
            ),
            Positioned(
              top: 0,
              left: 8,
              child: SizedBox(
                width: 80,
                height: 80,
                child: Icon(Icons.favorite_border, size: 50, color: Colors.blue.withOpacity(0.5)),
              ),
            )
          ],
        )));
  }
}

class HexColor extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in