Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

android - how to sync bottom navigation bar icon with current page in flutter

I have here 5 pages. I want when the user routes to a new page without using bottom navigation bar for the icon for that page to appear in the bottom navigation bar.

How can I do this?

this is main class:

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'Basirat.dart';
import 'Gallery.dart';
import 'MainPage.dart';
import 'BottomNav.dart';
import 'Niko_Kary.dart';
import 'Nohe.dart';

void main() {
  runApp(deldadeganApp());
}

class deldadeganApp extends StatefulWidget {
  @override
  _deldadeganAppState createState() => _deldadeganAppState();
}

class _deldadeganAppState extends State<deldadeganApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: BottomNav(),
    );
  }
}

This is bottom navigation bar class in which I manage route the plan:

import 'package:deldadeganapp/Basirat.dart';
import 'package:deldadeganapp/Gallery.dart';
import 'package:deldadeganapp/MainPage.dart';
import 'package:deldadeganapp/Niko_Kary.dart';
import 'package:deldadeganapp/Nohe.dart';
import 'package:flutter/material.dart';
import 'MainPage.dart';

class BottomNav extends StatefulWidget {
  @override
  _BottomNavState createState() => _BottomNavState();
}

class _BottomNavState extends State<BottomNav> {
  int _curentIndex = 4;
  final List<Widget> _childeren = [
    Niko_kary(),
    Nohe(),
    Gallery(),
    Basirat(),
    MainPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _childeren[_curentIndex],
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.only(
              topRight: Radius.circular(30), topLeft: Radius.circular(30)),
          boxShadow: [
            BoxShadow(color: Colors.black38, spreadRadius: 0, blurRadius: 10),
          ],
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(30.0), topRight: Radius.circular(30.0)),
          child: BottomNavigationBar(
              currentIndex: _curentIndex,
              selectedItemColor: Colors.green,
              unselectedItemColor: Colors.black87,
              type: BottomNavigationBarType.fixed,
              // backgroundColor: Colors.amberAccent,
              onTap: (index) {
                setState(() {
                  bClickindex(index);
                });
              },
              items: [
                BottomNavigationBarItem(
                  icon: Icon(Icons.payment),
                  title: Text('help'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.queue_music),
                  title: Text('Nohe'),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.photo),
                  title: Text('gallery'),
                ),
                BottomNavigationBarItem(
                    icon: Icon(Icons.event_note), title: Text('Basirat')),
                BottomNavigationBarItem(
                    icon: Icon(Icons.home), title: Text('khane'))
              ]),
        ),
      ),
    );
  }

  void bClickindex(int index) {
    setState(() {
      _curentIndex = index;
    });
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Made a short example. Dartpad example

You have to get the state object of the parent in the child widget and use it to set the state of the parent. Make sure to check if the parent is mounted before calling set state as shown in the example.

Hope this solves your problem!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...