import 'package:flutter/material.dart'; import 'package:mileograph_flutter/components/legs/leg_card.dart'; import 'package:mileograph_flutter/objects/objects.dart'; import 'package:mileograph_flutter/services/data_service.dart'; import 'package:provider/provider.dart'; class LocoLegsPage extends StatefulWidget { const LocoLegsPage({ super.key, required this.locoId, required this.locoLabel, }); final int locoId; final String locoLabel; @override State createState() => _LocoLegsPageState(); } class _LocoLegsPageState extends State { bool _includeNonPowering = false; late Future> _future; @override void initState() { super.initState(); _future = _fetch(); } Future> _fetch() { return context.read().fetchLegsForLoco( widget.locoId, includeNonPowering: _includeNonPowering, ); } Future _refresh() async { final items = await _fetch(); if (!mounted) return; setState(() { _future = Future.value(items); }); } @override Widget build(BuildContext context) { final titleLabel = widget.locoLabel.trim().isEmpty ? 'Loco ${widget.locoId}' : widget.locoLabel; return Scaffold( appBar: AppBar( title: Text('Legs ยท $titleLabel'), actions: [ IconButton( tooltip: 'Refresh', onPressed: _refresh, icon: const Icon(Icons.refresh), ), ], ), body: SafeArea( child: Column( children: [ Padding( padding: const EdgeInsets.fromLTRB(16, 12, 16, 8), child: Card( child: SwitchListTile( title: const Text('Include non-powering (dead-in-tow)'), subtitle: const Text('Off by default'), value: _includeNonPowering, onChanged: (val) { setState(() { _includeNonPowering = val; _future = _fetch(); }); }, ), ), ), Expanded( child: FutureBuilder>( future: _future, builder: (context, snapshot) { final items = snapshot.data ?? const []; final isLoading = snapshot.connectionState == ConnectionState.waiting; if (isLoading && items.isEmpty) { return const Center(child: CircularProgressIndicator()); } if (!isLoading && items.isEmpty) { return const Center( child: Padding( padding: EdgeInsets.all(16.0), child: Text('No legs found for this loco.'), ), ); } return RefreshIndicator( onRefresh: _refresh, child: ListView.builder( padding: const EdgeInsets.fromLTRB(16, 0, 16, 16), physics: const AlwaysScrollableScrollPhysics(), itemCount: items.length, itemBuilder: (context, index) => LegCard(leg: items[index]), ), ); }, ), ), ], ), ), ); } }