参数类型“void Function(bool)”无法分配给参数类型“PopInvokedWithResultCallback<dynamic>?”

问题描述 投票:0回答:1

更新 Dart 和 Flutter(3.5.2 和 3.24.2)后,PopScope 停止工作并给出错误。另外,结果不打印字符串。我查看了文档,但不明白如何将应用程序调整为新格式。

代码本身应该保存用户在特定屏幕上选择的过滤器,并在切换到其他屏幕时保存此信息。

过滤器

import 'package:flutter/material.dart';
import 'package:food_catalog/screens/tabs.dart';
import 'package:food_catalog/widgets/main_drawer.dart';

enum Filter {
  glutenFree,
  lactoseFree,
  vegetarian,
  vegan,
}

class FiltersScreen extends StatefulWidget {
  const FiltersScreen({
    super.key,
    required this.currentFilters,
  });

  final Map<Filter, bool> currentFilters;

  @override
  State<FiltersScreen> createState() => _FiltersScreenState();
}

class _FiltersScreenState extends State<FiltersScreen> {
  var _glutenFreeFilterSet = false;
  var _lactoseFreeFilterSet = false;
  var _vegetarianFilterSet = false;
  var _veganFilterSet = false;

  @override
  void initState() {
    super.initState();
    _glutenFreeFilterSet = widget.currentFilters[Filter.glutenFree]!;
    _lactoseFreeFilterSet = widget.currentFilters[Filter.lactoseFree]!;
    _vegetarianFilterSet = widget.currentFilters[Filter.vegetarian]!;
    _veganFilterSet = widget.currentFilters[Filter.vegan]!;
  }

  void _saveFilters() {
    Navigator.of(context).pop({
      Filter.glutenFree: _glutenFreeFilterSet,
      Filter.lactoseFree: _lactoseFreeFilterSet,
      Filter.vegetarian: _vegetarianFilterSet,
      Filter.vegan: _veganFilterSet,
    });
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      onPopInvokedWithResult: (didPop) {
        if (didPop) {
          _saveFilters();
        }
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Ваши фильтры'),
        ),
        drawer: MainDrawer(onSelectScreen: (identifier) {
          Navigator.of(context).pop();
          if (identifier == 'meals') {
            Navigator.of(context).pushReplacement(
              MaterialPageRoute(
                builder: (context) => const TabsScreen(),
              ),
            );
          }
        }),
        body: Column(
          children: [
            SwitchListTile(
              value: _glutenFreeFilterSet,
              onChanged: (newValue) {
                setState(() {
                  _glutenFreeFilterSet = newValue;
                });
              },
              title: Text(
                'Не содержит глютена',
                style: Theme.of(context).textTheme.titleLarge!.copyWith(
                      color: Colors.black,
                    ),
              ),
              subtitle: Text(
                'Показать только блюда без глютена',
                style: Theme.of(context).textTheme.labelMedium!.copyWith(
                      color: Colors.black54,
                    ),
              ),
              activeColor: Theme.of(context).colorScheme.tertiary,
              contentPadding: const EdgeInsets.only(left: 34, right: 22),
            ),
            SwitchListTile(
              value: _lactoseFreeFilterSet,
              onChanged: (newValue) {
                setState(() {
                  _lactoseFreeFilterSet = newValue;
                });
              },
              title: Text(
                'Не содержит лактозу',
                style: Theme.of(context).textTheme.titleLarge!.copyWith(
                      color: Colors.black,
                    ),
              ),
              subtitle: Text(
                'Показать только блюда без лактозы',
                style: Theme.of(context).textTheme.labelMedium!.copyWith(
                      color: Colors.black54,
                    ),
              ),
              activeColor: Theme.of(context).colorScheme.tertiary,
              contentPadding: const EdgeInsets.only(left: 34, right: 22),
            ),
            SwitchListTile(
              value: _vegetarianFilterSet,
              onChanged: (newValue) {
                setState(() {
                  _vegetarianFilterSet = newValue;
                });
              },
              title: Text(
                'Только вегетарианские блюда',
                style: Theme.of(context).textTheme.titleLarge!.copyWith(
                      color: Colors.black,
                    ),
              ),
              subtitle: Text(
                'Показать только вегетарианские блюда',
                style: Theme.of(context).textTheme.labelMedium!.copyWith(
                      color: Colors.black54,
                    ),
              ),
              activeColor: Theme.of(context).colorScheme.tertiary,
              contentPadding: const EdgeInsets.only(left: 34, right: 22),
            ),
            SwitchListTile(
              value: _veganFilterSet,
              onChanged: (newValue) {
                setState(() {
                  _veganFilterSet = newValue;
                });
              },
              title: Text(
                'Только веганские блюда',
                style: Theme.of(context).textTheme.titleLarge!.copyWith(
                      color: Colors.black,
                    ),
              ),
              subtitle: Text(
                'Показать только веганские блюда',
                style: Theme.of(context).textTheme.labelMedium!.copyWith(
                      color: Colors.black54,
                    ),
              ),
              activeColor: Theme.of(context).colorScheme.tertiary,
              contentPadding: const EdgeInsets.only(left: 34, right: 22),
            ),
          ],
        ),
      ),
    );
  }
}

标签------------------------------------------------ -------------------------------------------------- ---

import 'package:flutter/material.dart';
import 'package:food_catalog/data/dummy_data.dart';
import 'package:food_catalog/models/meal.dart';
import 'package:food_catalog/screens/category_view.dart';
import 'package:food_catalog/screens/filters.dart';
import 'package:food_catalog/screens/meals.dart';
import 'package:food_catalog/widgets/main_drawer.dart';

const kInitialFilters = {
  Filter.glutenFree: false,
  Filter.lactoseFree: false,
  Filter.vegetarian: false,
  Filter.vegan: false,
};

class TabsScreen extends StatefulWidget {
  const TabsScreen({super.key});

  @override
  State<TabsScreen> createState() => _TabsScreenState();
}

class _TabsScreenState extends State<TabsScreen> {
  int _selectedPageIndex = 0;
  final List<Meal> _favoriteMeals = [];
  Map<Filter, bool> _selectedFilters = kInitialFilters;

  void _showInfoMessage(String message) {
    ScaffoldMessenger.of(context).clearSnackBars();
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
      ),
    );
  }

  void _toggleMealFavoriteStatus(Meal meal) {
    final isExisting = _favoriteMeals.contains(meal);

    if (isExisting) {
      setState(() {
        _favoriteMeals.remove(meal);
      });
      _showInfoMessage('Чао персик, дозревай');
    } else {
      setState(() {
        _favoriteMeals.add(meal);

        _showInfoMessage('Опа чиназес, сюда!');
      });
    }
  }

  void _selectedPage(int index) {
    setState(() {
      _selectedPageIndex = index;
    });
  }

  void _setScreen(String identifier) async {
    Navigator.of(context).pop();
    if (identifier == "Фильтры") {
      final result = await Navigator.of(context).push<Map<Filter, bool>>(
        MaterialPageRoute(
          builder: (context) => FiltersScreen(
            currentFilters: _selectedFilters,
          ),
        ),
      );
      setState(() {
        _selectedFilters = result ?? kInitialFilters;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final availableMeals = dummyMeals.where((meal) {
      if (_selectedFilters[Filter.glutenFree]! && !meal.isGlutenFree) {
        return false;
      }
      if (_selectedFilters[Filter.lactoseFree]! && !meal.isLactoseFree) {
        return false;
      }
      if (_selectedFilters[Filter.vegetarian]! && !meal.isVegetarian) {
        return false;
      }
      if (_selectedFilters[Filter.vegan]! && !meal.isVegan) {
        return false;
      }
      return true;
    }).toList();
    Widget activePage = CategoriesScreen(
      onToggleFavorite: _toggleMealFavoriteStatus,
      availebleMeals: availableMeals,
    );
    var activePageTittle = 'Категории';

    if (_selectedPageIndex == 1) {
      activePage = MealsScreen(
        meals: _favoriteMeals,
        onToggleFavorite: _toggleMealFavoriteStatus,
      );
      activePageTittle = 'Избранное';
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(activePageTittle),
      ),
      drawer: MainDrawer(
        onSelectScreen: _setScreen,
      ),
      body: activePage,
      bottomNavigationBar: BottomNavigationBar(
        onTap: _selectedPage,
        currentIndex: _selectedPageIndex,
        items: const [
          BottomNavigationBarItem(
              icon: Icon(Icons.set_meal), label: "Категории"),
          BottomNavigationBarItem(icon: Icon(Icons.stars), label: "Избранное"),
        ],
      ),
    );
  }
}

我尝试联系ChatGPT和Gemini,但他们无法解决问题,尽管我以不同的方式询问他们,例如问题出在哪里或在代码中发现拼写错误

android flutter dart
1个回答
0
投票

PopInvokedWithResultCallback<dynamic>?
需要一个回调函数,该函数接受
didPop
的参数,无论是否弹出,另一个接受返回的
result

尝试以下操作:

 PopScope(
  onPopInvokedWithResult: (didPop, result) {
    if (didPop) {
      _saveFilters();
    }
    // Return the result instead of false
    return result;
  },
© www.soinside.com 2019 - 2024. All rights reserved.