conditional_parent_widget.dart:
import 'package:flutter/material.dart';
class ConditionalParentWidget extends StatelessWidget {
final bool condition;
final Widget Function({Widget? child}) leftParent;
final Widget Function({Widget? child}) rightParent;
final Widget child;
const ConditionalParentWidget({
Key? key,
required this.condition,
required this.leftParent,
required this.rightParent,
required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return condition ? rightParent(child: child) : leftParent(child: child);
}
}
Usage:
ConditionalParentWidget(
condition: _conditionalVariable,
rightParent: ({child}) => MouseRegion(
onEnter: (event) {},
onExit: (event) {},
child: child ?? const SizedBox.shrink(),
),
leftParent: ({child}) => Dismissible(
key: Key('key'),
onDismissed: (direction) {},
background: Container(
color: Colors.redAccent,
alignment: Alignment.centerRight,
padding: const EdgeInsets.all(8.0),
child: Text('Remove'),
),
child: child ?? const SizedBox.shrink(),
),
child: Container(color: Colors.blue),
)