I am write a search appbar in flutter(1.22.5) app, this is my appbar code:
sliver: SliverAppBar(
title: Text(
'Cruise',
style: TextStyle(
color: Theme.of(context).brightness == Brightness.light ? Colors.black : Colors.white,
),
),
pinned: true,
expandedHeight: 10.0,
brightness: Brightness.light, // or use Brightness.dark
floating: true,
snap: true,
forceElevated: innerBoxIsScrolled,
actions: [
if (state.currentStoriesType == StoriesType.channels)
IconButton(icon: Icon(Icons.search), onPressed: (){
showSearch(context: context,delegate: CustomSearchDelegate());
}),
],
)),
and this is my CustomSearchDelegate
to implement the action of appbar search component:
import 'dart:math';
import 'package:flutter/material.dart';
class CustomSearchDelegate extends SearchDelegate {
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
tooltip: 'Clear',
icon: const Icon(Icons.clear),
onPressed: () {
query = '';
showSuggestions(context);
})
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
tooltip: 'Back',
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
return ListView.builder(
itemCount: Random().nextInt(10),
itemBuilder: (context, index) {
return ListTile(
title: Text('result $index'),
);
},
);
}
@override
Widget buildSuggestions(BuildContext context) {
return ListView(
children: <Widget>[
ListTile(title: Text('Suggest 01')),
ListTile(title: Text('Suggest 02')),
ListTile(title: Text('Suggest 03')),
ListTile(title: Text('Suggest 04')),
ListTile(title: Text('Suggest 05')),
],
);
}
}
now when the user search in appbar, I want to send a HTTP request to the server side and rerender the list, what I do is dispatch an action and request to server, then get the response from server side and change the flutter state, the UI would rerender. But I did not known how to send the dispatch in CustomSearchDelegate
, what should I do to fetch data in CustomSearchDelegate
class?
question from:
https://stackoverflow.com/questions/65940683/is-it-possible-to-dispath-and-fetch-data-from-server-in-flutter-when-using-custo