Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

don't want to have floating action button in second bottomnavigationbaritem

I have two bottomnavigationbaritem in scaffold for adding todo and viewing completed todo

here I have added floating actionbutton, but I don’t want to have it on comppletedtodo’s screen

I have used if for selected index but it is showing an error…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

here is the code


class _HomePageState extends State<HomePage> {
  int selectedindex=0;

  final tabs=[
    TodoListWidget(),
    Container(child: Text('Second Container'),),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Todo App'),),
      body:tabs[selectedindex],
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.white,
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.blue,
        currentIndex: selectedindex,
        onTap: (index){
          selectedindex=index;
          setState(() {
          });
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.fact_check_outlined),
          label: 'Todos'),
          BottomNavigationBarItem(
              label: 'Completed',
              icon: Icon(Icons.done))
          

        ],

      ),
        if(selectedindex==0)floatingActionButton:
          FloatingActionButton(
        onPressed: (){
          showDialog(
              barrierDismissible: false,
              context: context,
              builder: (ctx)=> AddTodoDialogWidget());
        },
        child: Icon(Icons.add),
        backgroundColor: Colors.orange,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
      )
    );
  }
}


>Solution :

Use like

floatingActionButton:selectedindex==0?
          FloatingActionButton():null,

And if you like to place inside widget that use children/items it will be

if(selectedindex==0)
          FloatingActionButton(... ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading