1

I’m trying to create a top bar using a Container with a horizontal LinearGradient background and a Row of widgets inside it.

However, even though I’m using Alignment.centerLeft to Alignment.centerRight, the gradient does not appear to connect exactly in the center. It looks slightly shifted towards the right.

Here is my code:

child: Container(
  height: 65.hp, // using flutter_screenutil
  decoration: const BoxDecoration(
    gradient: LinearGradient(
      colors: [
        kGradientFirstColor,
        kGradientSecondColor,
      ],
      begin: Alignment.centerLeft,
      end: Alignment.centerRight,
    ),
  ),
  child: Padding(
    padding: EdgeInsets.symmetric(horizontal: 16.wp, vertical: 10.hp),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          children: [
            Padding(
              padding: EdgeInsets.only(bottom: 4.hp),
              child: SvgPicture.asset(
                "assets/images/svg/home_hamburger_svg.svg",
                width: 24.hp,
                height: 24.hp,
              ),
            ),
            SizedBox(width: 10.wp),
            SvgPicture.asset(
              "assets/images/svg/my_prop_ai_header.svg",
              width: 111.wp,
              height: 22.wp,
            ),
          ],
        ),
        Row(
          children: [
            SvgPicture.asset(
              "assets/images/svg/heart_svg.svg",
              width: 24.wp,
              height: 24.hp,
            ),
            SizedBox(width: 20.wp),
            SvgPicture.asset(
              "assets/images/svg/bell_svg.svg",
              width: 24.wp,
              height: 24.hp,
            ),
          ],
        ),
      ],
    ),
  ),
)

2 Answers 2

2

You can adjust the gradient with the code below

const LinearGradient(
  colors: [Colors.blue, Colors.black, Colors.black],
  stops: [0.0, 0.5, 1.0], // Adjust stops to control the gradient spread
  begin: Alignment.centerLeft, // Start from left
  end: Alignment.centerRight, // End at right
),
Sign up to request clarification or add additional context in comments.

Comments

1

This is caused by the asymmetry of your Row content, which makes the gradient appear shifted. The left side of the Row contains more visual weight (larger widgets) than the right, creating an optical imbalance.

here are 2 alternatives:

1. Use a Stack to separate the background gradient from the content
By placing the gradient in a background Container and layering the Row on top, you ensure the gradient stretches evenly from left to right, independent of the widget layout.

Stack(
  children: [
    Container( // background gradient
      height: 65.hp,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          colors: [kGradientFirstColor, kGradientSecondColor],
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
        ),
      ),
    ),
    Padding( // foreground content
      padding: EdgeInsets.symmetric(horizontal: 16.wp, vertical: 10.hp),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row( /* left content */ ),
          Row( /* right content */ ),
        ],
      ),
    ),
  ],
)

2. Adjust the visual balance in the Row
Within the Row, you can use Spacer or extra padding on the lighter side (the right side) to visually balance it with the heavier left side. This helps center the content more evenly over the gradient and avoids the illusion of a shifted background.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.