使用Flutter的永久背景图像

时间:2018-04-11 16:47:42

标签: image user-interface flutter

我正在开发一个带有2个标签视图的应用,并希望能够在2个标签之间滑动或导航时获得永久背景图像。这是小部件的代码:

class MyTabs extends StatefulWidget {
  @override
  MyTabsState createState() => new MyTabsState();
}

class MyTabsState extends State<MyTabs> with SingleTickerProviderStateMixin {

  TabController controller;

  @override
  void initState() {
    super.initState();
    controller = new TabController(length: 2, vsync: this);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Divot', style: new TextStyle(fontFamily: 'Pacifico')),
        centerTitle: true,
        backgroundColor: Colors.green,
        bottom: new TabBar(
          controller: controller,
            tabs: <Tab>[
              new Tab(icon: new Icon(Icons.golf_course)),
              new Tab(icon: new Icon(Icons.account_circle)),
            ]),
      ),
      body: new Stack(
        children: <Widget>[
          new Container(
            decoration: new BoxDecoration(
              image: new DecorationImage(image: new AssetImage("image"), fit: BoxFit.fill,),
            ),
          ),
          new TabBarView(
            controller: controller,
              children: <Widget>[
                new second.GameMenu(),
                new third.MyProfilePage(),
              ],
          )
        ],
      )
    );
  }
}

我没有错误,但我在第一个标签上获得了白色背景,在第二个标签上获得了我的AssetImage背景。我错过了什么?

2 个答案:

答案 0 :(得分:0)

在Android中,这要容易得多,您所要做的就是在XML文件中设置background属性,就这么简单。

在混乱的情况下,我建议在脚手架的body属性中的TabBarView子级中,您可以尝试传递带有背景的Widget。或尝试将大多数小部件的backgroundColor属性设置为Colors.transparent

答案 1 :(得分:0)

我知道这是一个老问题,但以防万一...

通过使用Container作为脚手架主体,并使TabBarView成为Container的子节点,我得到了想要的效果。然后,您可以使用...

decoration: BoxDecoration(
            image: DecorationImage(
              image:
                  AssetImage("some image path"),
              fit: BoxFit.cover,
              colorFilter: ColorFilter.mode(
                  Colors.black.withOpacity(0.4), BlendMode.dstATop),

创建背景,该背景出现在每个选项卡视图中:-)