Persistent view with navigation bar in Flutter

I have a scenario where I have 2 views:

  • View 1
  • View 2

Clicking a button from window 1 calls Navigator.of (context) .pushnamed ('/ view2'), which clicks View 2 to the screen. In this case, the entire view goes from View 1 to View 2.

Is it possible to have a permanent view that will still remain on the screen when switching from View 1 to View 2? Similar to how Soundclould does it with the playback controls at the bottom. Where it is always shown constantly, no matter which screens you navigate to. This diagram shows what I am trying to achieve: enter image description here

On iOS I can achieve this by having a Navigation Controller in the ContainerView in the ViewController and then the persistent view takes up the bottom of the screen right below the ContainerView

+3


source to share


1 answer


You can use the same approach here. The parent widget can have two parts: Navigator and PermanentView. Clicking on the routes will only change the Navigator widget. Demo video:



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

      

+4


source







All Articles