I'm trying to build a background consisting of alternating red and orange stripes like this:
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,
],
),
),
),
Is there a better way to solve this other than gradients in Dart / 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.
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.
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.
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.
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.
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,
),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With