Navigation with Fluro (Flutter web) - flutter

I'm trying to have navigation into pages. Trying to create some routes.
The problem I have is:
When the route changes it disappears from the URL
When I try to go back or forward in the browser it doesn't do anything.
I'm trying to use the Fluro package. I'm also trying to compare their example with mine and I don't find what is the difference.
main.dart:
void main() {
runApp(AppComponent());
}
class AppComponent extends StatefulWidget {
#override
State createState() {
return _AppComponentState();
}
}
class _AppComponentState extends State<AppComponent> {
_AppComponentState() {
final router = FluroRouter();
Routes.configureRoutes(router);
Application.router = router;
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'NexTeam',
debugShowCheckedModeBanner: false,
initialRoute: kHomeRoute,
onGenerateRoute: Application.router.generator,
);
}
}
class Application {
static FluroRouter router;
}
router.dart:
class Routes {
final router = FluroRouter();
static Handler _loginHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) => LoginPage());
static Handler _registerHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) => RegisterPage());
static Handler _homeHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) => HomePage());
static Handler _profileHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) => ProfilePage());
static Handler _notificationsHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) => NotificationsPage());
static Handler _chatHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) => ChatPage());
static void configureRoutes(FluroRouter router) {
router.define(kLoginRoute, handler: _loginHandler);
router.define(kRegisterRoute, handler: _registerHandler);
router.define(kHomeRoute, handler: _homeHandler);
router.define(kProfileRoute, handler: _profileHandler);
router.define(kNotificationsRoute, handler: _notificationsHandler);
router.define(kChatRoute, handler: _chatHandler);
}
}
function to navigate:
Application.router.navigateTo(context, kNotificationsRoute);

I still don't know why, but the issue was that all my pages started with MaterialApp, and the solution was to use Material.

Related

Pass data in navigator.pushNamed

I want to pass data from list_screen to tenancyDetailsScreen, but I stuck at the routes page.
list_screen.dart
Navigator.pushNamed(context, AppRoute.tenancyDetailsScreen,arguments:tenancy[index]);
app_route.dart
class AppRoute {
static const tenancyDetailsScreen = '/tenancyDetailsScreen';
static final _tenancyDetailsScreenRoute = Routes(tenancyDetailsScreen,TenancyDetailsScreen());
static Route? onGenerateRoutes(RouteSettings settings) {
if(settings.name == tenancyDetailsScreen){
final args = settings.arguments as Tenancy; // how to pass this args to DetailsScreen?
return _tenancyDetailsScreenRoute.materialRoute;
}
....
}
}
routes.dart
class Routes extends Equatable {
final String route;
final Widget page;
const Routes(this.route, this.page);
#override
List<Object> get props => [
route,
];
get materialRoute => MaterialPageRoute(
builder: (_) => SafeArea(child: page),
);
}
If you want to pass an arguments, you can't use custom Routes.
static Route? onGenerateRoutes(RouteSettings settings) {
if(settings.name == tenancyDetailsScreen){
final args = settings.arguments as Tenancy; // how to pass this args to DetailsScreen?
return MaterialPageRoute(
builder: (_) => SafeArea(child: TenancyDetailsScreen(tenancy: args)),
);
}
....
}

Receiving error message Bad state: No element

I am getting the following error message Bad state: No element
On investigation I found out that the notificationId is not flowing from one page to another.
Hence if you could please help me resolve this issue.
Let me know if you require anymore information from my end.
Please find below code :
notification.dart
class NotificationList extends StatelessWidget {
static const routeName = 'notification-list';
void selectCategory(BuildContext ctx, int id, String title) {
print(id);
print(title);
// id and title data flowing till here
Navigator.of(ctx).pushNamed(
NotificationDetail.routeName,
arguments: {
'notificationId': id,
'Title': title,
},
);
}
main.dart
initialRoute: '/',
routes: {
'/': (ctx) => LoginMainPage(),
NotificationList.routeName: (ctx) => NotificationList(),
NotificationDetail.routeName: (ctx) => NotificationDetail(),
},
),
notificationDetail.dart
class NotificationDetail extends StatefulWidget {
static const routeName = 'notification-detail';
#override
_NotificationDetailState createState() => _NotificationDetailState();
}
class _NotificationDetailState extends State<NotificationDetail> {
#override
Widget build(BuildContext context) {
final args =
ModalRoute.of(context).settings.arguments as Map<String, dynamic>;
final notificationId = args['notificationId'];
print(notificationId); //notificationId is coming as null
final loadednotification =
Provider.of<NotificationProvider>(context, listen: false)
.findByNotificationId(notificationId);
print(loadednotification.description);
I think "args['notificationId']" will be "args['Id'];"?

Where to handle Firebase Dynamic Links in Flutter?

I use Firebase dynamic links and also named routes. What I want is to install a global listener for the dynamic link events and forward to register page if a token is provided. In the code below I got the exception The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget. which means I have to put navigation code below the home: property of MaterialApp. But when doing this I had to implement the dynamic links event handler for earch route.
class MyApp extends StatelessWidget {
String title = "Framr";
#override
Widget build(BuildContext context) {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (linkData) {
if (linkData != null) {
try {
Navigator.pushNamed(context, '/register', arguments: linkData);
// throws: The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
} catch(e) {
print(e);
}
}
return null;
}
);
return MaterialApp(
title: "...",
home: LoginPage(),
routes: {
'/createEvent': (context) => CreateEventPage(),
'/showEvent': (context) => ShowEventPage(),
'/register': (context) => RegisterPage(),
},
);
}
}
I was able to get this work by following the example provided from the dynamic link README with the use of the no_context_navigation package or GlobalKey to workaround around the lack of context to call Navigator.pushNamed(...). Note: You don't have to use no_context_navigation. You can implement the no context routing yourself. Here's an example.
// Add this
import 'package:no_context_navigation/no_context_navigation.dart';
void main() {
runApp(MaterialApp(
title: 'Dynamic Links Example',
// Add this
navigatorKey: NavigationService.navigationKey,
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => MyHomeWidget(), // Default home route
'/helloworld': (BuildContext context) => MyHelloWorldWidget(),
},
));
}
class MyHomeWidgetState extends State<MyHomeWidget> {
.
.
.
#override
void initState() {
super.initState();
this.initDynamicLinks();
}
void initDynamicLinks() async {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
// Add this.
final NavigationService navService = NavigationService();
final Uri deepLink = dynamicLink?.link;
if (deepLink != null) {
// This doesn't work due to lack of context
// Navigator.pushNamed(context, deepLink.path);
// Use this instead
navService.pushNamed('/helloworld', args: dynamicLink);
}
},
onError: (OnLinkErrorException e) async {
print('onLinkError');
print(e.message);
}
);
final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;
if (deepLink != null) {
// This doesn't work due to lack of context
// Navigator.pushNamed(context, deepLink.path);
// Use this instead
navService.pushNamed('/helloworld', args: dynamicLink);
}
}
.
.
.
}
// pubspec.yaml
no_context_navigation: ^1.0.4

Flutter web url navigation

I would like to know how can I navigate to a URL in my Flutter web app.
Currently am using the Navigator.of(context).push(MaterialPageRoute(...)); and I only get localhost:5354/#/ in the address bar.
Also I would like to know how I can I navigate to a particular URL directly by just pasting the URL into the browser's addresses bar.
You need to use named routes instead of directly using classes to routes.
You can use this package named fluro https://pub.dev/packages/fluro
or else you can use default navigation that flutter provides.
with fluro you can do something like this
main.dart
import '../routes/routes.dart';
void main() {
FluroRouter.setupRouter();
// run app
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
onGenerateRoute: FluroRouter.router.generator,
);
}
}
routes.dart
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
class FluroRouter {
static Router router = Router();
static Handler _storyhandler = Handler(
handlerFunc: (BuildContext context, Map<String, dynamic> params) =>
HomeView(id: params['id'][0]));
static Handler _homehandler = Handler(
handlerFunc: (BuildContext context, Map<String, dynamic> params) =>
Home());
static void setupRouter() {
router.define(
'/',
handler: _homehandler,
);
router.define(
'/story/:id',
handler: _storyhandler,
);
}
}
you can also define routes with query parameters.
Hope this helps!
you must use of Navigator v2 for Web.
see more info: here and here

How to pass parameters to flutter web app

After hours of searching about the topic and due to lack of documentation on Flutter Web I am asking this question.
I was trying to create a web app using flutter and had an requirement where URL such as below
website.com/user/someUserCode
would be called and an page will be launched where the data (someUserCode) will be passed to the page
but haven't got any solutions yet to resolve it.
so just rounding it all up,
How to pass and fetch the data using (get / post) methods to flutter web app?
EDIT 1
What all I know / have tried yet
I am using below code to read if some parameters are being to some class file
final Map<String, String> params = Uri.parse(html.window.location.href).queryParameters;
String data = params["userData"];
all this actually solves the Fetch part of my question (maybe)
but the part where that data will be passed to the page via URL is still missing.
EDIT 2
Since I haven't got any replies and was not able to find anything i raised an ticket on Flutter GitHub page here
anyone else looking for the same issue can track it there (if it gets resolve)
May you could do it in a easy way:
import 'dart:html';
import 'package:flutter/material.dart';
import 'home_page.dart';
void getParams() {
var uri = Uri.dataFromString(window.location.href);
Map<String, String> params = uri.queryParameters;
var origin = params['origin'];
var destiny = params['destiny'];
print(origin);
print(destiny);
}
void main() {
getParams();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Your app',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
And then call it from browser:
http://localhost:52695/?origin=pointA&destiny=pointB
Output:
pointA
pointB
I tried the above method from #Mariano Zorrilla but it still opened the pages in order:
/
/user
/user/yFbOfUAwx1OCC93INK8O7VqgBXq2
I have found Fluro, and works efficiently and cleanly you only need to add one routing file and do all the routing in one file rather than editing every page you want to route to, here's how you would implement it:
main.dart
void main() {
FluroRouter.setupRouter();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Website Title',
onGenerateRoute: FluroRouter.router.generator
);
}
}
fluro_router.dart
class FluroRouter {
static Router router = Router();
//Define your routers here
static void setupRouter() {
router.define('/', handler: _homeHandler);
router.define('/login', handler: _loginHandler);
router.define('/online-enquiry/:userId', handler: _userHandler);
}
//Add your handlers here
static Handler _homeHandler = Handler(handlerFunc: (context, Map<String, dynamic> params) => Home());
static Handler _loginHandler = Handler(handlerFunc: (context, Map<String, dynamic> params) => Login());
static Handler _userHandler = Handler(handlerFunc: (context, Map<String, dynamic> params) => UserProfile(userID: params['userId'].first));
}
Source
You can get everything (paths, parameters, etc) from onGenerateRoute. Your Home will be / and everything from there can be grabbed and used to redirect users.
My approach to solve this is the following. Your base App() should be like:
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Website Title',
onGenerateRoute: (settings) => NavigatorRoute.route(settings.name),
);
}
}
and the class NavigatorRoute will be:
class NavigatorRoute extends StatefulWidget {
final String path;
static Route<dynamic> route(String path) {
return SimpleRoute(
name: '', // this one is always empty as you didn't route yet
title: 'Website Title',
builder: (_) => NavigatorRoute(path: path),
animated: false
);
}
const NavigatorRoute({Key key, this.path}) : super(key: key);
#override
_NavigatorRouteState createState() => _NavigatorRouteState();
}
class _NavigatorRouteState extends State<NavigatorRoute> {
#override
void initState() {
super.initState();
Future.microtask(() {
if (widget.path == '/') {
Navigator.of(context).pushAndRemoveUntil(HomeScreen.route(false), (_) => false);
return;
} else if (widget.path == '/user') {
Navigator.of(context).pushAndRemoveUntil(UserScreen.route(false), (_) => false);
return;
} else if (widget.path.contains('/user/')) {
Navigator.of(context).pushAndRemoveUntil(UserScreen.routeCode(widget.path.split('/')[2]), (_) => false);
return;
} else if (widget.path == '/about') {
Navigator.of(context).pushAndRemoveUntil(AboutScreen.route(), (_) => false);
return;
} else {
Navigator.of(context).pushAndRemoveUntil(HomeScreen.route(), (_) => false);
return;
}
});
}
#override
Widget build(BuildContext context) {
return SizedBox();
}
}
The code for the SimpleRoute is:
class SimpleRoute extends PageRoute {
SimpleRoute({#required String name, #required this.title, #required this.builder, #required this.animated})
: super(settings: RouteSettings(name: name));
final String title;
final WidgetBuilder builder;
final bool animated;
#override
Color get barrierColor => null;
#override
String get barrierLabel => null;
#override
bool get maintainState => true;
#override
Duration get transitionDuration => Duration(milliseconds: 200);
#override
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
return animated
? FadeTransition(
opacity: animation,
child: Title(
title: this.title,
color: Theme.of(context).primaryColor,
child: builder(context),
),
)
: Title(
title: this.title,
color: Theme.of(context).primaryColor,
child: builder(context),
);
}
}
So, finally... if you want to easily open one of your screens, you can do:
class HomeScreen extends StatefulWidget {
static Route<dynamic> route(bool animated) {
return SimpleRoute(name: '/', title: 'Home', builder: (_) => HomeScreen(), animated: animated);
}
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
...
}
The routeCode could be:
static Route<dynamic> routeCode(String id) {
return SimpleRoute(name: '/user/$id', title: 'User', builder: (_) => UserScreen(id: id), animated: false);
}
The main benefit of doing this is avoiding the stack of pages generated by accessing the last screen.
For example, if you're using directly the onGenerateRoute for "www.mywebsite.com/user/userId/edit" then Flutter will open:
Home Screen
User Screen
UserId Screen
Edit Screen
but with this approach, only "Edit Screen" will be open.