【发布时间】:2019-04-06 20:01:18
【问题描述】:
Flutter 新手,我正在使用底部导航来切换页面内容。根据调试,设置状态正在发生,其中正确的索引号被捕获并分配给 _selectedPage 变量。然而,一旦我跨过该代码,页面内容将保持不变,而不会更改内容,并且无论我选择什么图标,底部导航中的“主页”图标都会保持选中状态。
当我从导航栏中选择不同的图标时,我预计会有所不同的注释部分。请指教。
import 'package:flutter/material.dart';
class LatestFeed extends StatefulWidget{
@override
State<StatefulWidget> createState() => LatestFeedState();
}
class LatestFeedState extends State<LatestFeed>{
@override
Widget build(BuildContext context) {
int _selectedPage = 0;
List pageOptions = [
Text('one1', style: TextStyle(fontSize: 36),), // Expecting a change but now it always remains on this data. No change even when set state changes to different index.
Text('two', style: TextStyle(fontSize: 36),),
Text('three', style: TextStyle(fontSize: 36),),
Text('four', style: TextStyle(fontSize: 36),)
];
return MaterialApp(
home: new Scaffold(
body: pageOptions[_selectedPage],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home')
),
BottomNavigationBarItem(
icon: Icon(Icons.rss_feed),
title: Text('feed')
),
BottomNavigationBarItem(
icon: Icon(Icons.featured_play_list),
title: Text('list')
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
title: Text('settings')
),
],
currentIndex: _selectedPage, // expecting different icons to be selected based on this index which is being selected in the set state below. Not seeing any change.
onTap: (int index){
setState(() {
_selectedPage = index; // being assigned correctly yet no effect
});
},
),
),
);
}
}
【问题讨论】: