简体   繁体   English

Flutter 搜索嵌套对象列表

[英]Flutter search nested list of objects

I am building a Flutter app for selling alcohol/bottle delivery service.我正在构建一个 Flutter 应用程序来销售酒精/瓶子运送服务。 I'm having issues trying to search a beverage in the search bar.我在尝试在搜索栏中搜索饮料时遇到问题。 I have put the bottles in a nested list because the bottles have different categories.我将瓶子放在嵌套列表中,因为瓶子有不同的类别。 When I try to search for a bottle nothing actually comes up in the search bar.当我尝试搜索瓶子时,搜索栏中实际上没有出现任何内容。 I guess I have something to fix in my code.我想我的代码中有一些东西要修复。 I define the nested list categories.我定义了嵌套列表类别。 Then I access the bottle object using the method getBottle() method and return a list of bottle objects which is assigned to a new list called allBottles.然后我使用 getBottle() 方法访问瓶子 object 并返回一个瓶子对象列表,该列表分配给一个名为 allBottles 的新列表。 The retrieved bottle object is then used in a search query.然后将检索到的瓶子 object 用于搜索查询。 However I get nothing when I type in the searchbar.但是,当我在搜索栏中输入时,我什么也得不到。 Can anyone please assist.任何人都可以请帮忙。 Below is my code下面是我的代码

This image shows the search bar with the hintext此图像显示带有提示文本的搜索栏

在此处输入图像描述

This image shows the search bar with the searched text此图像显示带有搜索文本的搜索栏

在此处输入图像描述

class ProductProvider with ChangeNotifier {
  final List<BottleCategory> categories = [
    BottleCategory(categoryNo: 1, bottleType: 'Beers', bottleList: [
      Bottle(
          id: 'AMST',
          bottleName: 'Amstel Lager',
          image: 'lib/assets/images/AmstelLager.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BLKL',
          bottleName: 'Black Label',
          image: 'lib/assets/images/BlackLabel.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CSTL',
          bottleName: 'Castle Lite',
          image: 'lib/assets/images/CastleLite.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRNE',
          bottleName: 'Corona Extra',
          image: 'lib/assets/images/CoronaExtra.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNKN',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'WDHK',
          bottleName: 'Windhoek Draught',
          image: 'lib/assets/images/WindhoekDraught.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 2, bottleType: 'Brandy', bottleList: [
      Bottle(
          id: 'KLPB',
          bottleName: 'Klipdrift Brandy',
          image: 'lib/assets/images/KlipdriftBrandy.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'KWV3',
          bottleName: 'Kwv Brandy 3 years',
          image: 'lib/assets/images/KwvBrandy3years.jpg',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 3, bottleType: 'Ciders', bottleList: [
      Bottle(
          id: 'BLGT',
          bottleName: 'Belgravia Gin and Tonic',
          image: 'lib/assets/images/BelgraviaGinAndTonic.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BRNB',
          bottleName: 'Bernini Blush',
          image: 'lib/assets/images/BerniniBlush.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BRTF',
          bottleName: 'Brutal Fruit',
          image: 'lib/assets/images/BrutalFruit.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'FLYF',
          bottleName: 'Flying Fish',
          image: 'lib/assets/images/FlyingFish.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNTD',
          bottleName: 'Hunters Dry',
          image: 'lib/assets/images/HuntersDry.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNTG',
          bottleName: 'Hunters Gold',
          image: 'lib/assets/images/HuntersGold.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'ICET',
          bottleName: 'Ice Tropez',
          image: 'lib/assets/images/IceTropez.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RDSB',
          bottleName: 'Red Square Blue Ice',
          image: 'lib/assets/images/RedSquareBlueIce.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SRMG',
          bottleName: 'Smirnoff Guarana',
          image: 'lib/assets/images/SmirnoffGuarana.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SRMS',
          bottleName: 'Smirnoff Storm',
          image: 'lib/assets/images/SmirnoffStorm.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'STRB',
          bottleName: 'Strong bow',
          image: 'lib/assets/images/Strongbow.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 4, bottleType: 'Cocktails', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 5, bottleType: 'Cognac', bottleList: [
      Bottle(
          id: 'CRVV',
          bottleName: 'Courvoisier VS',
          image: 'lib/assets/images/CourvoisierVS.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNSY',
          bottleName: 'Hennesy',
          image: 'lib/assets/images/HennesyCognac.jpeg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'HNVC',
          bottleName: 'Honor VS Cognac',
          image: 'lib/assets/images/HonorVSCognac.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RMVS',
          bottleName: 'Remy Martin VSOP Cognac',
          image: 'lib/assets/images/RemyMartinVSOPCognac.jpg',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 6, bottleType: 'Dry Red Wine', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 7, bottleType: 'Gin', bottleList: [
      Bottle(
          id: 'BLGN',
          bottleName: 'Belgravia Gin',
          image: 'lib/assets/images/BelgraviaGin.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BMBS',
          bottleName: 'Bombay Sapphire Gin',
          image: 'lib/assets/images/BombaySapphireGin.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'GDLG',
          bottleName: 'Gordon London Dry Gin',
          image: 'lib/assets/images/GordonLondonDryGin.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'TNGN',
          bottleName: 'Tanqueray Gin',
          image: 'lib/assets/images/TanquerayGin.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 8, bottleType: 'Liqueur', bottleList: [
      Bottle(
          id: 'AMLC',
          bottleName: 'Amarula Cream',
          image: 'lib/assets/images/AmarulaCream.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'JAGM',
          bottleName: 'Jagermeister',
          image: 'lib/assets/images/Jagermeister.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'STWL',
          bottleName: 'Strawberry Lips',
          image: 'lib/assets/images/StrawberryLips.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 9, bottleType: 'Soft Drinks', bottleList: [
      Bottle(
          id: 'APPT',
          bottleName: 'Appletiser',
          image: 'lib/assets/images/Appletiser.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'COCA',
          bottleName: 'Coca Cola',
          image: 'lib/assets/images/CocaCola.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'GRPT',
          bottleName: 'Grapetiser',
          image: 'lib/assets/images/Grapetiser.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SPRT',
          bottleName: 'Sprite',
          image: 'lib/assets/images/Sprite.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'TNWT',
          bottleName: 'Tonic Water',
          image: 'lib/assets/images/TonicWater.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 10, bottleType: 'Sparkling', bottleList: [
      Bottle(
          id: 'ARMG',
          bottleName: 'Armand De Brignac Brut Gold',
          image: 'lib/assets/images/ArmandDeBrignacBrutGold.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'DMPL',
          bottleName: 'Dom Perignon Luminous',
          image: 'lib/assets/images/DomPerignonLuminous.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'MCBI',
          bottleName: 'Moet and Chandon Brut Imperial',
          image: 'lib/assets/images/MoetAndChandonBrutImperial.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'MCNI',
          bottleName: 'Moet and Chandon Nectar Imperial',
          image: 'lib/assets/images/MoetAndChandonNectarImperial.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'MNIR',
          bottleName: 'Moet and Chandon Nectar Imperial Rose',
          image: 'lib/assets/images/MoetAndChandonNectarImperialRose.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'VVCB',
          bottleName: 'Veuve Clicquot Brut',
          image: 'lib/assets/images/VeuveClicquotBrut.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'VVCR',
          bottleName: 'Veuve Clicquot Rich',
          image: 'lib/assets/images/VeuveClicquotRich.jpg',
          time: 20,
          price: 274.00)
    ]),
    BottleCategory(categoryNo: 11, bottleType: 'Vodka', bottleList: [
      Bottle(
          id: '1818',
          bottleName: '1818 Vodka',
          image: 'lib/assets/images/1818-original.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'ABSL',
          bottleName: 'Absolut Vodka',
          image: 'lib/assets/images/AbsolutVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'BLVD',
          bottleName: 'Belvedere Vodka',
          image: 'lib/assets/images/BelvedereVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRCV',
          bottleName: 'Ciroc Vodka',
          image: 'lib/assets/images/CirocVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRZB',
          bottleName: 'Cruz Banana',
          image: 'lib/assets/images/CruzBanana.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRZV',
          bottleName: 'Cruz Vintage',
          image: 'lib/assets/images/CruzVintage.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'CRZW',
          bottleName: 'Cruz Watermelon',
          image: 'lib/assets/images/CruzWatermelon.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RDSV',
          bottleName: 'Red Square Vodka',
          image: 'lib/assets/images/RedSquareVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'RUSB',
          bottleName: 'Russian Bear Vodka',
          image: 'lib/assets/images/RussianBearVodka.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: 'SKYY',
          bottleName: 'Skyy Vodka',
          image: 'lib/assets/images/SkyyVodka.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 12, bottleType: 'White Wine', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Heineken',
          image: 'lib/assets/images/HeinekenOriginalBottle.png',
          time: 20,
          price: 274.00),
    ]),
    BottleCategory(categoryNo: 13, bottleType: 'Whiskey', bottleList: [
      Bottle(
          id: '',
          bottleName: 'Bells Scotch',
          image: 'lib/assets/images/BellsScotch.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'J & B Whiskey',
          image: 'lib/assets/images/JBWhiskey.png',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Jameson',
          image: 'lib/assets/images/Jameson.jpg',
          time: 20,
          price: 274.00),
      Bottle(
          id: '',
          bottleName: 'Johnnie Walker Black Label',
          image: 'lib/assets/images/JohnnieWalkerBlackLabel.jpg',
          time: 20,
          price: 274.00),
    ]),
  ];
  List<Bottle> getBottleItem() {
    List<Bottle> localBottles = [];
    for (var x = 0; x < categories.length; x++) {
      var currentElement = categories[x].bottleList;
      for (var y = 0; y < currentElement.length; y++) {
        var bottleElement = currentElement[y];
        localBottles.add(bottleElement);
      }
    }
    print(localBottles);
    return localBottles;
  }

  late List<Bottle> allBottles = getBottleItem();
  late List<Bottle> bottles;
  String query = " ";

  void searchBottle(String query) {
    final bottles = allBottles.where((bottle) {
      final titleLower = bottle.bottleName.toLowerCase();
      final searchLower = query.toLowerCase();

      return titleLower.contains(searchLower);
    }).toList();

    setState(() {
      this.bottles = bottles;
      this.query = query;
    });
  }

  Widget buildSearch() {
    return SearchWidget(
      text: query,
      hintText: 'Search for beverages',
      onChanged: searchBottle,
    );
  }
}

Code for searchWidget: searchWidget 的代码:

class SearchWidget extends StatefulWidget {
  final String text;
  final ValueChanged<String> onChanged;
  final String hintText;
  const SearchWidget(
      {Key? key,
      required this.text,
      required this.onChanged,
      required this.hintText})
      : super(key: key);

  @override
  State<SearchWidget> createState() => _SearchWidgetState();
}

class _SearchWidgetState extends State<SearchWidget> {
  final controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextField(
        controller: controller,
        decoration: InputDecoration(
            icon: const Icon(
              Icons.search,
              color: Colors.grey,
            ),
            suffixIcon: widget.text.isNotEmpty
                ? GestureDetector(
                    child: const Icon(Icons.close),
                    onTap: () {
                      controller.clear();
                      widget.onChanged('');
                      FocusScope.of(context).requestFocus(FocusNode());
                    },
                  )
                : null,
            hintText: widget.hintText,
            border: InputBorder.none),
      ),
    );
  }
}

You have to call your call onChanged, your onChanged in the TextField is not defined.你必须调用你的电话 onChanged,你在 TextField 中的 onChanged 没有定义。

onChanged: widget.onChanged,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM