Ride Tracking
Loading preview
Starting the Flutter app
A Flutter ride tracking screen with a drawn route, an ETA label, and a driver details panel.
import 'package:flutter/material.dart';
class RideTracking extends StatelessWidget {
const RideTracking({super.key});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scheme = theme.colorScheme;
return Scaffold(
body: Stack(
children: [
Positioned.fill(child: CustomPaint(painter: _RoutePainter(scheme))),
Positioned(
top: 0,
left: 0,
right: 0,
child: SafeArea(
child: Padding(
padding: const EdgeInsets.all(12),
child: Align(
alignment: Alignment.topLeft,
child: CircleAvatar(
backgroundColor: scheme.surface,
child: IconButton(
tooltip: 'Back',
onPressed: () => Navigator.maybePop(context),
icon: const Icon(Icons.arrow_back)),
),
),
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: scheme.surface,
borderRadius:
const BorderRadius.vertical(top: Radius.circular(24)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: scheme.primaryContainer,
borderRadius: BorderRadius.circular(20),
),
child: Text('Arriving in 4 min',
style: TextStyle(
color: scheme.onPrimaryContainer,
fontWeight: FontWeight.w600)),
),
const Spacer(),
Text('2.1 mi away',
style:
TextStyle(color: scheme.onSurfaceVariant)),
],
),
const SizedBox(height: 16),
Row(
children: [
const CircleAvatar(radius: 26, child: Text('DK')),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Daniel Kessler',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16)),
Text('Toyota Prius · KTM 4821',
style: TextStyle(
color: scheme.onSurfaceVariant)),
const Text('4.9 ★'),
],
),
),
CircleAvatar(
backgroundColor: scheme.primaryContainer,
child: IconButton(
tooltip: 'Call',
onPressed: () => _showDemo(context, 'Call (demo)'),
icon: Icon(Icons.call,
color: scheme.onPrimaryContainer)),
),
const SizedBox(width: 8),
CircleAvatar(
backgroundColor: scheme.primaryContainer,
child: IconButton(
tooltip: 'Message',
onPressed: () => _showDemo(context, 'Message (demo)'),
icon: Icon(Icons.message,
color: scheme.onPrimaryContainer)),
),
],
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: OutlinedButton(
onPressed: () => _showDemo(context, 'Cancel ride (demo)'),
child: const Text('Cancel ride')),
),
],
),
),
),
],
),
);
}
}
class _RoutePainter extends CustomPainter {
final ColorScheme scheme;
_RoutePainter(this.scheme);
@override
void paint(Canvas canvas, Size size) {
canvas.drawRect(Offset.zero & size,
Paint()..color = scheme.surfaceContainerHighest);
final road = Paint()
..color = scheme.surface
..strokeWidth = 12
..strokeCap = StrokeCap.round;
canvas.drawLine(Offset(0, size.height * 0.3),
Offset(size.width, size.height * 0.42), road);
canvas.drawLine(Offset(size.width * 0.6, 0),
Offset(size.width * 0.5, size.height * 0.7), road);
// Route polyline
final route = Paint()
..color = scheme.primary
..strokeWidth = 6
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round;
final path = Path()
..moveTo(size.width * 0.25, size.height * 0.65)
..lineTo(size.width * 0.25, size.height * 0.42)
..lineTo(size.width * 0.55, size.height * 0.38)
..lineTo(size.width * 0.55, size.height * 0.2);
canvas.drawPath(path, route);
// Start dot
canvas.drawCircle(Offset(size.width * 0.25, size.height * 0.65), 8,
Paint()..color = scheme.primary);
// Car marker
final carBg = Paint()..color = scheme.primary;
canvas.drawCircle(Offset(size.width * 0.55, size.height * 0.2), 12, carBg);
}
@override
bool shouldRepaint(covariant _RoutePainter oldDelegate) =>
oldDelegate.scheme != scheme;
}
void _showDemo(BuildContext context, String message) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
A Stack puts a full-screen _RoutePainter behind a driver panel docked with Align. The painter draws two roads, a three-segment route and a plain circle standing in for the car, all as fractions of screen size. Nothing moves: Arriving in 4 min, 2.1 mi away and the driver details are literals, and back calls Navigator.maybePop while the others, including Cancel ride, show a demo SnackBar.
Adapting it
- Replace
_RoutePainterwith a map SDK and animate a marker from your driver location stream. - Drive the
Arriving in 4 minpill from the same stream. - Confirm in a dialog before cancelling from the
OutlinedButton.