|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:flet/src/utils/dismissible.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | + |
| 5 | +import '../flet_app_services.dart'; |
| 6 | +import '../models/control.dart'; |
| 7 | +import 'create_control.dart'; |
| 8 | +import 'error.dart'; |
| 9 | + |
| 10 | +class DismissibleControl extends StatelessWidget { |
| 11 | + final Control? parent; |
| 12 | + final Control control; |
| 13 | + final List<Control> children; |
| 14 | + final bool parentDisabled; |
| 15 | + |
| 16 | + const DismissibleControl( |
| 17 | + {Key? key, |
| 18 | + this.parent, |
| 19 | + required this.control, |
| 20 | + required this.children, |
| 21 | + required this.parentDisabled}) |
| 22 | + : super(key: key); |
| 23 | + |
| 24 | + @override |
| 25 | + Widget build(BuildContext context) { |
| 26 | + var server = FletAppServices.of(context).server; |
| 27 | + debugPrint("Dismissible build: ${control.id}"); |
| 28 | + |
| 29 | + bool disabled = control.isDisabled || parentDisabled; |
| 30 | + var contentCtrls = children.where((c) => c.name == "content"); |
| 31 | + |
| 32 | + if (contentCtrls.isEmpty) { |
| 33 | + return const ErrorControl("Dismissible does not have a content."); |
| 34 | + } |
| 35 | + |
| 36 | + var backgroundCtrls = children.where((c) => c.name == "background"); |
| 37 | + |
| 38 | + var secondaryBackgroundCtrls = |
| 39 | + children.where((c) => c.name == "secondaryBackground"); |
| 40 | + |
| 41 | + SnackBarBehavior? behavior = SnackBarBehavior.values.firstWhereOrNull((a) => |
| 42 | + a.name.toLowerCase() == |
| 43 | + control.attrString("behavior", "")!.toLowerCase()); |
| 44 | + |
| 45 | + var dismissThresholds = |
| 46 | + parseDismissThresholds(control, "dismissThresholds"); |
| 47 | + |
| 48 | + DismissDirection? direction = DismissDirection.values.firstWhere( |
| 49 | + (a) => |
| 50 | + a.name.toLowerCase() == |
| 51 | + control.attrString("dismissDirection", "")!.toLowerCase(), |
| 52 | + orElse: () => DismissDirection.horizontal); |
| 53 | + |
| 54 | + return constrainedControl( |
| 55 | + context, |
| 56 | + Dismissible( |
| 57 | + key: ValueKey<String>(control.id), |
| 58 | + direction: direction, |
| 59 | + background: backgroundCtrls.isNotEmpty |
| 60 | + ? createControl(control, backgroundCtrls.first.id, disabled) |
| 61 | + : Container(color: Colors.transparent), |
| 62 | + secondaryBackground: secondaryBackgroundCtrls.isNotEmpty |
| 63 | + ? createControl( |
| 64 | + control, secondaryBackgroundCtrls.first.id, disabled) |
| 65 | + : Container(color: Colors.transparent), |
| 66 | + onDismissed: (DismissDirection d) { |
| 67 | + server.sendPageEvent( |
| 68 | + eventTarget: control.id, eventName: "dismiss", eventData: ""); |
| 69 | + }, |
| 70 | + onResize: () { |
| 71 | + server.sendPageEvent( |
| 72 | + eventTarget: control.id, eventName: "resize", eventData: ""); |
| 73 | + }, |
| 74 | + onUpdate: (DismissUpdateDetails d) { |
| 75 | + server.sendPageEvent( |
| 76 | + eventTarget: control.id, eventName: "update", eventData: ""); |
| 77 | + }, |
| 78 | + // confirmDismiss: // TODO: implement |
| 79 | + movementDuration: |
| 80 | + Duration(milliseconds: control.attrInt("duration", 200)!), |
| 81 | + resizeDuration: |
| 82 | + Duration(milliseconds: control.attrInt("resizeDuration", 300)!), |
| 83 | + crossAxisEndOffset: control.attrDouble("crossAxisEndOffset", 0.0)!, |
| 84 | + dismissThresholds: dismissThresholds ?? {}, |
| 85 | + child: createControl(control, contentCtrls.first.id, disabled)), |
| 86 | + parent, |
| 87 | + control); |
| 88 | + } |
| 89 | +} |
0 commit comments