首页 文章

Flutter中带导航栏的永久视图

提问于
浏览
4

我有一个场景,我有2个视图:

  • 查看1

  • 查看2

在按钮上按下View 1,Navigator.of(context).pushnamed('/ view2')被调用,它将View 2推送到屏幕 . 在这种情况下,整个视图将从视图1转换为视图2 .

从View 1转换到View 2时,是否有可能保留在屏幕上的永久视图?类似于Soundclould在底部播放控件的方式 . 在哪里's always permanently displayed no matter which screens you navigate to. This diagram depicts what I' m试图实现:
enter image description here

在iOS上,我可以通过在ViewController中的ContainerView中安装导航控制器来实现这一点,然后让永久视图占据ContainerView正下方的屏幕底部

1 回答

  • 5

    您可以在此处使用相同的方法 . 父窗口小部件可以包含两部分:Navigator和PermanentView . 通过推送路线,您将只更改Navigator小部件 . 演示:

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

相关问题