How to rebuild a suggestionBuilder from SearchAnchor manually?

56 Views Asked by At

I want to use the SearchAnchor Widget, but unfortunately, I find no way to trigger a rebuild. I have tried many approaches to achieve something similar to what is shown in my picture checklist inside suggestionBuilder. However, it doesn't work because the widget only rebuilds if I enter new text and the Search Controller is triggered.

I attempted using stateful widgets and changing the state, but it didn't work. I also tried ValueListenableBuilder and attempted to edit the SearchController manually, which is definitely not a valid solution and also didn't work. I even tried Riverpod Provider, as shown in the provided minimal code snippet.

It's driving me crazy. Thank you for any help! :D

Minimal Example with Riverpod to reproduce (I am using Flutter 3.16.3):

import 'dart:async';
import 'dart:developer';

import 'package:flutter/material.dart';

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

part 'init_jobs_screen.g.dart';

class InitJobsScreen extends ConsumerWidget {
  const InitJobsScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    //This value i want to display
    int seconds = ref.watch(secondsCounterProvider);

    final SecondsCounter notifier = ref.watch(secondsCounterProvider.notifier);
    if (seconds == 0) {
      callFunctionEverySecond(notifier);
    }
    return Scaffold(
        appBar: AppBar(
          title: const Text('Whats going on here?!'),
        ),
        body: Column(
          children: [
            Text('Here it works as expected $seconds'),

            //Here the provider doesnt work -> It drives me quazy
            SearchAnchor.bar(
                suggestionsBuilder: (context, controller) =>
                    [Text(ref.watch(secondsCounterProvider).toString())]),
          ],
        ));
  }
}

//Provider i want to watch
@riverpod
class SecondsCounter extends _$SecondsCounter {
  @override
  int build() => 0;

  void incrementSeconds() => state++;
}

//increment seconds
void callFunctionEverySecond(SecondsCounter notifier) {
  // Call the function every second
  Timer.periodic(const Duration(seconds: 1), (timer) {
    log('increment Secondscounter');
    notifier.incrementSeconds();
  });
}

I tried stateful widgets and changed the state, which didnt work, i tried ValueListenableBuilder, i tried to edit the SearchController manually (which is defentitly not a valid solutions and although didn't work), i tried Riverpod Provider like in the provided minimal code snippet

0

There are 0 best solutions below