From this question I am using Flutter's SVG package (flutter_svg
) to render a SVG image.
I want to use the SVG as a Container
background with Text
in the middle.
This is the code I have so far:
Container( decoration: BoxDecoration( image: DecorationImage(image: SvgPicture.asset( 'assets/example.svg', ),), ), children: <Widget>[ Text('Welcome to my Flutter App', style: Theme.of(context).textTheme.display1.copyWith( color: Colors.white, fontWeight: FontWeight.bold ) ), ], )
The problem I am finding is that SvgPicture
is not an ImageProvider
so I can't add the BoxDecoration
to get a background image.
Is there a way to then use SvgPicture
as a Container's box decoration or background?
SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. All the same awesomeness of SVG comes along for the ride, like flexibility while retaining sharpness. Plus you can do anything a raster graphic can do, like repeat.
We can use SVG in CSS via data URI, but without encoding it works only in Webkit based browsers. If encode SVG using encodeURIComponent() it will work everywhere. SVG must have attribute xmlns like this: xmlns='http: //www.w3.org/2000/svg' . If it doesn't exist, it will be added automagically.
Steps to set the background image:Step 1: Add the Container widget. Step 2: Add the decoration parameter (inside Container) and assign the BoxDecoration class. Step 3: Add the image parameter (inside BoxDecoration) and assign the DecorationImage class.
The SVG background is used to draw any kind of shape, set any color you want by the set property. The below examples illustrates the concept of SVG set background-color more specifically. The SVG allowed the CSS background sizing, position, and much more complex property.
The exact way to use an SvgPicture is like this:
Widget build(BuildContext context) { return Scaffold( body: Stack( children: <Widget>[ SvgPicture.asset( 'assets/images/splash/background.svg', alignment: Alignment.center, width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, ), Container( child: Column( children: <Widget>[Expanded(child: _createLogo())], ), ), ], ), ); }
how about using a stack() and build everything on top of that. That is how I have done it with just an image as a background for the full viewport.
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