- fixed: duplicates on the frontpage - new: search by tag
This commit is contained in:
		
							
								
								
									
										86
									
								
								lib/utils/customsearchdelegate_util.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								lib/utils/customsearchdelegate_util.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,86 @@
 | 
			
		||||
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);
 | 
			
		||||
                  },
 | 
			
		||||
                );
 | 
			
		||||
              },
 | 
			
		||||
            );
 | 
			
		||||
          },
 | 
			
		||||
        );
 | 
			
		||||
      },
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user