首页 > 解决方案 > 使用 SVG 作为容器的背景图像

问题描述

这个问题中,我使用 Flutter 的 SVG 包 ( flutter_svg) 来渲染SVG 图像

我想使用 SVG 作为中间的Container背景Text

带有背景图像的容器示例

这是我到目前为止的代码:

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
          )
        ),
      ],
    )

我发现的问题SvgPicture不是,ImageProvider所以我不能添加BoxDecoration来获取背景图像。

有没有办法然后SvgPicture用作容器的盒子装饰或背景?

标签: dartflutter

解决方案


使用 SvgPicture 的确切方法是这样的:

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())],
          ),
        ),
      ],
    ),
  );
}

推荐阅读