2017-08-04 144 views
1

导航栏常驻视图我有一个场景,其中我有2次:与颤振

  • 视图1
  • 视图2

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

是否有可能有从视图1转换到视图时仍然会停留在屏幕上永久的看法?类似于Soundclould在底部播放控件时的操作方式。无论您导航到哪个屏幕,它总是永久显示。此图描绘了我想要实现: enter image description here

在iOS上,我能够在一个视图控制器一个ContainerView具有导航控制器来实现这一点,那么有永久观点占据了屏幕的底部正下方的ContainerView

回答

3

你可以在这里使用同样的方法。父窗口小部件可以有两部分:导航器和永久视图。通过推送路线,您将只更改导航器小部件。 演示:

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

太棒了!谢谢! – user3217522

+0

如果你已经有了一个'MaterialApp',但是你需要一个你想拥有自己的导航栈的特定页面,这会怎么做呢?将'MaterialApp'嵌套在对方中会产生一些时髦的结果... –

+0

您可以为每个页面创建新的导航器。作为参考来看[试验例](https://github.com/flutter/flutter/blob/c46347b6df27abdb91b8b7c63e90255fefc86a6e/packages/flutter/test/widgets/routes_test.dart#L193)或[标签视图插件](https://开头github.com/flutter/flutter/blob/9909e773dc66608a866efa7388f39127509d0e1e/packages/flutter/lib/src/cupertino/tab_view.dart#L10) – Mogol