-
Notifications
You must be signed in to change notification settings - Fork 30k
Closed
Labels
f: routesNavigator, Router, and related APIs.Navigator, Router, and related APIs.platform-webWeb applications specificallyWeb applications specifically
Milestone
Description
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.
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.
- If you are in the route /page1 your url would look like https://<..>/#/page1
- If you are in the route /page2 your url would look like https://<..>/#/page2
- If you change routes it updates the url
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. )
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
f: routesNavigator, Router, and related APIs.Navigator, Router, and related APIs.platform-webWeb applications specificallyWeb applications specifically

