Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 颤振-底部导航栏更改页面,但未选择图标_Android_Ios_Flutter_Dart - Fatal编程技术网

Android 颤振-底部导航栏更改页面,但未选择图标

Android 颤振-底部导航栏更改页面,但未选择图标,android,ios,flutter,dart,Android,Ios,Flutter,Dart,基本上,我有一个主小部件,它有一个this.\u bottomNavigationBar,当屏幕改变时,我将它传递到新屏幕。然后,每个屏幕都有自己的Scaffold和AppBar 听起来很好,但工作不正常 当我单击以更改所选选项时,它会更改屏幕,但所选图标保持不变。如果我打印this.currentIndex,它会正常更改索引。我不希望发生的事情,因为当我声明this.\u bottomNavigationBar时,currentIndex属性的定义如下:currentIndex:this.cu

基本上,我有一个主小部件,它有一个
this.\u bottomNavigationBar
,当屏幕改变时,我将它传递到新屏幕。然后,每个屏幕都有自己的
Scaffold
AppBar

听起来很好,但工作不正常

当我单击以更改所选选项时,它会更改屏幕,但所选图标保持不变。如果我打印
this.currentIndex
,它会正常更改索引。我不希望发生的事情,因为当我声明
this.\u bottomNavigationBar
时,
currentIndex
属性的定义如下:
currentIndex:this.currentIndex
,因此如果图标没有更改,那么变量也不会更改

因此,我意识到我可能会原谅在onTap上使用
setState()
方法,但事实并非如此

我只是不知道为什么会这样。我想可能是关于
设置状态
范围的问题吧?为了更好地说明这一点,我将在下面留下我的代码

霍姆·达特
导入“包装:颤振/材料.省道”;
导入“package:font_awesome_flatter/font_awesome_flatter.dart”;
导入“landing.dart”;
const List homeScreenNavbarItems=[
底部导航气压计(
图标:图标(FontAwesomeIcons.home),
标题:文本(“”),
),
底部导航气压计(
图标:图标(Icons.search),
标题:文本(“”),
),
底部导航气压计(
图标:图标(图标.书签),
标题:文本(“”),
),
底部导航气压计(
图标:图标(Icons.person),
标题:文本(“”),
),
];
类Home扩展了StatefulWidget{
@凌驾
状态createState(){
返回家乡();
}
}
类(HomeState扩展状态){
int currentIndex=1;
底部导航栏_底部导航栏;
列出家庭纲领;
无效onNavbarTapped(整数索引){
设置状态(){
this.currentIndex=索引;
});
}
@凌驾
void initState(){
这是。_bottomNavigationBar=bottomNavigationBar(
showSelectedLabels:false,
显示未选择的标签:false,
选择EditemColor:Color(0xFF000000),
unselectedItemColor:Color(0xFFB3),
currentIndex:this.currentIndex,
项目:家庭用品,
onTap:this.onNavbarTapped,
);
这是我的信条=[
着陆屏幕(此._底部导航栏),
脚手架(
底部导航栏:这个,
)
];
super.initState();
}
@凌驾
小部件构建(构建上下文){
打印(此.currentIndex);
返回此.homeScreens.elementAt(此.currentIndex);
}
}
着陆,飞镖
导入“包装:颤振/材料.省道”;
导入“package:font_awesome_flatter/font_awesome_flatter.dart”;
类LandingScreen扩展StatefulWidget{
最终底部导航栏_底部导航栏;
着陆屏幕(此._底部导航栏);
@凌驾
状态createState(){
返回_LandingScreenState();
}
}
类_LandingScreenState扩展状态{
@凌驾
小部件构建(构建上下文){
返回DefaultTabController(
长度:3,
孩子:脚手架(
appBar:appBar(
标题:文本(“a”),
底部:选项卡栏(
选项卡:[
选项卡(图标:图标(图标方向车)),
选项卡(图标:图标(图标方向和交通)),
选项卡(图标:图标(图标方向)),
],
),
),
正文:选项卡视图(
儿童:[
图标(图标、方向和汽车),
图标(图标、方向和交通),
图标(图标、方向和自行车),
],
),
bottomNavigationBar:widget.\u bottomNavigationBar,
),
);
}
}
main.dart
文件只是调用
Home
小部件,所以我没有把它放在这里

编辑
我删除了initState方法的所有内容,然后将其放入Widget构建方法中,工作正常,但我不知道确切原因……在我看来,这个问题与上下文有关。您可以创建一个新类并将HomeScreennavaritems放入其中。请参考我的答案,我认为,这个问题与上下文有关。您可以创建一个新类并将HomeScreenAvaritems放入其中。请参考我的答案
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'landing.dart';

const List<BottomNavigationBarItem> homeScreenNavbarItems = [
  BottomNavigationBarItem(
    icon: Icon(FontAwesomeIcons.home),
    title: Text(''),
  ),
  BottomNavigationBarItem(
    icon: Icon(Icons.search),
    title: Text(''),
  ),
  BottomNavigationBarItem(
    icon: Icon(Icons.bookmark),
    title: Text(''),
  ),
  BottomNavigationBarItem(
    icon: Icon(Icons.person),
    title: Text(''),
  ),
];

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

class _HomeState extends State<Home> {
  int currentIndex = 1;
  BottomNavigationBar _bottomNavigationBar;
  List homeScreens;

  void onNavbarTapped(int index) {
    setState(() {
      this.currentIndex = index;
    });
  }

  @override
  void initState() {
    this._bottomNavigationBar = BottomNavigationBar(
      showSelectedLabels: false,
      showUnselectedLabels: false,
      selectedItemColor: Color(0xFF000000),
      unselectedItemColor: Color(0xFFb3b3b3),
      currentIndex: this.currentIndex,
      items: homeScreenNavbarItems,
      onTap: this.onNavbarTapped,
    );

    this.homeScreens = [
      LandingScreen(this._bottomNavigationBar),
      Scaffold(
        bottomNavigationBar: this._bottomNavigationBar,
      )
    ];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print(this.currentIndex);
    return this.homeScreens.elementAt(this.currentIndex);
  }
}
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class LandingScreen extends StatefulWidget {
  final BottomNavigationBar _bottomNavigationBar;

  LandingScreen(this._bottomNavigationBar);

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

class _LandingScreenState extends State<LandingScreen> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          title: Text('a'),
          bottom: TabBar(
            tabs: [
              Tab(icon: Icon(Icons.directions_car)),
              Tab(icon: Icon(Icons.directions_transit)),
              Tab(icon: Icon(Icons.directions_bike)),
            ],
          ),
        ),
        body: TabBarView(
          children: [
            Icon(Icons.directions_car),
            Icon(Icons.directions_transit),
            Icon(Icons.directions_bike),
          ],
        ),
        bottomNavigationBar: widget._bottomNavigationBar,
      ),
    );
  }
}