All checks were successful
Flutter Schmutter / build (push) Successful in 3m57s
- fixed: duplicates on the frontpage - new: search by tag
87 lines
2.6 KiB
Dart
87 lines
2.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:f0ckapp/services/api_service.dart';
|
|
import 'package:f0ckapp/models/suggestion_model.dart';
|
|
import 'package:f0ckapp/providers/media_provider.dart';
|
|
|
|
class CustomSearchDelegate extends SearchDelegate<String> {
|
|
@override
|
|
List<Widget> buildActions(BuildContext context) {
|
|
return [
|
|
IconButton(
|
|
icon: Icon(Icons.clear),
|
|
onPressed: () {
|
|
query = '';
|
|
showSuggestions(context);
|
|
},
|
|
),
|
|
];
|
|
}
|
|
|
|
@override
|
|
Widget buildLeading(BuildContext context) {
|
|
return IconButton(
|
|
icon: Icon(Icons.arrow_back),
|
|
onPressed: () => close(context, 'null'),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget buildResults(BuildContext context) {
|
|
return Center(child: Text('Suchergebnisse für: "$query"'));
|
|
}
|
|
|
|
@override
|
|
Widget buildSuggestions(BuildContext context) {
|
|
if (query.isEmpty) {
|
|
return Container(padding: EdgeInsets.all(16.0), child: Text(''));
|
|
}
|
|
|
|
final Future<List<Suggestion>> futureSuggestions = Future.delayed(
|
|
Duration(milliseconds: 300),
|
|
() => fetchSuggestions(query),
|
|
);
|
|
|
|
return FutureBuilder<List<Suggestion>>(
|
|
future: futureSuggestions,
|
|
builder: (BuildContext context, AsyncSnapshot<List<Suggestion>> snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return Center(child: CircularProgressIndicator());
|
|
}
|
|
if (snapshot.hasError) {
|
|
return Center(child: Text("Fehler: ${snapshot.error}"));
|
|
}
|
|
if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
|
return Center(child: Text("Keine Vorschläge gefunden."));
|
|
}
|
|
|
|
final List<Suggestion> suggestions = snapshot.data!;
|
|
return Consumer(
|
|
builder: (BuildContext context, WidgetRef ref, Widget? child) {
|
|
return ListView.builder(
|
|
itemCount: suggestions.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
final Suggestion suggestion = suggestions[index];
|
|
return ListTile(
|
|
title: Text(suggestion.tag),
|
|
subtitle: Text(
|
|
'Getaggt: ${suggestion.tagged}x • Score: ${suggestion.score.toStringAsFixed(2)}',
|
|
style: TextStyle(fontSize: 12),
|
|
),
|
|
onTap: () {
|
|
ref.read(mediaProvider.notifier).setTag(suggestion.tag);
|
|
close(context, suggestion.tag);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|