Flutter Dart:底部导航子一次完成初始化,而仅在第一次点击时初始化

问题描述

我在项目底部导航,子项在indexedItems中。但是问题在于应用程序启动时会初始化其所有子项。就像iOS中的行为一样,它会初始化第一个孩子,而在选项卡上第一次点击时会初始化其他孩子。之后,它们将不会重新初始化。我如何实现相同的行为?

import '../../UI/logout/logoutScreen.dart';
import 'package:Flutter/material.dart';
import '../MyTask/MyTaskListScreen.dart';
import './../Home/HomeScreen.dart';
import 'package:hexcolor/hexcolor.dart';

class RootScreen extends StatefulWidget {
  @override
  _RootScreenState createState() => _RootScreenState();
}

class _RootScreenState extends State<RootScreen> {
  final List<Widget> childScreens = [
    HomeScreen(),MyTaskListScreen(),logoutScreen()
  ];

  int _selectedindex = 0;

  void _selectPage(int index,context) {
    if (index == 2) {
      logoutScreen().showlogoutView(context);
      return;
    }

    this.setState(() {
      this._selectedindex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: IndexedStack(
          index: this._selectedindex,children: this.childScreens,),bottomNavigationBar: BottomNavigationBar(
          onTap: (index) => this._selectPage(index,context),backgroundColor: Hexcolor("#f2f2f2"),unselectedItemColor: Colors.grey,selectedItemColor: Colors.blue,currentIndex: this._selectedindex,type: BottomNavigationBarType.fixed,showSelectedLabels: false,showUnselectedLabels: false,items: [
            BottomNavigationBarItem(
                activeIcon: ImageIcon(
                  Assetimage(
                      "lib/Source/Resources/Assets/Root/registration/active/activeRegistration.png"),color: Colors.black,icon: ImageIcon(
                  Assetimage(
                      "lib/Source/Resources/Assets/Root/registration/default/registration.png"),label: ""),BottomNavigationBarItem(
                activeIcon: ImageIcon(
                  Assetimage(
                      "lib/Source/Resources/Assets/Root/myTask/active/myTask.png"),icon: ImageIcon(
                  Assetimage(
                      "lib/Source/Resources/Assets/Root/myTask/default/myTask.png"),BottomNavigationBarItem(
                backgroundColor: Colors.lightBlue,icon: Icon(Icons.logout),label: "")
          ],));
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...