Last active
April 9, 2022 06:04
-
-
Save rodydavis/956f572b3bedf6ac47ff412efe26a1fd to your computer and use it in GitHub Desktop.
Flutter Master Detail View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class MasterDetail<T> extends StatefulWidget { | |
const MasterDetail({ | |
Key? key, | |
required this.listBuilder, | |
required this.detailBuilder, | |
required this.onPush, | |
this.emptyBuilder, | |
}) : super(key: key); | |
final Widget Function(BuildContext, ValueChanged<T?>, T?) listBuilder; | |
final Widget Function(BuildContext, T, bool) detailBuilder; | |
final void Function(BuildContext, T) onPush; | |
final WidgetBuilder? emptyBuilder; | |
@override | |
State<MasterDetail<T>> createState() => _MasterDetailState<T>(); | |
} | |
class _MasterDetailState<T> extends State<MasterDetail<T>> { | |
final selected = ValueNotifier<T?>(null); | |
double? detailsWidth; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
primary: false, | |
body: LayoutBuilder( | |
builder: (context, dimens) { | |
const double minWidth = 350; | |
final maxWidth = dimens.maxWidth - minWidth; | |
if (detailsWidth != null) { | |
if (detailsWidth! > maxWidth) { | |
detailsWidth = maxWidth; | |
} | |
if (detailsWidth! < minWidth) { | |
detailsWidth = minWidth; | |
} | |
} | |
return ValueListenableBuilder<T?>( | |
valueListenable: selected, | |
builder: (context, item, child) { | |
final canShowDetails = dimens.maxWidth > 800; | |
final showDetails = item != null && canShowDetails; | |
return Row( | |
children: [ | |
Expanded( | |
child: widget.listBuilder(context, (item) { | |
if (canShowDetails) { | |
selected.value = item; | |
} else { | |
selected.value = null; | |
if (item != null) widget.onPush(context, item); | |
} | |
}, selected.value), | |
), | |
if (canShowDetails) | |
MouseRegion( | |
cursor: SystemMouseCursors.resizeLeftRight, | |
child: GestureDetector( | |
behavior: HitTestBehavior.opaque, | |
onHorizontalDragUpdate: (details) { | |
if (mounted) { | |
setState(() { | |
double w = detailsWidth ?? maxWidth; | |
w -= details.delta.dx; | |
// Check for min width | |
if (w < minWidth) { | |
w = minWidth; | |
} | |
// Check for max width | |
if (w > maxWidth) { | |
w = maxWidth; | |
} | |
detailsWidth = w; | |
}); | |
} | |
}, | |
child: const SizedBox( | |
width: 5, | |
height: double.infinity, | |
child: VerticalDivider(), | |
), | |
), | |
), | |
if (canShowDetails) | |
SizedBox( | |
width: detailsWidth ?? maxWidth, | |
height: double.infinity, | |
child: showDetails | |
? widget.detailBuilder(context, item, false) | |
: widget.emptyBuilder?.call(context) ?? | |
const Center( | |
child: Text('Select a item to view details'), | |
), | |
), | |
], | |
); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment