Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

is it possible to dispath and fetch data from server in flutter when using CustomSearchDelegate

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...