dart Flutter ChipThemeData的labelStyle与MaterialStateTextStyle不兼容

yuvru6vn  于 2023-11-14  发布在  Flutter
关注(0)|答案(1)|浏览(148)

我有这个主题我提供给我的MaterialApp的数据

  1. ThemeData get themeData => ThemeData(
  2. ...
  3. chipTheme: _chipTheme(),
  4. );

字符串
其中_chipTheme只是一个自定义的ChipThemeData,它具有labelStyle属性:

  1. labelStyle: MaterialStateTextStyle.resolveWith(
  2. (states) {
  3. TextStyle? textStyle;
  4. if (states.contains(MaterialState.disabled)) {
  5. textStyle = ...
  6. }
  7. else {
  8. ....
  9. }
  10. return textStyle ?? const TextStyle();
  11. },
  12. ),


问题是labelStyle没有被应用,在resolveWith方法中设置断点/打印语句,我可以看到它没有被调用,我做错了什么,我如何根据芯片的状态设置labelStyle(选择/禁用/等...)

编辑1

我知道我通常可以传递TextStyle而不是MaterialStateTextStyle,但这不会解决问题,因为我需要根据芯片的MaterialState更改TextStyle

oymdgrw7

oymdgrw71#

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. theme: ThemeData.light().copyWith(
  10. useMaterial3: true,
  11. chipTheme: ChipThemeData(
  12. labelStyle: TextStyle(
  13. color: MaterialStateColor.resolveWith((states) {
  14. Color? color;
  15. if (states.contains(MaterialState.disabled)) {
  16. color = Colors.red;
  17. } else {
  18. color = Colors.green;
  19. }
  20. return color;
  21. }),
  22. ),),
  23. ),
  24. debugShowCheckedModeBanner: false,
  25. home: Scaffold(
  26. body: Center(
  27. child: MyWidget(),
  28. ),
  29. ),
  30. );
  31. }
  32. }
  33. class MyWidget extends StatelessWidget {
  34. @override
  35. Widget build(BuildContext context) {
  36. return ActionChip(label: Text("Test"), onPressed: (){});
  37. }
  38. }

字符串
如果您将onPressed从Windows Chip中删除,它应该跳转到禁用状态。
如文档中所述,您可以处理以下状态:
如果TextStyle.color是MaterialStateProperty,则MaterialStateProperty.resolve用于以下MaterialStates:
MaterialState.disabled.MaterialState.selected.MaterialState.hovered.MaterialState.focused.MaterialState.pressed.

展开查看全部

相关问题