我正在开发一个具有 2 个选项卡 View 的应用程序,并且想要一个永久的背景图像,同时能够在 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 背景。我错过了什么?
最佳答案
我知道这是一个老问题,但以防万一......
我通过使用 Container 作为 Scaffold body: 并使 TabBarView 成为 Container 的 child: 获得了您想要的效果。然后你可以使用...
decoration: BoxDecoration(
image: DecorationImage(
image:
AssetImage("some image path"),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.4), BlendMode.dstATop),
创建出现在每个选项卡 View 上的背景:-)
关于image - 在 Flutter 中使用永久背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49780519/