forked from flutter/devtools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon_widgets.dart
More file actions
2347 lines (2047 loc) · 61.5 KB
/
common_widgets.dart
File metadata and controls
2347 lines (2047 loc) · 61.5 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 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 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:devtools_app_shared/ui.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vm_service/vm_service.dart';
import '../analytics/analytics.dart' as ga;
import '../analytics/constants.dart' as gac;
import '../config_specific/copy_to_clipboard/copy_to_clipboard.dart';
import '../console/widgets/expandable_variable.dart';
import '../diagnostics/dart_object_node.dart';
import '../diagnostics/tree_builder.dart';
import '../framework/routing.dart';
import '../globals.dart';
import '../primitives/flutter_widgets/linked_scroll_controller.dart';
import '../primitives/utils.dart';
import '../utils/utils.dart';
double get assumedMonospaceCharacterWidth =>
scaleByFontFactor(_assumedMonospaceCharacterWidth);
double _assumedMonospaceCharacterWidth = 9.0;
@visibleForTesting
void setAssumedMonospaceCharacterWidth(double width) {
_assumedMonospaceCharacterWidth = width;
}
/// Creates a semibold version of [style].
TextStyle semibold(TextStyle style) =>
style.copyWith(fontWeight: FontWeight.w600);
/// Creates a version of [style] that uses the primary color of [context].
///
/// When the app is in dark mode, it instead uses the accent color.
TextStyle primaryColor(TextStyle style, BuildContext context) {
final theme = Theme.of(context);
return style.copyWith(
color: (theme.brightness == Brightness.light)
? theme.primaryColor
: theme.colorScheme.secondary,
fontWeight: FontWeight.w400,
);
}
/// Creates a version of [style] that uses the lighter primary color of
/// [context].
///
/// In dark mode, the light primary color still has enough contrast to be
/// visible, so we continue to use it.
TextStyle primaryColorLight(TextStyle style, BuildContext context) {
final theme = Theme.of(context);
return style.copyWith(
color: theme.primaryColorLight,
fontWeight: FontWeight.w300,
);
}
class GaDevToolsButton extends DevToolsButton {
GaDevToolsButton({
super.key,
required VoidCallback? onPressed,
required String gaScreen,
required String gaSelection,
super.icon,
super.label,
super.tooltip,
super.color,
super.minScreenWidthForTextBeforeScaling,
super.elevated,
super.outlined,
super.tooltipPadding,
}) : super(
onPressed: onPressed != null
? () {
ga.select(gaScreen, gaSelection);
onPressed();
}
: null,
);
factory GaDevToolsButton.iconOnly({
required IconData icon,
required String gaScreen,
required String gaSelection,
String? tooltip,
VoidCallback? onPressed,
bool outlined = true,
}) {
return GaDevToolsButton(
icon: icon,
gaScreen: gaScreen,
gaSelection: gaSelection,
outlined: outlined,
tooltip: tooltip,
onPressed: onPressed,
);
}
}
class PauseButton extends GaDevToolsButton {
PauseButton({
super.key,
super.tooltip = 'Pause',
required super.onPressed,
required super.gaScreen,
required super.gaSelection,
super.outlined = true,
super.minScreenWidthForTextBeforeScaling,
bool iconOnly = false,
}) : super(label: iconOnly ? null : 'Pause', icon: Icons.pause);
}
class ResumeButton extends GaDevToolsButton {
ResumeButton({
super.key,
super.tooltip = 'Resume',
required super.onPressed,
required super.gaScreen,
required super.gaSelection,
super.outlined = true,
super.minScreenWidthForTextBeforeScaling,
bool iconOnly = false,
}) : super(label: iconOnly ? null : 'Resume', icon: Icons.play_arrow);
}
/// A button that groups pause and resume controls and automatically manages
/// the button enabled states depending on the value of [paused].
class PauseResumeButtonGroup extends StatelessWidget {
const PauseResumeButtonGroup({
super.key,
required this.paused,
required this.onPause,
required this.onResume,
this.pauseTooltip = 'Pause',
this.resumeTooltip = 'Resume',
required this.gaScreen,
required this.gaSelectionPause,
required this.gaSelectionResume,
});
final bool paused;
final VoidCallback onPause;
final VoidCallback onResume;
final String pauseTooltip;
final String resumeTooltip;
final String gaScreen;
final String gaSelectionPause;
final String gaSelectionResume;
@override
Widget build(BuildContext context) {
return Row(
children: [
PauseButton(
iconOnly: true,
onPressed: paused ? null : onPause,
tooltip: pauseTooltip,
gaScreen: gaScreen,
gaSelection: gaSelectionPause,
),
const SizedBox(width: denseSpacing),
ResumeButton(
iconOnly: true,
onPressed: paused ? onResume : null,
tooltip: resumeTooltip,
gaScreen: gaScreen,
gaSelection: gaSelectionResume,
),
],
);
}
}
class ClearButton extends GaDevToolsButton {
ClearButton({
super.key,
super.color,
super.tooltip = 'Clear',
super.outlined = true,
super.minScreenWidthForTextBeforeScaling,
required super.gaScreen,
required super.gaSelection,
required super.onPressed,
bool iconOnly = false,
String label = 'Clear',
}) : super(icon: Icons.block, label: iconOnly ? null : label);
}
class RefreshButton extends GaDevToolsButton {
RefreshButton({
super.key,
String label = 'Refresh',
super.tooltip,
super.minScreenWidthForTextBeforeScaling,
super.outlined,
required super.gaScreen,
required super.gaSelection,
required super.onPressed,
bool iconOnly = false,
}) : super(icon: Icons.refresh, label: iconOnly ? null : label);
}
/// A Refresh ToolbarAction button.
class ToolbarRefresh extends ToolbarAction {
const ToolbarRefresh({
super.key,
super.icon = Icons.refresh,
required super.onPressed,
super.tooltip = 'Refresh',
});
}
class StartStopRecordingButton extends GaDevToolsButton {
StartStopRecordingButton({
super.key,
required this.recording,
required super.onPressed,
required super.gaScreen,
required super.gaSelection,
super.minScreenWidthForTextBeforeScaling,
String? tooltipOverride,
Color? colorOverride,
String? labelOverride,
}) : super(
icon: _icon(recording),
label: labelOverride ?? _label(recording),
color: colorOverride ?? _color(recording),
tooltip: tooltipOverride ?? _tooltip(recording),
);
static IconData _icon(bool recording) =>
recording ? Icons.stop : Icons.fiber_manual_record;
static String _label(bool recording) =>
recording ? 'Stop recording' : 'Start recording';
static String _tooltip(bool recording) =>
recording ? 'Stop recording' : 'Start recording';
static Color? _color(bool recording) => recording ? Colors.red : null;
final bool recording;
}
/// Button to start recording data.
///
/// * `recording`: Whether recording is in progress.
/// * `minScreenWidthForTextBeforeScaling`: The minimum width the button can be before the text is
/// omitted.
/// * `labelOverride`: Optional alternative text to use for the button.
/// * `onPressed`: The callback to be called upon pressing the button.
class RecordButton extends GaDevToolsButton {
RecordButton({
super.key,
required bool recording,
required VoidCallback onPressed,
required super.gaScreen,
required super.gaSelection,
super.minScreenWidthForTextBeforeScaling,
super.tooltip = 'Start recording',
String? labelOverride,
}) : super(
onPressed: recording ? null : onPressed,
icon: Icons.fiber_manual_record,
label: labelOverride ?? 'Record',
);
}
/// Button to stop recording data.
///
/// * `recording`: Whether recording is in progress.
/// * `minScreenWidthForTextBeforeScaling`: The minimum width the button can be before the text is
/// omitted.
/// * `onPressed`: The callback to be called upon pressing the button.
class StopRecordingButton extends GaDevToolsButton {
StopRecordingButton({
super.key,
required bool recording,
required VoidCallback? onPressed,
required super.gaScreen,
required super.gaSelection,
super.minScreenWidthForTextBeforeScaling,
super.tooltip = 'Stop recording',
}) : super(
onPressed: !recording ? null : onPressed,
icon: Icons.stop,
label: 'Stop',
);
}
class SettingsOutlinedButton extends GaDevToolsButton {
SettingsOutlinedButton({
super.key,
required super.onPressed,
required super.gaScreen,
required super.gaSelection,
super.tooltip,
}) : super(outlined: true, icon: Icons.settings_outlined);
}
class HelpButton extends GaDevToolsButton {
HelpButton({
super.key,
required super.gaScreen,
required super.gaSelection,
required super.onPressed,
super.outlined = true,
}) : super(icon: Icons.help_outline, tooltip: 'Help');
}
class ExpandAllButton extends StatelessWidget {
const ExpandAllButton({
super.key,
required this.gaScreen,
required this.gaSelection,
required this.onPressed,
this.minScreenWidthForTextBeforeScaling,
});
final VoidCallback onPressed;
final String gaScreen;
final String gaSelection;
final double? minScreenWidthForTextBeforeScaling;
@override
Widget build(BuildContext context) {
return GaDevToolsButton(
icon: Icons.unfold_more,
label: 'Expand All',
tooltip: 'Expand All',
onPressed: onPressed,
gaScreen: gaScreen,
gaSelection: gaSelection,
minScreenWidthForTextBeforeScaling: minScreenWidthForTextBeforeScaling,
);
}
}
class CollapseAllButton extends StatelessWidget {
const CollapseAllButton({
super.key,
required this.gaScreen,
required this.gaSelection,
required this.onPressed,
this.minScreenWidthForTextBeforeScaling,
});
final VoidCallback onPressed;
final String gaScreen;
final String gaSelection;
final double? minScreenWidthForTextBeforeScaling;
@override
Widget build(BuildContext context) {
return GaDevToolsButton(
icon: Icons.unfold_less,
label: 'Collapse All',
tooltip: 'Collapse All',
onPressed: onPressed,
gaScreen: gaScreen,
gaSelection: gaSelection,
minScreenWidthForTextBeforeScaling: minScreenWidthForTextBeforeScaling,
);
}
}
/// Button that should be used to control showing or hiding a chart.
///
/// The button automatically toggles the icon and the tooltip to indicate the
/// shown or hidden state.
class VisibilityButton extends StatelessWidget {
const VisibilityButton({
super.key,
required this.show,
required this.onPressed,
this.minScreenWidthForTextBeforeScaling,
required this.label,
required this.tooltip,
required this.gaScreen,
// We use a default value for visibility button because in some cases, the
// analytics for the visibility this button controls are tracked at the
// preferenes change.
this.gaSelection = gac.visibilityButton,
});
final ValueListenable<bool> show;
final void Function(bool) onPressed;
final double? minScreenWidthForTextBeforeScaling;
final String label;
final String tooltip;
final String gaScreen;
final String gaSelection;
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<bool>(
valueListenable: show,
builder: (_, show, _) {
return GaDevToolsButton(
key: key,
tooltip: tooltip,
icon: show ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
label: label,
minScreenWidthForTextBeforeScaling:
minScreenWidthForTextBeforeScaling,
gaScreen: gaScreen,
gaSelection: gaSelection,
onPressed: () => onPressed(!show),
);
},
);
}
}
/// Default switch for DevTools that enforces size restriction.
class DevToolsSwitch extends StatelessWidget {
const DevToolsSwitch({
super.key,
required this.value,
required this.onChanged,
this.padding,
this.height,
this.activeColor,
this.inactiveColor,
});
final bool value;
final void Function(bool)? onChanged;
final EdgeInsets? padding;
final double? height;
final Color? activeColor;
final Color? inactiveColor;
@override
Widget build(BuildContext context) {
return Container(
height: height ?? defaultButtonHeight,
padding: padding,
child: FittedBox(
fit: BoxFit.fill,
child: Switch(
activeTrackColor: activeColor,
inactiveTrackColor: inactiveColor,
value: value,
onChanged: onChanged,
),
),
);
}
}
class ProcessingInfo extends StatelessWidget {
const ProcessingInfo({
super.key,
required this.progressValue,
required this.processedObject,
});
final double? progressValue;
final String processedObject;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Processing $processedObject',
style: Theme.of(context).regularTextStyle,
),
const SizedBox(height: defaultSpacing),
SizedBox(
width: 200.0,
child: LinearProgressIndicator(value: progressValue),
),
],
),
);
}
}
/// Common button for exiting offline mode.
class ExitOfflineButton extends StatelessWidget {
const ExitOfflineButton({required this.gaScreen, super.key});
final String gaScreen;
@override
Widget build(BuildContext context) {
final routerDelegate = DevToolsRouterDelegate.of(context);
return GaDevToolsButton(
key: const Key('exit offline button'),
label: 'Exit offline mode',
icon: Icons.clear,
gaScreen: gaScreen,
gaSelection: gac.stopShowingOfflineData,
onPressed: () {
offlineDataController.stopShowingOfflineData();
// Use Router.neglect to replace the current history entry with
// the homepage so that clicking Back will not return here.
Router.neglect(
context,
() => routerDelegate.navigateHome(clearScreenParam: true),
);
},
);
}
}
class OfflineAwareControls extends StatelessWidget {
const OfflineAwareControls({
required this.controlsBuilder,
required this.gaScreen,
super.key,
});
final Widget Function(bool) controlsBuilder;
final String gaScreen;
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<bool>(
valueListenable: offlineDataController.showingOfflineData,
builder: (context, offline, _) {
return Row(
children: [
if (offlineDataController.showingOfflineData.value)
Padding(
padding: const EdgeInsets.only(right: defaultSpacing),
child: ExitOfflineButton(gaScreen: gaScreen),
),
Expanded(child: controlsBuilder(offline)),
],
);
},
);
}
}
/// A small element containing some accessory information, often a numeric
/// value.
class Badge extends StatelessWidget {
const Badge(this.text, {super.key});
final String text;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
// These constants are sized to give 1 digit badges a circular look.
const badgeCornerRadius = 12.0;
const verticalBadgePadding = 1.0;
const horizontalBadgePadding = 6.0;
return Container(
decoration: BoxDecoration(
color: theme.colorScheme.onSurface,
borderRadius: BorderRadius.circular(badgeCornerRadius),
),
padding: const EdgeInsets.symmetric(
vertical: verticalBadgePadding,
horizontal: horizontalBadgePadding,
),
child: Text(
text,
// Use a slightly smaller font for the badge.
style: theme.regularTextStyle
.copyWith(color: theme.colorScheme.surface)
.apply(fontSizeDelta: -1),
),
);
}
}
/// A wrapper around a TextButton, an Icon, and an optional Tooltip; used for
/// small toolbar actions.
class ToolbarAction extends StatelessWidget {
const ToolbarAction({
required this.icon,
required this.onPressed,
this.tooltip,
super.key,
this.size,
this.style,
this.buttonStyle,
this.color,
this.gaScreen,
this.gaSelection,
}) : assert((gaScreen == null) == (gaSelection == null));
final TextStyle? style;
final ButtonStyle? buttonStyle;
final IconData icon;
final Color? color;
final String? tooltip;
final VoidCallback? onPressed;
final double? size;
final String? gaScreen;
final String? gaSelection;
@override
Widget build(BuildContext context) {
return SmallAction(
onPressed: onPressed,
tooltip: tooltip,
style: style,
buttonStyle: buttonStyle,
gaScreen: gaScreen,
gaSelection: gaSelection,
child: Icon(
icon,
size: size ?? actionsIconSize,
color: color ?? Theme.of(context).colorScheme.onSurface,
),
);
}
}
class SmallAction extends StatelessWidget {
const SmallAction({
required this.child,
required this.onPressed,
this.tooltip,
super.key,
this.style,
this.buttonStyle,
this.gaScreen,
this.gaSelection,
}) : assert((gaScreen == null) == (gaSelection == null));
final TextStyle? style;
final ButtonStyle? buttonStyle;
final Widget child;
final String? tooltip;
final VoidCallback? onPressed;
final String? gaScreen;
final String? gaSelection;
@override
Widget build(BuildContext context) {
final button = TextButton(
style:
buttonStyle ??
TextButton.styleFrom(
padding: EdgeInsets.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
textStyle: style,
),
onPressed: () {
if (gaScreen != null && gaSelection != null) {
ga.select(gaScreen!, gaSelection!);
}
onPressed?.call();
},
child: child,
);
return tooltip == null
? button
: DevToolsTooltip(message: tooltip, child: button);
}
}
/// Icon action button used in the main DevTools toolbar or footer.
abstract class ScaffoldAction extends StatelessWidget {
const ScaffoldAction({
super.key,
required this.tooltip,
required this.onPressed,
this.icon,
this.iconAsset,
this.color,
}) : assert(
(icon == null) != (iconAsset == null),
'Exactly one of icon and iconAsset must be specified.',
);
/// The icon to use for this scaffold action.
///
/// Only one of [icon] or [iconAsset] may be non-null.
final IconData? icon;
/// The icon asset path to render as the icon for this scaffold action.
///
/// Only one of [icon] or [iconAsset] may be non-null.
final String? iconAsset;
final String tooltip;
final void Function(BuildContext) onPressed;
final Color? color;
@override
Widget build(BuildContext context) {
return DevToolsTooltip(
message: tooltip,
child: InkWell(
onTap: () => onPressed(context),
child: Container(
width: actionWidgetSize,
height: actionWidgetSize,
alignment: Alignment.center,
child: DevToolsIcon(
icon: icon,
iconAsset: iconAsset,
size: actionsIconSize,
color: color,
),
),
),
);
}
}
/// Button to open related information / documentation.
///
/// [tooltip] specifies the hover text for the button.
/// [link] is the link that should be opened when the button is clicked.
class InformationButton extends StatelessWidget {
const InformationButton({
super.key,
required this.tooltip,
required this.link,
});
final String tooltip;
final String link;
@override
Widget build(BuildContext context) {
return Tooltip(
message: tooltip,
child: IconButton(
icon: const Icon(Icons.help_outline),
onPressed: () async => await launchUrlWithErrorHandling(link),
),
);
}
}
class OutlinedRowGroup extends StatelessWidget {
const OutlinedRowGroup({super.key, required this.children, this.borderColor});
final List<Widget> children;
final Color? borderColor;
@override
Widget build(BuildContext context) {
final color = borderColor ?? Theme.of(context).focusColor;
final childrenWithOutlines = <Widget>[];
for (int i = 0; i < children.length; i++) {
childrenWithOutlines.addAll([
Container(
decoration: BoxDecoration(
border: Border(
left: i == 0 ? BorderSide(color: color) : BorderSide.none,
right: BorderSide(color: color),
top: BorderSide(color: color),
bottom: BorderSide(color: color),
),
),
child: children[i],
),
]);
}
return Row(mainAxisSize: MainAxisSize.min, children: childrenWithOutlines);
}
}
class ThickDivider extends StatelessWidget {
const ThickDivider({super.key});
static const thickDividerHeight = 5.0;
@override
Widget build(BuildContext context) {
return const Divider(
thickness: thickDividerHeight,
height: thickDividerHeight,
);
}
}
BoxDecoration roundedBorderDecoration(BuildContext context) => BoxDecoration(
border: Border.all(color: Theme.of(context).focusColor),
borderRadius: defaultBorderRadius,
);
class LeftBorder extends StatelessWidget {
const LeftBorder({super.key, this.child});
final Widget? child;
@override
Widget build(BuildContext context) {
final leftBorder = Border(
left: BorderSide(color: Theme.of(context).focusColor),
);
return Container(
decoration: BoxDecoration(border: leftBorder),
child: child,
);
}
}
/// The golden ratio.
///
/// Makes for nice-looking rectangles.
final goldenRatio = 1 + sqrt(5) / 2;
/// A centered text widget with the default DevTools text style applied.
///
/// Only one of [message] or [richMessage] can be specified.
class CenteredMessage extends StatelessWidget {
const CenteredMessage({this.message, this.richMessage, super.key})
: assert((message == null) != (richMessage == null));
final String? message;
final List<InlineSpan>? richMessage;
@override
Widget build(BuildContext context) {
Widget child;
if (message != null) {
child = Text(
message!,
textAlign: TextAlign.center,
style: Theme.of(context).regularTextStyle,
);
} else {
child = RichText(text: TextSpan(children: richMessage));
}
return Center(child: child);
}
}
class CenteredCircularProgressIndicator extends StatelessWidget {
const CenteredCircularProgressIndicator({super.key, this.size});
final double? size;
@override
Widget build(BuildContext context) {
const indicator = Center(child: CircularProgressIndicator());
if (size == null) return indicator;
return SizedBox(width: size, height: size, child: indicator);
}
}
/// An extension on [LinkedScrollControllerGroup] to facilitate having the
/// scrolling widgets auto scroll to the bottom on new content.
///
/// This extension serves the same function as the [ScrollControllerAutoScroll]
/// extension above, but we need to implement these methods again as an
/// extension on [LinkedScrollControllerGroup] because individual
/// [ScrollController]s are intentionally inaccessible from
/// [LinkedScrollControllerGroup].
extension LinkedScrollControllerGroupExtension on LinkedScrollControllerGroup {
bool get atScrollBottom {
final pos = position;
return pos.pixels == pos.maxScrollExtent;
}
/// Scroll the content to the bottom using the app's default animation
/// duration and curve..
void autoScrollToBottom() async {
await animateTo(
position.maxScrollExtent,
duration: rapidDuration,
curve: defaultCurve,
);
// Scroll again if we've received new content in the interim.
if (hasAttachedControllers) {
final pos = position;
if (pos.pixels != pos.maxScrollExtent) {
jumpTo(pos.maxScrollExtent);
}
}
}
}
class BreadcrumbNavigator extends StatelessWidget {
const BreadcrumbNavigator.builder({
super.key,
required this.itemCount,
required this.builder,
});
final int itemCount;
final IndexedWidgetBuilder builder;
@override
Widget build(BuildContext context) {
return Container(
height: Breadcrumb.height + 2 * borderPadding,
alignment: Alignment.centerLeft,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: itemCount,
itemBuilder: builder,
),
);
}
}
class Breadcrumb extends StatelessWidget {
const Breadcrumb({
super.key,
required this.text,
required this.isRoot,
required this.onPressed,
});
static const height = 24.0;
static const caretWidth = 4.0;
final String text;
final bool isRoot;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
// Create the text painter here so that we can calculate `breadcrumbWidth`.
// We need the width for the wrapping Container that gives the CustomPaint
// a bounded width.
final textPainter = TextPainter(
text: TextSpan(
text: text,
style: theme.regularTextStyle.copyWith(
color: theme.colorScheme.contrastTextColor,
decoration: TextDecoration.underline,
),
),
textAlign: TextAlign.right,
textDirection: TextDirection.ltr,
)..layout();
final caretWidth = isRoot
? Breadcrumb.caretWidth
: Breadcrumb.caretWidth * 2;
final breadcrumbWidth = textPainter.width + caretWidth + densePadding * 2;
return InkWell(
onTap: onPressed,
child: Container(
width: breadcrumbWidth,
padding: const EdgeInsets.all(borderPadding),
child: CustomPaint(
painter: _BreadcrumbPainter(
textPainter: textPainter,
isRoot: isRoot,
breadcrumbWidth: breadcrumbWidth,
colorScheme: theme.colorScheme,
),
),
),