【问题标题】:Add background image in flutter在颤动中添加背景图像
【发布时间】:2019-09-07 16:15:24
【问题描述】:

我正在尝试在 Flutter 中创建附加屏幕。如何添加背景图片并在特定位置添加文本(忽略白色文本框)。

感谢您的帮助

【问题讨论】:

标签: flutter flutter-layout


【解决方案1】:

要添加背景图片,您必须使用 DecorationImage 类并在 BoxDecoration 内部。

 class Home extends StatelessWidget{
      @override
      Widget build(BuildContext context){
        return Scaffold(
          body: Container(
            decoration: BoxDecoration(
              image: DecorationImage(image: AssetImage("assets/image1.jpg"), fit: BoxFit.cover),
            ),
            child: Center(child: Text('Welcome To',style: TextStyle(
              color: Colors.white,
              fontSize: 40.0
            ),)),
            )
        );
      }
    }

【讨论】:

    【解决方案2】:

    还要确保创建一个资产目录并将您的图像资产添加到其中,然后在“flutter:”下的update your pubspec.yaml 文件如下所示:

    flutter:
      assets:
        - assets/splash.png
    

    其中 splash.png 是您的图片资源。或者只是使用:

    flutter:
      assets:
        - assets/
    

    如果你想要整个目录。如果没有,您将只渲染一个空白容器。

    【讨论】:

    • 为什么这被否决了?我自己来寻找这个答案并按照上面的方法进行操作,但仍然没有渲染图像,最后才意识到我搞砸了 pubspec.yaml 文件。
    【解决方案3】:

    试试这个;

    Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
                image: new DecorationImage(
                    image: new AssetImage("assets/splash.png"),
                    fit: BoxFit.cover
                )
            ),
            alignment: Alignment.bottomCenter,
            padding: EdgeInsets.only(bottom: 150.0),
            child: JumpingDotsProgressIndicator(
              fontSize: 50.0,
              numberOfDots: 4,
              dotSpacing: 2.0,
              color: Colors.white,
              milliseconds: 400,
            ),
          ),
    
        ],
      ),
    );
    }
    

    您可以自定义子部分。

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 2019-12-27
      • 2021-06-18
      • 2018-07-20
      • 2020-10-01
      • 2020-08-17
      相关资源
      最近更新 更多