forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrop_down_button.dart
More file actions
85 lines (72 loc) · 2.44 KB
/
drop_down_button.dart
File metadata and controls
85 lines (72 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2023 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
import 'package:collection/collection.dart';
import 'package:devtools_app_shared/ui.dart';
import 'package:flutter/material.dart';
import '../analytics/analytics.dart' as ga;
/// A DropDownButton implementation that reports selection changes to our
/// analytics.
class AnalyticsDropDownButton<T> extends StatelessWidget {
const AnalyticsDropDownButton({
super.key,
required this.gaScreen,
required this.gaDropDownId,
required this.message,
required this.value,
required this.items,
required this.onChanged,
this.sendAnalytics = true,
this.isDense = false,
this.isExpanded = false,
this.roundedCornerOptions,
});
/// The GA ID for the screen this widget is displayed on.
final String gaScreen;
/// The GA ID for this widget.
final String gaDropDownId;
/// Whether to send analytics events to GA.
///
/// Only set this to false if [AnalyticsDropDownButton] is being used for
/// experimental code we do not want to send GA events for yet.
final bool sendAnalytics;
/// The message to be displayed in the widget's tooltip.
final String? message;
/// The currently selected value.
final T? value;
/// The list of options available in the drop down with their associated GA
/// IDs.
final List<({DropdownMenuItem<T> item, String gaId})>? items;
/// Invoked when the selected drop down item has changed.
final void Function(T?)? onChanged;
final bool isDense;
final bool isExpanded;
final RoundedCornerOptions? roundedCornerOptions;
@override
Widget build(BuildContext context) {
return SizedBox(
height: defaultButtonHeight,
child: DevToolsTooltip(
message: message,
child: RoundedDropDownButton<T>(
isDense: isDense,
isExpanded: isExpanded,
value: value,
items: items?.map((e) => e.item).toList(),
onChanged: _onChanged,
roundedCornerOptions: roundedCornerOptions,
),
),
);
}
void _onChanged(T? newValue) {
if (sendAnalytics && items != null) {
final gaId =
items?.firstWhereOrNull((element) => element.item == newValue)?.gaId;
if (gaId != null) {
ga.select(gaScreen, '$gaDropDownId $gaId');
}
}
onChanged?.call(newValue);
}
}