Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Positioned Widget responsive

I want to make positioned widgets responsive but cannot find a way. I used media query but nothing is working. I know that there are many answers but I did not see anyone targeting positioned widget. Everyone doing row-column responsive. If you attach a simple example with the position it will be great. Here is my code.

  Positioned(
              top: MediaQuery.of(context).size.width * 0.87,
              left: MediaQuery.of(context).size.width * 0.02,
              // width: MediaQuery.of(context).size.width / 30,
              child: Text(
                'TRACK’R',
                style: TextStyle(
                  fontWeight: FontWeight.w500,
                  fontSize: MediaQuery.of(context).size.width / 30,
                ),
              ),
            ),
like image 634
Shahryar Rafique Avatar asked Nov 15 '25 05:11

Shahryar Rafique


1 Answers

Here is your Positioned widget inside a Stack. Isn't this what you want to achieve?

enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: HomePage(),
    ),
  );
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(color: Colors.blueGrey.shade300),
          Positioned(
            top: MediaQuery.of(context).size.width * 0.87,
            left: MediaQuery.of(context).size.width * 0.02,
            width: MediaQuery.of(context).size.width / 3,
            child: ColoredBox(
              color: Colors.white.withOpacity(.4),
              child: Text(
                'TRACK’R',
                style: TextStyle(
                  fontWeight: FontWeight.w500,
                  fontSize: MediaQuery.of(context).size.width / 30,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Though, you might want to define your top based on the height of your screen instead of the width. Otherwise, your Text will be out of the screen in landscape mode:

enter image description here

Another way would be to use Align and FractionallySizedBox inside your Stack:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: HomePage(),
    ),
  );
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(color: Colors.blueGrey.shade300),
          Align(
            alignment: Alignment(-.9, .9),
            child: FractionallySizedBox(
              widthFactor: .3,
              child: ColoredBox(
                color: Colors.white.withOpacity(.4),
                child: Text(
                  'TRACK’R',
                  style: TextStyle(
                    fontWeight: FontWeight.w500,
                    fontSize: MediaQuery.of(context).size.width / 30,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Or, using a LayoutBuilder and then a Positioned widget inside your Stack:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: HomePage(),
    ),
  );
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LayoutBuilder(
        builder: (_, constraints) {
          final width = constraints.biggest.width;
          final height = constraints.biggest.height;
          return Stack(
            children: [
              Container(color: Colors.blueGrey.shade300),
              Positioned(
                bottom: height * .05,
                left: width * .05,
                width: width / 3,
                child: ColoredBox(
                  color: Colors.white.withOpacity(.4),
                  child: Text(
                    'TRACK’R',
                    style: TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: width / 30,
                    ),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
like image 68
Thierry Avatar answered Nov 17 '25 18:11

Thierry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!