【问题标题】:How to update browser URL on route changes to non-root Navigator in flutter?如何在颤动中将浏览器 URL 更新为非根导航器的路由更改?
【发布时间】:2020-07-14 13:49:23
【问题描述】:

当我在我的颤振应用程序中使用我的嵌套导航器时,我正在尝试更新 Web URL。

默认路由 / 有一个可导航到 /sub 路由的 FlatButton。然后在_generateSubRoute -> /second

这是我的例子:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      routes: {
    // When navigating to the "/" route, build the FirstScreen widget
        '/': (context) => Scaffold(
              body: FlatButton(
                onPressed: () {
                  Navigator.of(context).pushNamed('/sub');
                },
                child: Text(
                  "Go to sub",
                ),
              ),
            ),
// When navigating to the "/second" route, build the  SecondScreen widget.
        '/sub': (context) =>
            Scaffold(body: Column(children: [Text('/sub'), Flexible(
              child: Navigator(
                onGenerateRoute: _generateSubRoute ))]))
      });
  }

  Route _generateSubRoute(RouteSettings settings) {
    if (settings.name == '/second') {
      return MaterialPageRoute(
        settings: settings,
        builder: (BuildContext context) => Text('Second screen')
      );
    }

    return MaterialPageRoute(
        settings: settings,
        builder: (BuildContext context) => Column(children: <Widget>[
          Text('/subroot/'),
          FlatButton(
            onPressed: () {
              Navigator.of(context).pushNamed('/second');
            },
            child: Text(
              "Go to second",
            ))])
    );
  }
}

此代码中的 Web url 无法正常工作。 知道如何解决这个问题吗?

【问题讨论】:

  • 尝试使用 Navigator.of(context).pushNamed("test") 它应该使用 //root.com/test 更新 url
  • 嗨,你发现了吗?

标签: flutter dart


【解决方案1】:

import 'dart:html' as html; html.window.history.pushState(null, "Home", "/");

【讨论】:

  • 无论这段代码如何回答这个问题,提倡您的解决方案是一个好习惯。
猜你喜欢
  • 2013-02-11
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多