liquid_go_router 0.2.0
liquid_go_router: ^0.2.0 copied to clipboard
GoRouter screen tracking for liquid_analytics — emit a liquid `screen` event on every route change, by route name or by matched path.
// GoRouter screen tracking for liquid_analytics.
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:liquid_analytics/liquid_analytics.dart';
import 'package:liquid_go_router/liquid_go_router.dart';
late final Liquid liquid;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
liquid = Liquid(sinks: [DebugSink()], consent: ConsentPolicy.allowAll);
await liquid.init();
// Option A — report GoRoute.name ("home", "product").
final router = GoRouter(
observers: [LiquidGoRouterObserver(liquid)],
routes: [
GoRoute(
path: '/',
name: 'home',
builder: (context, state) => const _Page(title: 'Home'),
routes: [
GoRoute(
path: 'product/:id',
name: 'product',
builder: (context, state) =>
_Page(title: 'Product ${state.pathParameters['id']}'),
),
],
),
],
);
// Option B — report the matched path ("/product/42") instead. Use one or the
// other; attaching both would emit two screen events per navigation.
//
// final binding = LiquidGoRouterBinding(liquid, router)..attach();
runApp(MaterialApp.router(routerConfig: router));
}
class _Page extends StatelessWidget {
const _Page({required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Center(
child: ElevatedButton(
onPressed: () => context.goNamed('product', pathParameters: {
'id': '42',
}),
child: const Text('Go to product 42'),
),
),
);
}
}