Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a background with stripes in Flutter

I'm trying to build a background consisting of alternating red and orange stripes like this: Flutter background

I don't want to use a static image to ensure consistency over different devices.

I tried to use gradients but I'm having trouble making it work.

Container(
    decoration: BoxDecoration(
      // Box decoration takes a gradient
      gradient: LinearGradient(
        // Where the linear gradient begins and ends
        begin: Alignment.topRight,
        end: Alignment(0.3, 0),
        tileMode: TileMode.repeated, // repeats the gradient over the canvas
        colors: [
          // Colors are easy thanks to Flutter's Colors class.
          Colors.red,
          Colors.orange,
        ],
      ),
    ),
  ),

Flutter code

Is there a better way to solve this other than gradients in Dart / Flutter?

like image 370
Kevin Bos Avatar asked Aug 28 '19 20:08

Kevin Bos


People also ask

How to set a background image in flutter?

A common way to set a background image in Flutter applications is by using DecorationImage. Below are the examples which include how to set the fit mode, transparency, and prevent image resizing when the keyboard is shown. You may already be familiar with Container widget.

How do I add a background image to my decoration?

To set a background image, you can use a Container widget and pass a Decoration object containing the image. For the image source, you need to create a DecorationImage and pass it to the Decoration. It's also possible to define how the image should be inscribed to the available space and set the opacity of the image.

How to set an image background in WordPress?

Here, I am explaining two ways to set an image background. The first way is by using the Stack widget. The Stack widget helps us to create multiple layers of widgets which overlay each other in a given order. Here, the Bottom widget will be the bottom most widget. The Middle widget overlays over Bottom widget.

How to set transparent background color for a widget?

You can use Color.fromARGB (Alpha, Red, Green, Blue) method to set the transparent background color. The alpha value ranges from 0-255. You can wrap your widget tree with Opacity () widget, it will set the opacity to your widget including its content.


1 Answers

I've just modified gradient value in the question and ended up with this. You can adjust the end value to change the angle and width of the strips.

Image of resulting widget with red and orange repeated strips

BoxDecoration(
    gradient: LinearGradient(
        begin: Alignment.topLeft,
        end: Alignment(-0.4, -0.8),
        stops: [0.0, 0.5, 0.5, 1],
        colors: [
            Colors.red,
            Colors.red,
            Colors.orange,
            Colors.orange,
        ],
        tileMode: TileMode.repeated,
    ),
)

like image 103
ruwan800 Avatar answered Nov 11 '22 08:11

ruwan800