123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import 'package:flutter/material.dart';
- import 'package:vitalapp/consts/styles.dart';
- final _builderMap = <GlobalKey<ScaffoldState>, WidgetBuilder>{};
- class VDrawer extends StatelessWidget {
- final Widget? child;
- final double? width;
- final String? title;
- final GlobalKey<ScaffoldState>? scaffoldKey;
- final VoidCallback? onConfirm;
- final VoidCallback? onCancel;
- const VDrawer({
- super.key,
- this.child,
- this.width,
- this.title,
- this.scaffoldKey,
- this.onConfirm,
- this.onCancel,
- });
- @override
- Widget build(BuildContext context) {
- return Drawer(
- shadowColor: Colors.black.withOpacity(.1),
- backgroundColor: Colors.white,
- shape: RoundedRectangleBorder(
- borderRadius: GlobalStyles.borderRadius,
- ),
- elevation: 0,
- width: width,
- child: Container(
- alignment: Alignment.topLeft,
- padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: _buildChildren(context),
- ),
- ),
- );
- }
- List<Widget> _buildChildren(BuildContext context) {
- final children = <Widget>[];
- final header = _buildHeader(context);
- if (header != null) {
- children.add(header);
- }
- if (child != null) {
- children.add(Expanded(child: child!));
- }
- return children;
- }
- Widget? _buildHeader(BuildContext context) {
- final children = <Widget>[];
- final btnTextStyle = TextStyle(
- fontSize: 20,
- color: Theme.of(context).primaryColor,
- );
- if (scaffoldKey != null) {
- children.add(
- TextButton(
- onPressed: () {
- scaffoldKey!.currentState?.closeEndDrawer();
- onCancel?.call();
- },
- child: Text("取消", style: btnTextStyle),
- ),
- );
- }
- if (title != null) {
- children.add(Text(
- title!,
- style: TextStyle(color: Colors.grey.shade700, fontSize: 20),
- ));
- }
- if (onConfirm != null) {
- children.add(
- TextButton(
- onPressed: () {
- onConfirm?.call();
- },
- child: Text("确定", style: btnTextStyle),
- ),
- );
- }
- if (children.isEmpty) return null;
- return Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: children,
- );
- }
- }
- class VDynamicDrawerWrapper extends StatefulWidget {
- final GlobalKey<ScaffoldState> scaffoldKey;
- const VDynamicDrawerWrapper({super.key, required this.scaffoldKey});
- @override
- State<StatefulWidget> createState() => _VDynamicDrawerWrapperState();
- static void show({
- required GlobalKey<ScaffoldState> scaffoldKey,
- required WidgetBuilder builder,
- }) {
- _builderMap[scaffoldKey] = builder;
- scaffoldKey.currentState?.openEndDrawer();
- }
- static void hide({required GlobalKey<ScaffoldState> scaffoldKey}) {
- scaffoldKey.currentState?.closeEndDrawer();
- }
- }
- class _VDynamicDrawerWrapperState extends State<VDynamicDrawerWrapper> {
- @override
- Widget build(BuildContext context) {
- final builder = _builderMap[widget.scaffoldKey];
- if (builder != null) {
- return builder(context);
- }
- return const SizedBox();
- }
- }
|