Dart 无法根据状态更改页面

Dart 无法根据状态更改页面,dart,flutter,Dart,Flutter,新的颤振和我使用底部导航切换页面内容。根据调试,设置状态发生,此时将捕获正确的索引号并将其分配给_selectedPage变量。然而,一旦我跳过该代码,页面内容将保持不变,而不会更改内容,并且无论我选择什么图标,底部导航中的“主页”图标都将保持选中状态 当我从导航栏中选择不同的图标时,我期望出现差异的已注释部分。请给我一些建议 import 'package:flutter/material.dart'; class LatestFeed extends StatefulWidget{ @

新的颤振和我使用底部导航切换页面内容。根据调试,设置状态发生,此时将捕获正确的索引号并将其分配给_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
              });
            },
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
类LatestFeed扩展StatefulWidget{
@凌驾
State createState()=>LatestFeedState();
}
类LatestFeedState扩展了状态{
@凌驾
小部件构建(构建上下文){
int _selectedPage=0;
列表页面选项=[
Text('one1',style:TextStyle(fontSize:36),),//需要更改,但现在它始终保留在此数据上。即使设置状态更改为其他索引,也不会更改。
文本('two',样式:TextStyle(fontSize:36),),
文本('three',样式:TextStyle(fontSize:36),),
文本('four',样式:TextStyle(fontSize:36),)
];
返回材料PP(
家:新脚手架(
正文:页面选项[\u selectedPage],
底部导航栏:底部导航栏(
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
图标:图标(Icons.home),
标题:文本(“主页”)
),
底部导航气压计(
图标:图标(Icons.rss_提要),
标题:文本(“提要”)
),
底部导航气压计(
图标:图标(图标。特色播放列表),
标题:文本(“列表”)
),
底部导航气压计(
图标:图标(图标.设置),
标题:文本(“设置”)
),
],
currentIndex:_selectedPage,//希望根据在下面的设置状态下选择的此索引选择不同的图标。未看到任何更改。
onTap:(int索引){
设置状态(){
_selectedPage=index;//已正确分配,但没有效果
});
},
),
),
);
}
}

将_selectPage和pageOptions置于initState或构建方法之外,以防止在调用setState时重新生成

int _selectedPage;
List<Widget> pageOptions;
    @override
      void initState() {
        super.initState();
         _selectedPage = 0;
        pageOptions = [
          Text('one1', style: TextStyle(fontSize: 36),), // always remains on this data. No change even when set stae changes to different index.
          Text('two', style: TextStyle(fontSize: 36),),
          Text('three', style: TextStyle(fontSize: 36),),
          Text('four', style: TextStyle(fontSize: 36),)
        ];
      }  
int\u选择的页面;
列出页面选项;
@凌驾
void initState(){
super.initState();
_selectedPage=0;
页面选项=[
Text('one1',style:TextStyle(fontSize:36),),//始终保留在此数据上。即使set state更改为其他索引,也不会更改。
文本('two',样式:TextStyle(fontSize:36),),
文本('three',样式:TextStyle(fontSize:36),),
文本('four',样式:TextStyle(fontSize:36),)
];
}  

无论何时调用setState,都会有一些更改导致小部件重新生成。这意味着将再次调用生成方法。当它被调用时,
\u selectedPage
将再次设置为0,并且pageOptions[0]将始终是所选的小部件。

我可以尝试一下,但有点困惑为什么这会起作用。我想在设置状态时更改它。这不是要将数据锁定在永远不变的位置吗?让我更新我的答案,以便在您尝试时更好地解释。是的,我明白您的意思。现在,它不会每次都初始化并返回初始设置。谢谢