如何使用 Riverpod Notifier 创建下拉按钮?

how can i create drowdownbutton with Riverpod Notifier?

我想在 Riverpod 中使用 changenotifierProvider 创建 DropDownButton,但我无法很好地编写代码。请帮我。我想,我只是写了 'watch' 方法,但我不知道如何阅读它。所以,它应该显示选择的项目,并且它应该更新为 provider.Category.

我的 DropDownButton 代码在这里:

Widget dropdownButton(BuildContext context,watch) {
    String constantValue = "League Of Legends";
      final postProvider = ChangeNotifierProvider<PostProvider>((ref) => PostProvider());
      
      final provider = watch(postProvider);
      return Consumer(
        builder: (context, watch, _) {
       
          
      return DropdownButton(
            value: provider.postCategory ?? constantValue,
            
            onChanged: (newValue) {
               provider.postCategory = newValue;
              
            },
            items: <String>["League Of Legends", "Steam", "Csgo"]
                .map<DropdownMenuItem<String>>((String value) {
              return DropdownMenuItem<String>(
                onTap: () => value,
                value: value ?? constantValue,
                child: Text(value ?? constantValue),
              );
            }).toList());
        },
              
      );
    
  }

这是我的 DropDownButton 图片:(当我选择列表中的任何项目时,它无法正常工作。它总是选择第一个(它选择“英雄联盟”)。

我select steam 但卡牌类别显示英雄联盟

final postProvider =
    ChangeNotifierProvider<PostProvider>((ref) => PostProvider());

class PostProvider extends ChangeNotifier {
  String _postCategory;

  String get postCategory => _postCategory;

  categoryOnChanged(String value) {
    if (value.isEmpty) {
      return _postCategory;
    }
    _postCategory = value;
    print(_postCategory);
    return notifyListeners();
  }
}

class DropDownPage extends StatefulWidget {
  @override
  _DropDownPageState createState() => _DropDownPageState();
}

class _DropDownPageState extends State<DropDownPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dropdown Riverpod issue"),
      ),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Center(
          child: _DropDownWidgetConsumer(),
        ),
      ),
    );
  }
}

class _DropDownWidgetConsumer extends ConsumerWidget {
  @override
  Widget build(BuildContext context, ScopedReader watch) {
    final category = watch(postProvider).postCategory;
    return DropdownButton(
      hint: Text("Choose category"),
      value: category,
      items: <String>["League Of Legends", "Steam", "Csgo"]
          .map((e) => DropdownMenuItem<String>(
        
                onTap: () => e,
                value: e ?? category,
                child: Text(e ?? "$category"),
              ))
          .toList(),
      onChanged: (value) {
        context.read(postProvider).categoryOnChanged(value);
      },
    );
  }
}