简体   繁体   English

在Flutter中使用永久背景图像

[英]Using a permanent background image with Flutter

I am working on an app with a 2 tab view and want a permanent background image while being able to swipe or navigate between the 2 tabs. 我正在使用具有2个标签视图的应用程序工作,并且想要一个永久的背景图像,同时能够在2个标签之间滑动或浏览。 Here is the code for the widget: 这是小部件的代码:

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

I get no errors, but I get a white background on the first tab, and my AssetImage background on the second tab. 我没有任何错误,但是在第一个选项卡上获得了白色背景,在第二个选项卡上获得了AssetImage背景。 What am I missing? 我想念什么?

In Android this would have been much easier, all you had to do was set the background property in the XML file and its that simple. 在Android中,这要容易得多,您所要做的就是在XML文件中设置background属性,就这么简单。

In flutter i would suggest in the body property of the Scaffold, in the TabBarView children you might try to pass Widgets with backgrounds. TabBarView ,我建议在Scaffold的body属性中,在TabBarView子级中,您可以尝试传递带有背景的Widget。 Or try setting the backgroundColor property of most of your widgets to Colors.transparent . 或者尝试将大多数小部件的backgroundColor属性设置为Colors.transparent

I know this is an old question but just in case... 我知道这是一个老问题,但以防万一。

I got the effect you want by using a Container as the Scaffold body: and making TabBarView the child: of the Container. 通过使用容器作为支架主体,并使TabBarView成为容器的子代,我得到了想要的效果。 You can then use... 然后,您可以使用...

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

to create the background which appears on each tab view :-) 创建出现在每个选项卡视图中的背景:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM