2

i'm new to flutter and i want to make this the proper way , the thing is i have a bottom navigation bar package i Curved_navigation_bar it has awesome looking and animation, this navigation bar should change the body of the Scaffold widget and reveal each time a new widget depending on the button clicked , what i'm trying to achieve is each time a button of the navigation bar is clicked do the following :

  • clear the body of the Scaffold
  • load the related widget

i hope this is the right approach to follow in flutter for navigation (changing the screen or the view) , if this is wrong please tell me

    class _SituationState extends State<ScreenSituation>{
         int _page = 0;
         GlobalKey _bottomNavigationKey = GlobalKey();
         @override
    Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        //if list button is clicked preveiw widget_client
        //if people button is clicked preveiw widget_people
        //if launch button is clicked preveiw launch
      ),
      bottomNavigationBar: CurvedNavigationBar(
        key: _bottomNavigationKey,
        index: 0,
        height: 75.0,
        items: <Widget>[
          Icon(Icons.list, size: 30),
          Icon(Icons.people, size: 30),
          Icon(Icons.launch, size: 30),
        ],
        color: Colors.white,
        buttonBackgroundColor: Colors.white,
        backgroundColor: Colors.blueAccent,
        animationCurve: Curves.easeInOut,
        animationDuration: Duration(milliseconds: 300),
        onTap: (index) {
          setState(() {
            _page = index;
          });
        },
      ),
    );
  }
}
Asmoun
  • 1,417
  • 5
  • 20
  • 53

1 Answers1

5

Getting your code example.

import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _page = 0;
  GlobalKey _bottomNavigationKey = GlobalKey();

  Widget bodyFunction() {
    switch (_page) {
      case 0:
        return Container(color: Colors.red);
        break;
      case 1:
        return Container(color: Colors.blue);
        break;
      case 2:
        return Container(color: Colors.orange);
        break;
      default:
        return Container(color: Colors.white);
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: bodyFunction(),
      bottomNavigationBar: CurvedNavigationBar(
        key: _bottomNavigationKey,
        index: 0,
        height: 75.0,
        items: <Widget>[
          Icon(Icons.list, size: 30),
          Icon(Icons.people, size: 30),
          Icon(Icons.launch, size: 30),
        ],
        color: Colors.white,
        buttonBackgroundColor: Colors.white,
        backgroundColor: Colors.blueAccent,
        animationCurve: Curves.easeInOut,
        animationDuration: Duration(milliseconds: 300),
        onTap: (index) {
          setState(() {
            _page = index;
          });
        },
      ),
    );
  }
}
Claudio Castro
  • 1,541
  • 12
  • 28
  • 1
    one more question please , is this the right approach to switch screen/view using the bottom navigation bar in flutter ? – Asmoun Aug 23 '20 at 18:41
  • 2
    It would be best to create classes for each body and place them separately in files to avoid getting too much in one file. You can also create an array with the classes so you wouldn't need to use the switch case – Claudio Castro Aug 23 '20 at 18:48