Skip to content

Commit 3958f5b

Browse files
simple overlay widget
1 parent 2127527 commit 3958f5b

File tree

4 files changed

+40
-52
lines changed

4 files changed

+40
-52
lines changed

example/lib/storybook/stories/help_widget_story.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import 'package:widgetbook/widgetbook.dart';
55
final _backgroundColor = Colors.grey.withOpacity(0.5);
66

77
/// Test height.
8-
const testHeight = 1000.0;
8+
const testHeight = 420.0;
99

1010
/// Test width
11-
const testWidth = 600.0;
11+
const testWidth = 420.0;
12+
13+
/// helpWidget alignment
14+
const _alignment = Alignment.bottomRight;
1215

1316
/// Definition of using 4 different widget inherited from [HelpWidget].
1417
WidgetbookComponent get helpWidgetStory => WidgetbookComponent(
@@ -20,7 +23,24 @@ WidgetbookComponent get helpWidgetStory => WidgetbookComponent(
2023
return Container(
2124
alignment: Alignment.center,
2225
color: _backgroundColor,
23-
child: HorizontalHelpWidget(),
26+
child: CardRounded(
27+
backgroundColor: Colors.black,
28+
height: testHeight,
29+
width: testWidth,
30+
child: OverlayWidget(
31+
alignment: _alignment,
32+
overlayWidget: HorizontalHelpWidget(alignment: _alignment,),
33+
child: ListView(
34+
children: [
35+
for (var i = 0; i < 100; i++)
36+
Text(
37+
i.toString(),
38+
style: const TextStyle(color: Colors.white,),
39+
),
40+
],
41+
),
42+
),
43+
),
2444
);
2545
},
2646
),

lib/src/widgets/help_collection/horizontal_help_widget.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class HorizontalHelpWidget extends StatelessWidget {
1414
/// A description for the button that will be shown on the main card.
1515
final String detailsButtonDescription;
1616

17+
/// where to align this widget inside its container
18+
/// (matters for collapsed view mostly)
19+
final Alignment alignment;
20+
1721
static const _defaultTitle = 'Stop Russian Aggression!';
1822
static const _defaultDetailsButtonDesc = 'See what you can do';
1923

@@ -22,11 +26,13 @@ class HorizontalHelpWidget extends StatelessWidget {
2226
Key? key,
2327
this.title = _defaultTitle,
2428
this.detailsButtonDescription = _defaultDetailsButtonDesc,
29+
this.alignment = Alignment.topLeft,
2530
}) : super(key: key);
2631

2732
@override
2833
Widget build(BuildContext context) {
2934
return HelpWidget(
35+
alignment: alignment,
3036
controller: _controller,
3137
optionsView: CardRounded(
3238
padding: const EdgeInsets.only(right: 3, left: 3, bottom: 12, top: 6),

lib/src/widgets/help_widget/help_widget.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class HelpWidget extends StatelessWidget {
2727
/// It's a parameter that defines the allowed size of the widget.
2828
final BoxConstraints? constraints;
2929

30+
/// where to align this widget inside its container
31+
/// (matters for collapsed view mostly)
32+
final Alignment alignment;
33+
3034
///Constructor
3135
const HelpWidget({
3236
Key? key,
@@ -35,6 +39,7 @@ class HelpWidget extends StatelessWidget {
3539
required this.mainView,
3640
required TraverseController<HelpWidgetView> controller,
3741
this.axis = Axis.vertical,
42+
this.alignment = Alignment.topLeft,
3843
this.constraints,
3944
}) : _controller = controller,
4045
super(key: key);
@@ -48,6 +53,7 @@ class HelpWidget extends StatelessWidget {
4853
};
4954

5055
return Container(
56+
alignment: alignment,
5157
constraints: constraints,
5258
child: AnimatedBuilder(
5359
animation: _controller,
Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22

33
/// [OverlayWidget] need to drop in the widget to predefined location on screen.
4-
class OverlayWidget extends StatefulWidget {
4+
class OverlayWidget extends StatelessWidget {
55
/// Child widget.
66
final Widget child;
77

@@ -19,60 +19,16 @@ class OverlayWidget extends StatefulWidget {
1919
required this.alignment,
2020
}) : super(key: key);
2121

22-
@override
23-
State<OverlayWidget> createState() => _OverlayWidgetState();
24-
}
25-
26-
class _OverlayWidgetState extends State<OverlayWidget> {
27-
bool _isVisible = true;
28-
OverlayEntry? _overlay;
29-
@override
30-
void initState() {
31-
super.initState();
32-
final overlay = OverlayEntry(builder: (context) => widget.overlayWidget);
33-
_overlay = overlay;
34-
WidgetsBinding.instance.addPostFrameCallback((_) => _showOverlay(overlay));
35-
}
36-
3722
@override
3823
Widget build(BuildContext context) {
3924
return Stack(
4025
children: [
41-
widget.child,
26+
child,
4227
Align(
43-
alignment: widget.alignment,
44-
child: IconButton(
45-
color: Colors.purple,
46-
onPressed: _onTap,
47-
icon: const Icon(Icons.chevron_right),
48-
),
49-
)
28+
alignment: alignment,
29+
child: overlayWidget,
30+
),
5031
],
5132
);
5233
}
53-
54-
void _onTap() {
55-
_isVisible = !_isVisible;
56-
_onVisibilityChanged(_isVisible);
57-
}
58-
59-
void _onVisibilityChanged(bool visible) {
60-
final overlay = _overlay;
61-
if (overlay == null) return;
62-
if (visible) {
63-
_showOverlay(overlay);
64-
} else {
65-
overlay.remove();
66-
}
67-
}
68-
69-
void _showOverlay(OverlayEntry overlay) {
70-
Overlay.of(context)?.insert(overlay);
71-
}
72-
73-
@override
74-
void dispose() {
75-
_overlay?.dispose();
76-
super.dispose();
77-
}
7834
}

0 commit comments

Comments
 (0)