【问题标题】:Flutter: How can I keep to my selected navigator when I'll go to a new screen and go backFlutter:当我转到新屏幕并返回时,如何保持我选择的导航器
【发布时间】:2020-03-10 00:37:02
【问题描述】:

我现在在颤动的底部导航中遇到问题。

我有四个导航“社区、Feeds、Activity、Profile”。

在我的“Feeds”导航中,我有一个名为“View Profile”的按钮,每次单击该按钮时,它都会使用

将我定向到新屏幕

"Navigator.push(context, MaterialPageRoute())"

我注意到它会在应用栏上自动生成一个“

问题是每次我单击“后退箭头”时,它都会将我重定向到导航栏上的第一个选项。

不在“Feed”导航中。

任何提示如何解决这个问题?

这是我的底部导航代码:

_getPage(int page) {
switch (page) {
case 0:
  return NewsFeed();
case 1:
  return OrgAndNews();
case 2:
  return MyActivity();
case 3:
  return Profile();
 }
}

int currentPage = 0;

void _onBottomNavBarTab(int index) {
  setState(() {
    currentPage = index;
  });

}


 return Scaffold(
  body: Container(
    child: _getPage(currentPage),
  ),
  bottomNavigationBar: Container(
    height: _height * .09,
    child: BottomNavigationBar(
      backgroundColor: Color(0xFFFFFFFF),
      fixedColor: Color(0xFF121A21),
      unselectedItemColor: Color(0xFF121A21),
      currentIndex: currentPage,
      onTap: _onBottomNavBarTab,
      items: [
        BottomNavigationBarItem(
          icon: Icon(FontAwesomeIcons.users),
          title: Padding(
            padding: const EdgeInsets.only(top: 3.0),
            child: Text('Community', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(35),
              fontWeight: FontWeight.w800),
            ),
          ),
        ),
        BottomNavigationBarItem(
          icon: Icon(FontAwesomeIcons.newspaper),
          title: Padding(
            padding: const EdgeInsets.only(top: 3.0),
            child: Center(
              child: Text('Feeds', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(35),
                  fontWeight: FontWeight.w800),),
            ),
          ),
        ),
        BottomNavigationBarItem(
          icon: Icon(FontAwesomeIcons.listUl),
          title: Padding(
            padding: const EdgeInsets.only(top: 3.0),
            child: Text('My Activity', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(35),
                fontWeight: FontWeight.w800),),
          ),
        ),
        BottomNavigationBarItem(
          icon: Icon(FontAwesomeIcons.userAlt),
          title: Padding(
            padding: const EdgeInsets.only(top: 3.0),
            child: Text('Profile', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(35),
                fontWeight: FontWeight.w800),),
          ),
        ),
      ],
    ),
  ),
);

点击“查看个人资料”时我的页面代码:

  class OrgProfile extends StatefulWidget {
 OrgProfile(this.orgName) : super();

final String orgName;
  @override
  _OrgProfileState createState() => _OrgProfileState();
 }

 class _OrgProfileState extends State<OrgProfile> {
 @override
 final db = Firestore.instance;

  Container buildItem(DocumentSnapshot doc) {
return Container(
  child: Column(
    children: <Widget>[
      Center(
        child: Padding(
          padding: const EdgeInsets.only(top: 20.0),
          child: CircleAvatar(
            radius: 70,
          ),
        ),
      ),
      Text(
        '${doc.data['Email']}',
        style: TextStyle(color: Colors.black),
      )
    ],
  ),
 );
 }

 Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text(widget.orgName),
  ),
  body: StreamBuilder<QuerySnapshot>(
      stream: db
          .collection('USERS')
          .where('Name of Organization', isEqualTo: widget.orgName)
          .snapshots(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Column(
              children: snapshot.data.documents
                  .map((doc) => buildItem(doc))
                  .toList());
        } else {
          return SizedBox();
        }
      }),
   );
  }
 }

我点击“查看个人资料”按钮时的代码:

   onPressed: () {
      Navigator.push(
        context,
           MaterialPageRoute(
           builder: (BuildContext context) => new 
      OrgProfile(
         doc.data['Name of Organization'])));
     },

我的供稿界面:

我的查看个人资料用户界面:

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    你有没有像这样使用 MaterialPage Route with Builder?

    Navigator.push(
                    context,
                    MaterialPageRoute(
                    builder: (BuildContext context) => new MyToDoThunder(),
                        ),
                      )
    

    主页代码:-

    class HomePage extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        // 
        return new HomePageState();
      }
    }
    
    class HomePageState extends State<HomePage> {
      var db = DatabaseHelper();
      int _selectedIndex = 0;
      List<bool> textColorChange = [true, false, false, false];
      final _widgetOptions = [
        StatusPageRedux(),
        RequestPage(),
        NotificationPage(),
        DashboardPage(),
      ];
    
      _bottomNavigationView() {
        return new Theme(
          isMaterialAppTheme: true,
          data: Theme.of(context)
              .copyWith(canvasColor: Theme.of(context).primaryColor),
          child: new BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            onTap: _onItemTapped,
            currentIndex: _selectedIndex,
            fixedColor: Colors.white,
            items: [
              new BottomNavigationBarItem(
                activeIcon: ThunderSvgIcons(
                    path: 'assets/icons/Status.svg', height: 20.0, color: Colors.white),
                icon: ThunderSvgIcons(
                    path: 'assets/icons/Status.svg', height: 20.0, color: Colors.white30),
                title: new Text(
                  'Status',
                  style: TextStyle(
                      color: textColorChange[0] ? Colors.white : Colors.white30),
                ),
              ),
              new BottomNavigationBarItem(
                title: new Text(
                  'Requests',
                  style: TextStyle(
                      color: textColorChange[1] ? Colors.white : Colors.white30),
                ),
                activeIcon: ThunderSvgIcons(
                    path: 'assets/icons/Requests.svg', height: 20.0, color: Colors.white),
                icon: ThunderSvgIcons(
                    path: 'assets/icons/Requests.svg',
                    height: 20.0,
                    color: Colors.white30),
              ),
              new BottomNavigationBarItem(
                activeIcon: ThunderSvgIcons(
                    path: 'assets/icons/Notifications.svg',
                    height: 20.0,
                    color: Colors.white),
                icon: ThunderSvgIcons(
                    path: 'assets/icons/Notifications.svg',
                    height: 20.0,
                    color: Colors.white30),
                title: new Text(
                  'Notifications',
                  style: TextStyle(
                      color: textColorChange[2] ? Colors.white : Colors.white30),
                ),
              ),
              new BottomNavigationBarItem(
                activeIcon: ThunderSvgIcons(
                    path: 'assets/icons/dashboard.svg',
                    height: 20.0,
                    color: Colors.white),
                icon: ThunderSvgIcons(
                    path: 'assets/icons/dashboard.svg',
                    height: 20.0,
                    color: Colors.white30),
                title: new Text(
                  'Dashboard',
                  style: TextStyle(
                      color: textColorChange[3] ? Colors.white : Colors.white30),
                ),
              ),
            ],
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
    
        return new Scaffold(
          body: new Center(child: _widgetOptions.elementAt(_selectedIndex)),
          bottomNavigationBar: _bottomNavigationView(),
        );
      }
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
          for (int i = 0; i < textColorChange.length; i++) {
            if (index == i) {
              textColorChange[i] = true;
            } else {
              textColorChange[i] = false;
            }
          }
        });
      }
    }
    

    【讨论】:

    • 它工作正常先生,问题是当我单击页面的后退箭头时,它不会将我重定向到“Feeds”导航,因为在“Feeds”导航中我有按钮转到新页面。
    • 是的,我说的是同样的......在这里检查:- ibb.co/7SjrL0f
    • 但这对先生@@ 不起作用,它将我重定向到底部导航上的第一个选项@@
    • 我已经编辑了代码,您可以查看我的主页代码...希望它对您有用...
    • 它还是一样的先生 :( 它仍然是我导航上的第一个项目 :(
    【解决方案2】:

    您必须将自己的方式添加回堆栈。 在你的 'tuloung duloung' 标题页中尝试下面的 appbar,它应该可以解决问题。 请注意,如果您的主屏幕有标签,建议您在退出“tuloung duloung”时传递您想要到达的页面的索引。 如果有帮助,请告诉我。

    AppBar(
        backgroundColor: Colors.transparent,
        centerTitle: false,
        brightness: Brightness.dark,
        title: Container(
          width: 150,
          child: Row(
           children:[
              IconButton(icon:Icons.back_arrow, 
              onpressed:() =>
               Navigator.pushReplacementNamed(context, '/Your Home_Screen');
              ),
              Text('tuloung duloung',
              style: TextStyle(
                fontWeight: FontWeight.w400,
                color: theme.primaryColor,
              )),
           ]
         ),
        ),
        automaticallyImplyLeading: false,
        iconTheme: IconThemeData(
          color: theme.primaryColor,
        ),
        actions:[ Container(
                  width: 150,
                  child: FlatButton.icon(
                      label: Text('Done'),
                      icon: Icon(Icons.check_circle),
                      onPressed: () => {
                        setState(() {
                         takingsnap = true;
                          _captureImage();
                        })
                      }),
                ),
              ]
            
      ),
    

    【讨论】:

      猜你喜欢
      • 2019-12-02
      • 2018-09-12
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2018-10-06
      • 2019-03-12
      相关资源
      最近更新 更多