3

Hi i'm trying to achieve a navigation system similar to Instagram. I was wondering how i can push not full screen Widgets to Navigator also making navigator push widgets as a child of my view(to keep the bottom tabs fixed in parent widget). Took a look at this and PageRouteBuilder but understand exactly how i can do this: https://docs.flutter.io/flutter/widgets/Navigator-class.html Has anyone achieved this?

class _MainPage extends State<MainPage> {


 @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Home Sweet Home"),
      ),
      body: new Column(
        children: <Widget>[
          PUT_NAVIGATOR_RESULT_HERE,
          new Row(
            children: <Widget>[
              new RaisedButton(onPressed: addWidget, child: new Text("home")),
              new RaisedButton(onPressed: addWidget, child: new Text("discover")),
              new RaisedButton(onPressed: addWidget, child: new Text("heart")),
              new RaisedButton(onPressed: addWidget, child: new Text("user")),
            ],
          )
        ],
      ),
    );
  }
  //... other functions 
}

I've achieved what i want with a stack of Widgets but that wont work with hero :( .

behzad.robot
  • 587
  • 1
  • 5
  • 16
  • Have you considered using Bottom Navigation Bar? – salihgueler Jun 30 '18 at 11:57
  • Here is the code to test that from Flutter Gallery example. https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart – salihgueler Jun 30 '18 at 12:00
  • I've tried that but i want sth that stacks for each tab and can go to innner pages and stuff..(just like instagram) – behzad.robot Jun 30 '18 at 18:28
  • 1
    Possible duplicate of [How to use multiple navigators](https://stackoverflow.com/questions/46502751/how-to-use-multiple-navigators) or [Permanent view with navigation bar in Flutter](https://stackoverflow.com/questions/45511549/permanent-view-with-navigation-bar-in-flutter) – German Saprykin Jul 01 '18 at 13:05

1 Answers1

0

You can use a PageView widget instead

An working example:

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,
      ),
      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 bottomSelectedIndex = 0;

  List<BottomNavigationBarItem> buildBottomNavBarItems() {
    return [
      BottomNavigationBarItem(
          icon: new Icon(Icons.home),
          title: new Text('Red')
      ),
      BottomNavigationBarItem(
        icon: new Icon(Icons.search),
        title: new Text('Blue'),
      ),
      BottomNavigationBarItem(
          icon: Icon(Icons.info_outline),
          title: Text('Yellow')
      )
    ];
  }

  PageController pageController = PageController(
    initialPage: 0,
    keepPage: true,
  );

  Widget buildPageView() {
    return PageView(
      controller: pageController,
      onPageChanged: (index) {
        pageChanged(index);
      },
      children: <Widget>[
        Red(),
        Blue(),
        Yellow(),
      ],
    );
  }

  @override
  void initState() {
    super.initState();
  }

  void pageChanged(int index) {
    setState(() {
      bottomSelectedIndex = index;
    });
  }

  void bottomTapped(int index) {
    setState(() {
      bottomSelectedIndex = index;
      pageController.animateToPage(index, duration: Duration(milliseconds: 500), curve: Curves.ease);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: buildPageView(),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: bottomSelectedIndex,
        onTap: (index) {
          bottomTapped(index);
        },
        items: buildBottomNavBarItems(),
      ),
    );
  }
  }



class Red extends StatefulWidget {
  @override
  _RedState createState() => _RedState();
}

class _RedState extends State<Red> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
    );
  }
}

class Blue extends StatefulWidget {
  @override
  _BlueState createState() => _BlueState();
}

class _BlueState extends State<Blue> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blueAccent,
    );
  }
}

class Yellow extends StatefulWidget {
  @override
  _YellowState createState() => _YellowState();
}

class _YellowState extends State<Yellow> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.yellowAccent,
    );
  }}

Source : https://medium.com/@KarthikPonnam/flutter-pageview-withbottomnavigationbar-fb4c87580f6a

Mohit Shetty
  • 1,551
  • 8
  • 26