11

I have a scenario where I have 2 views:

  • View 1
  • View 2

On a button press from View 1, Navigator.of(context).pushnamed('/view2') is invoked, which pushes View 2 to the screen. In this case, the entire view is transitioned from View 1 to View 2.

Is it possible to have a permanent view that will still stay on screen when transitioning from View 1 to View 2? Similar to how Soundclould does it with its play controls at the bottom. Where it's always permanently displayed no matter which screens you navigate to. This diagram depicts what I'm trying to achieve: enter image description here

On iOS, I'm able to achieve this by having a Navigation Controller in a ContainerView in a ViewController, then have the permanent view occupy the bottom of the screen right below the ContainerView

user3217522
  • 5,786
  • 7
  • 26
  • 34

1 Answers1

9

You can use the same approach here. Parent widget can have two parts: Navigator and PermanentView. By pushing routes you will change only Navigator widget. Demo:

import 'package:flutter/material.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Route _onRoute(RouteSettings settings) {
    final str = settings.name.split("/")[1];
    final index = int.parse(str, onError: (s) => 0);

    return new MaterialPageRoute(
        builder: (BuildContext context) => new Home(index: index));
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[
        new Expanded(
          child: new MaterialApp(
            title: 'Flutter Demo',
            onGenerateRoute: _onRoute,
          ),
        ),
        new Container(
          height: 44.0,
          color: Colors.blueAccent,
          child: new Center(
            child: new Text("permanent view"),
          ),
        )
      ],
    );
  }
}

class Home extends StatelessWidget {
  Home({Key key, this.index}) : super(key: key);
  final int index;

  @override
  Widget build(BuildContext context) => new Scaffold(
        appBar: new AppBar(
          title: new Text("View ${index}"),
        ),
        body: new Center(
          child: new Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              new Text("View ${index}"),
              new FlatButton(
                  onPressed: () =>
                      Navigator.of(context).pushNamed("/${index + 1}"),
                  child: new Text("Push"))
            ],
          ),
        ),
      );
}

void main() {
  runApp(
    new MyApp(),
  );
}
German Saprykin
  • 6,631
  • 2
  • 29
  • 26
  • Wow awesome! Thank you! – user3217522 Aug 05 '17 at 05:02
  • How would this be done if you'd already have a `MaterialApp` but need a particular page that you'd like to have its own navigation stack as well? Nesting `MaterialApp`s in each other gives some funky results... – Bram Vanbilsen Sep 30 '17 at 12:24
  • You can create new Navigator for each page. As reference look at [test example](https://github.com/flutter/flutter/blob/c46347b6df27abdb91b8b7c63e90255fefc86a6e/packages/flutter/test/widgets/routes_test.dart#L193) or [tab view widget](https://github.com/flutter/flutter/blob/9909e773dc66608a866efa7388f39127509d0e1e/packages/flutter/lib/src/cupertino/tab_view.dart#L10) – German Saprykin Sep 30 '17 at 13:12
  • @GermanSaprykin Is it possible to load PersistentBottomSheet from Permanent View and let it cover the full screen (over Parent Widget) this is in regards to https://stackoverflow.com/questions/54118978/have-flutter-persistent-bottom-sheet-always-visible-with-minimum-height-and-go-f – skjagini Jan 18 '19 at 20:04