dynamic_drawer.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:flutter/material.dart';
  2. import 'package:vitalapp/consts/styles.dart';
  3. final _builderMap = <GlobalKey<ScaffoldState>, WidgetBuilder>{};
  4. class VDrawer extends StatelessWidget {
  5. final Widget? child;
  6. final double? width;
  7. final String? title;
  8. final GlobalKey<ScaffoldState>? scaffoldKey;
  9. final VoidCallback? onConfirm;
  10. final VoidCallback? onCancel;
  11. const VDrawer({
  12. super.key,
  13. this.child,
  14. this.width,
  15. this.title,
  16. this.scaffoldKey,
  17. this.onConfirm,
  18. this.onCancel,
  19. });
  20. @override
  21. Widget build(BuildContext context) {
  22. return Drawer(
  23. shadowColor: Colors.black.withOpacity(.1),
  24. backgroundColor: Colors.white,
  25. shape: RoundedRectangleBorder(
  26. borderRadius: GlobalStyles.borderRadius,
  27. ),
  28. elevation: 0,
  29. width: width,
  30. child: Container(
  31. alignment: Alignment.topLeft,
  32. padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
  33. child: Column(
  34. mainAxisSize: MainAxisSize.min,
  35. children: _buildChildren(context),
  36. ),
  37. ),
  38. );
  39. }
  40. List<Widget> _buildChildren(BuildContext context) {
  41. final children = <Widget>[];
  42. final header = _buildHeader(context);
  43. if (header != null) {
  44. children.add(header);
  45. }
  46. if (child != null) {
  47. children.add(Expanded(child: child!));
  48. }
  49. return children;
  50. }
  51. Widget? _buildHeader(BuildContext context) {
  52. final children = <Widget>[];
  53. final btnTextStyle = TextStyle(
  54. fontSize: 20,
  55. color: Theme.of(context).primaryColor,
  56. );
  57. if (scaffoldKey != null) {
  58. children.add(
  59. TextButton(
  60. onPressed: () {
  61. scaffoldKey!.currentState?.closeEndDrawer();
  62. onCancel?.call();
  63. },
  64. child: Text("取消", style: btnTextStyle),
  65. ),
  66. );
  67. }
  68. if (title != null) {
  69. children.add(Text(
  70. title!,
  71. style: TextStyle(color: Colors.grey.shade700, fontSize: 20),
  72. ));
  73. }
  74. if (onConfirm != null) {
  75. children.add(
  76. TextButton(
  77. onPressed: () {
  78. onConfirm?.call();
  79. },
  80. child: Text("确定", style: btnTextStyle),
  81. ),
  82. );
  83. }
  84. if (children.isEmpty) return null;
  85. return Row(
  86. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  87. crossAxisAlignment: CrossAxisAlignment.center,
  88. children: children,
  89. );
  90. }
  91. }
  92. class VDynamicDrawerWrapper extends StatefulWidget {
  93. final GlobalKey<ScaffoldState> scaffoldKey;
  94. const VDynamicDrawerWrapper({super.key, required this.scaffoldKey});
  95. @override
  96. State<StatefulWidget> createState() => _VDynamicDrawerWrapperState();
  97. static void show({
  98. required GlobalKey<ScaffoldState> scaffoldKey,
  99. required WidgetBuilder builder,
  100. }) {
  101. _builderMap[scaffoldKey] = builder;
  102. scaffoldKey.currentState?.openEndDrawer();
  103. }
  104. static void hide({required GlobalKey<ScaffoldState> scaffoldKey}) {
  105. scaffoldKey.currentState?.closeEndDrawer();
  106. }
  107. }
  108. class _VDynamicDrawerWrapperState extends State<VDynamicDrawerWrapper> {
  109. @override
  110. Widget build(BuildContext context) {
  111. final builder = _builderMap[widget.scaffoldKey];
  112. if (builder != null) {
  113. return builder(context);
  114. }
  115. return const SizedBox();
  116. }
  117. }