Skip to content

Commit

Permalink
Simplified the building of RichAttributionWidget (#1661)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramp authored Sep 25, 2023
1 parent d35d672 commit eb3b790
Showing 1 changed file with 59 additions and 88 deletions.
147 changes: 59 additions & 88 deletions lib/src/layer/attribution_layer/rich.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ class RichAttributionWidget extends StatefulWidget {
class RichAttributionWidgetState extends State<RichAttributionWidget> {
StreamSubscription<MapEvent>? mapEventSubscription;

final persistentAttributionKey = GlobalKey();
Size? persistentAttributionSize;

late bool popupExpanded = widget.popupInitialDisplayDuration != Duration.zero;
bool persistentHovered = false;

Expand All @@ -173,22 +170,6 @@ class RichAttributionWidgetState extends State<RichAttributionWidget> {
},
);
}

WidgetsBinding.instance.addPostFrameCallback(
(_) => WidgetsBinding.instance.addPostFrameCallback(
(_) {
assert(
persistentAttributionKey.currentContext?.findRenderObject() != null,
'persistentAttributionKey is not in the widget tree',
);
final renderObject =
persistentAttributionKey.currentContext?.findRenderObject();
if (renderObject is RenderBox) {
setState(() => persistentAttributionSize = renderObject.size);
}
},
),
);
}

@override
Expand Down Expand Up @@ -255,88 +236,78 @@ class RichAttributionWidgetState extends State<RichAttributionWidget> {
),
];

return LayoutBuilder(
builder: (context, constraints) => Align(
return Align(
alignment: widget.alignment.real,
child: Stack(
alignment: widget.alignment.real,
child: Stack(
alignment: widget.alignment.real,
children: [
if (persistentAttributionSize != null)
Padding(
padding: const EdgeInsets.all(6),
child: widget.animationConfig.popupAnimationBuilder(
context: context,
isExpanded: popupExpanded,
config: widget,
child: Container(
decoration: BoxDecoration(
color: widget.popupBackgroundColor ??
Theme.of(context).colorScheme.background,
border: Border.all(width: 0, style: BorderStyle.none),
borderRadius: widget.popupBorderRadius ??
BorderRadius.only(
topLeft: const Radius.circular(10),
topRight: const Radius.circular(10),
bottomLeft: widget.alignment ==
AttributionAlignment.bottomLeft
children: [
Padding(
padding: const EdgeInsets.all(6),
child: widget.animationConfig.popupAnimationBuilder(
context: context,
isExpanded: popupExpanded,
config: widget,
child: Container(
decoration: BoxDecoration(
color: widget.popupBackgroundColor ??
Theme.of(context).colorScheme.background,
border: Border.all(width: 0, style: BorderStyle.none),
borderRadius: widget.popupBorderRadius ??
BorderRadius.only(
topLeft: const Radius.circular(10),
topRight: const Radius.circular(10),
bottomLeft:
widget.alignment == AttributionAlignment.bottomLeft
? Radius.zero
: const Radius.circular(10),
bottomRight: widget.alignment ==
AttributionAlignment.bottomRight
bottomRight:
widget.alignment == AttributionAlignment.bottomRight
? Radius.zero
: const Radius.circular(10),
),
),
constraints: BoxConstraints(
minWidth: constraints.maxWidth < 420
? constraints.maxWidth
: persistentAttributionSize!.width,
),
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
...widget.attributions
.whereType<TextSourceAttribution>(),
const TextSourceAttribution(
"Made with 'flutter_map'",
prependCopyright: false,
textStyle: TextStyle(fontStyle: FontStyle.italic),
),
SizedBox(height: (widget.permanentHeight - 24) + 32),
],
),
),
),
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
...widget.attributions.whereType<TextSourceAttribution>(),
const TextSourceAttribution(
"Made with 'flutter_map'",
prependCopyright: false,
textStyle: TextStyle(fontStyle: FontStyle.italic),
),
SizedBox(height: (widget.permanentHeight - 24) + 32),
],
),
),
),
MouseRegion(
key: persistentAttributionKey,
onEnter: (_) => setState(() => persistentHovered = true),
onExit: (_) => setState(() => persistentHovered = false),
cursor: SystemMouseCursors.click,
child: AnimatedOpacity(
opacity: persistentHovered || popupExpanded ? 1 : 0.5,
curve: widget.animationConfig.buttonCurve,
duration: widget.animationConfig.buttonDuration,
child: Padding(
padding: const EdgeInsets.all(4),
child: FittedBox(
child: Row(
mainAxisSize: MainAxisSize.min,
children:
widget.alignment == AttributionAlignment.bottomLeft
? persistentAttributionItems.reversed.toList()
: persistentAttributionItems,
),
),
),
MouseRegion(
onEnter: (_) => setState(() => persistentHovered = true),
onExit: (_) => setState(() => persistentHovered = false),
cursor: SystemMouseCursors.click,
child: AnimatedOpacity(
opacity: persistentHovered || popupExpanded ? 1 : 0.5,
curve: widget.animationConfig.buttonCurve,
duration: widget.animationConfig.buttonDuration,
child: Padding(
padding: const EdgeInsets.all(4),
child: FittedBox(
child: Row(
mainAxisSize: MainAxisSize.min,
children:
widget.alignment == AttributionAlignment.bottomLeft
? persistentAttributionItems.reversed.toList()
: persistentAttributionItems,
),
),
),
),
],
),
),
],
),
);
}
Expand Down

0 comments on commit eb3b790

Please sign in to comment.