Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a SVG as a background image for a Container

Tags:

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.

example of Container with background image

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?

like image 469
S.D. Avatar asked Nov 18 '18 06:11

S.D.


People also ask

Can you set an SVG as a background image?

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.

How do I use SVG in CSS?

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.

How do you put a background image in a container in flutter?

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.

Does SVG have background color?

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.


2 Answers

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())],           ),         ),       ],     ),   ); } 
like image 78
locopump Avatar answered Sep 20 '22 16:09

locopump


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.

like image 44
carlosx2 Avatar answered Sep 18 '22 16:09

carlosx2