首页 > 解决方案 > 如何在颤动中设置背景图像?

问题描述

我最近开始学习flutter,我想添加背景图片,我想在上面添加两个平面按钮,但是我做不到,我在代码中添加了背景图片,但它不起作用我没有了解问题。谁能帮帮我?

    void main() {
      return runApp(
        MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              title: Text('Ani & genny',
                  style: TextStyle(
                    fontFamily: 'Pacifico',
                    color: Colors.white,
                  )),
              backgroundColor: Colors.red,
            ),
            body: MyApp(),
          ),
        ),
      );
    }

    class MyApp extends StatelessWidget {
      void navigateToPage() {
        print('Button pressed!!!!');
      }

      @override
      Widget build(BuildContext context) {
        return Center(
          child: Column(
            children: <Widget>[
              SizedBox(
                height: 50,
              ),
              Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                image: AssetImage("images/Ani.png"),
                fit: BoxFit.cover

                ),
                ),

              ),
              FlatButton(
                  child: Image.asset(
                    'images/li.png',
                    height: 120,
                    width: 120,
                  ),
                  onPressed: () {
                    navigateToPage();
                  }),
                ),
        ],
      ),
    );
  }
}

标签: flutterflutter-containerflutter-scaffold

解决方案


首先用容器敲击你的中心。

Container(
  decoration:BoxDecoration(
   image:DecorationImage(
     fit:BoxFit.fill
     image: AssetImage("images/Ani.png"),
 )
    )
  child:Center(
  )
)

BoxFit.fill 将使图像完全覆盖容器


推荐阅读