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

Time:08-11

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...

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)),
      )
    );
  }
}


CodePudding user response:

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(... ),

CodePudding user response:

yor Scaffold should be like this:

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