gpt4 book ai didi

Flutter:BottomNavigationBar在选项卡更改时重建页面

转载 作者:IT老高 更新时间:2023-10-28 12:30:01 24 4
gpt4 key购买 nike

我在 Flutter 中的 BottomNavigationBar 有问题。如果我更改选项卡,我希望我的页面保持活跃。

这里是我的实现

底部导航

class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HomeState();
}
}

class _HomeState extends State<Home> {
int _currentIndex = 0;
List<Widget> _children;
final Key keyOne = PageStorageKey("IndexTabWidget");

@override
void initState() {
_children = [
IndexTabWidget(key: keyOne),
PlaceholderWidget(Colors.green),
NewsListWidget(),
ShopsTabWidget(),
PlaceholderWidget(Colors.blue),
];
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(MyApp.appName),
textTheme: Theme.of(context).textTheme.apply(
bodyColor: Colors.black,
displayColor: Colors.blue,
),
),
body: _children[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: onTabTapped,
key: IHGApp.globalKey,
fixedColor: Colors.green,
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Container(height: 0.0),
),
BottomNavigationBarItem(
icon: Icon(Icons.message),
title: Container(height: 0.0),
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Container(height: 0.0),
),
BottomNavigationBarItem(
icon: Icon(Icons.perm_contact_calendar),
title: Container(height: 0.0),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Container(height: 0.0),
),
],
),
);
}

void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}

Column buildButtonColumn(IconData icon) {
Color color = Theme.of(context).primaryColor;

return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, color: color),
],
);
}
}

这是我的索引页(第一个标签):

class IndexTabWidget extends StatefulWidget {
IndexTabWidget({Key key}) : super(key: key);

@override
State<StatefulWidget> createState() {
return new IndexTabState();
}
}

class IndexTabState extends State<IndexTabWidget>
with AutomaticKeepAliveClientMixin {
List<News> news = List();
FirestoreNewsRepo newsFirestore = FirestoreNewsRepo();

@override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: new Container(
child: new SingleChildScrollView(
child: new ConstrainedBox(
constraints: new BoxConstraints(),
child: new Column(
children: <Widget>[
HeaderWidget(
CachedNetworkImageProvider(
'https://static1.fashionbeans.com/wp-content/uploads/2018/04/50-barbershop-top-savill.jpg',
),
"",
),
AboutUsWidget(),
Padding(
padding: const EdgeInsets.all(16.0),
child: SectionTitleWidget(title: StringStorage.salonsTitle),
),
StreamBuilder(
stream: newsFirestore.observeNews(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
} else {
news = snapshot.data;
return Column(
children: <Widget>[
ShopItemWidget(
AssetImage('assets/images/picture.png'),
news[0].title,
news[0],
),
ShopItemWidget(
AssetImage('assets/images/picture1.png'),
news[1].title,
news[1],
)
],
);
}
},
),
Padding(
padding: const EdgeInsets.only(
left: 16.0, right: 16.0, bottom: 16.0),
child: SectionTitleWidget(title: StringStorage.galleryTitle),
),
GalleryCategoryCarouselWidget(),
],
),
),
),
),
);
}

@override
bool get wantKeepAlive => true;
}

因此,如果我从我的索引选项卡切换到任何其他选项卡并返回到索引选项卡,则索引选项卡将始终重建。我调试了一下,发现tab switch上总是调用build函数。

你们能帮我解决这个问题吗?

非常感谢阿尔博

最佳答案

我之前的答案都没有。

在切换选项卡时保持页面事件的解决方案是将页面包装在 IndexedStack 中。

class Tabbar extends StatefulWidget {
Tabbar({this.screens});

static const Tag = "Tabbar";
final List<Widget> screens;
@override
State<StatefulWidget> createState() {
return _TabbarState();
}
}

class _TabbarState extends State<Tabbar> {
int _currentIndex = 0;
Widget currentScreen;

@override
Widget build(BuildContext context) {
var _l10n = PackedLocalizations.of(context);

return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: widget.screens,
),
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.black,
type: BottomNavigationBarType.fixed,
onTap: onTabTapped,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.format_list_bulleted),
title: new Text(_l10n.tripsTitle),
),
BottomNavigationBarItem(
icon: new Icon(Icons.settings),
title: new Text(_l10n.settingsTitle),
)
],
),
);
}

void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
}

关于Flutter:BottomNavigationBar在选项卡更改时重建页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52598900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com