Skip to content

[Web] Navigation and window href #41375

@jamesblasco

Description

@jamesblasco

Problem

Hi, I am testing a web app with flutter and the url doesn't change as expected when pushing a route from navigation.

By default the url doesn't change from https://<..>/#/ but sometimes it also appears the name of a new route like https://<..>/#/dashboard.

Captura de pantalla 2019-09-26 a las 16 17 34

Using the native chrome navigation for going forward and back doesn't work either as expected.

Proposal

I have been testing my self an example of how I would do it.

  1. If you are in the route /page1 your url would look like https://<..>/#/page1
  2. If you are in the route /page2 your url would look like https://<..>/#/page2
  3. If you change routes it updates the url

ezgif-4-29054d4381e5

import 'dart:html' as html;

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
            initialRoute: '/page1',
            onGenerateRoute: (settings)  {
               html.window.history.pushState(null, settings.name, '#' + settings.name);
               print(html.window.location.href);
               switch(settings.name) {
                 case '/page1': return MaterialPageRoute(builder: (_) => Page1());
                 case '/page2': return MaterialPageRoute(builder: (_) => Page2());
               }
               return MaterialPageRoute(builder: (_) => Page1());
            },
            title: 'History test'
    );
  }
}


class Page1 extends StatelessWidget
{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.green,
      appBar: AppBar(title: Text('Page 1'),),
      body: Center(
        child: FlatButton(
          child: Text('Go to page 2'),
          onPressed: () => {
              Navigator.of(context).pushNamed('/page2')
          },
        ),
      ),
    );
  }

}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red,
      appBar: AppBar(title: Text('Page 2'), leading: Container(),),
      body: Center(
        child: FlatButton(
          child: Text('Go back'),
          onPressed: ()  {
            if(Navigator.of(context).canPop()) {
              Navigator.pop(context);
              html.window.history.pushState(null, null, '#' + '/page1');
            } else {Navigator.pushReplacementNamed(context, '/page1');}
          },
        ),
      ),
      
    );
  }

}

(the example doesn't work with the navigation native buttons for pushing back and forward. )

Metadata

Metadata

Assignees

Labels

f: routesNavigator, Router, and related APIs.platform-webWeb applications specifically

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions