首页 > 解决方案 > Flutter 搜索委托,在文本输入中设置一个值或加载最后搜索的值

问题描述

我想知道如何在 Flutter Search 委托中为查询设置默认值,以便在启动时,用户可以更改默认值。

我已经尝试设置query@override buildLeading()但是当这样设置时,用户无法更改该值。

先感谢您


    class TheSearch extends SearchDelegate<String>{
      TheSearch({this.contextPage,this.controller,this.compressionRateSearch});
      BuildContext contextPage;
      WebViewController controller;
      final suggestions1 = ["https://www.google.com"];


      @override
      String get searchFieldLabel => "Enter a web address";

      @override
      List<Widget> buildActions(BuildContext context) {

        return [
          IconButton(icon:Icon(Icons.clear),onPressed:(){
            query = "";
          },)
        ];
      }

      @override
      Widget buildLeading(BuildContext context) {

        return IconButton(icon:AnimatedIcon(
            icon:AnimatedIcons.menu_arrow,
            progress: transitionAnimation,
        ),onPressed:(

            ){
              close(context,null);

        },);
      }



      @override
      Widget buildResults(BuildContext context) {

      }

      @override
      Widget buildSuggestions(BuildContext context) {

        final suggestions = query.isEmpty ? suggestions1 : [];

        return ListView.builder(itemBuilder: (content,index) => ListTile(
          leading:Icon(Icons.arrow_left),
          title:Text(suggestions[index])
        ),);
      }

    }

标签: flutterdartflutter-layout

解决方案


尝试这个,

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(MaterialApp(home: Home()));
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  Future<void> _showSearch() async {
    await showSearch(
      context: context,
      delegate: TheSearch(),
      query: "any query",
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Search Demo"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            onPressed: _showSearch,
          ),
        ],
      ),
    );
  }
}

class TheSearch extends SearchDelegate<String> {
  TheSearch({this.contextPage, this.controller});

  BuildContext contextPage;
  WebViewController controller;
  final suggestions1 = ["https://www.google.com"];

  @override
  String get searchFieldLabel => "Enter a web address";

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = "";
        },
      )
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: AnimatedIcon(
        icon: AnimatedIcons.menu_arrow,
        progress: transitionAnimation,
      ),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return null;
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    final suggestions = query.isEmpty ? suggestions1 : [];
    return ListView.builder(
      itemCount: suggestions.length,
      itemBuilder: (content, index) => ListTile(
          leading: Icon(Icons.arrow_left), title: Text(suggestions[index])),
    );
  }
}

推荐阅读