Files
abawo-bt-app/lib/pages/trainer_model_search_page.dart
2026-11-24 20:02:05 +01:00

120 lines
5.4 KiB
Dart

import 'package:abawo_bt_app/model/trainer_model.dart';
import 'package:flutter/material.dart';
class TrainerModelSearchPage extends StatefulWidget {
const TrainerModelSearchPage({super.key, this.canSelect = true, this.models});
final bool canSelect;
final Future<List<TrainerModel>>? models;
@override
State<TrainerModelSearchPage> createState() => _TrainerModelSearchPageState();
}
class _TrainerModelSearchPageState extends State<TrainerModelSearchPage> {
late Future<List<TrainerModel>> _models =
widget.models ?? TrainerModel.load();
String _query = '';
Future<void> _select(TrainerModel model) async {
final selected = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: Text(model.name),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(model.limitations),
const SizedBox(height: 12),
Text('Compatible firmware: ${model.bundleLabel}'),
const SizedBox(height: 12),
const Text(
'Your installed firmware must include this model. FTMS versions of this trainer may also work with Automatic detection.'),
const SizedBox(height: 12),
const Text(
'Support depends on the exact model and firmware revision. Not every listed model has been tested on hardware.'),
if (!widget.canSelect) ...[
const SizedBox(height: 12),
const Text('Firmware update required',
style: TextStyle(fontWeight: FontWeight.w600)),
const SizedBox(height: 6),
const Text(
'Model selection requires newer firmware. Automatic firmware downloads are not available yet. Contact abawo support for compatible firmware.'),
],
])),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Back')),
if (widget.canSelect)
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Find nearby trainer')),
],
));
if (selected == true && mounted) Navigator.pop(context, model);
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Find trainer model')),
body: SafeArea(
child: Column(children: [
Padding(
padding: const EdgeInsets.all(18),
child: TextField(
autofocus: true,
decoration: const InputDecoration(
labelText: 'Search brand or model',
prefixIcon: Icon(Icons.search),
hintText: 'For example, Kettler C12'),
onChanged: (value) => setState(() => _query = value),
)),
Expanded(
child: FutureBuilder<List<TrainerModel>>(
future: _models,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: TextButton(
onPressed: () =>
setState(() => _models = TrainerModel.load()),
child:
const Text('Could not load models. Retry')));
}
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
final models = snapshot.data!
.where((model) => model.matches(_query))
.toList();
if (models.isEmpty) {
return const Center(
child: Padding(
padding: EdgeInsets.all(24),
child: Text(
'No matching model found. Try the brand name or check whether your trainer supports FTMS.')));
}
return ListView.separated(
padding: const EdgeInsets.fromLTRB(18, 0, 18, 18),
itemCount: models.length,
separatorBuilder: (_, __) => const SizedBox(height: 10),
itemBuilder: (context, index) {
final model = models[index];
return Card(
child: ListTile(
contentPadding: const EdgeInsets.symmetric(
horizontal: 18, vertical: 8),
title: Text(model.name),
subtitle:
Text('${model.limitations}\n${model.id}'),
isThreeLine: true,
trailing: const Icon(Icons.chevron_right),
onTap: () => _select(model)));
});
})),
])),
);
}