-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathbutton.dart
More file actions
96 lines (86 loc) · 2.33 KB
/
button.dart
File metadata and controls
96 lines (86 loc) · 2.33 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
// Copyright 2025 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:jaspr/dom.dart';
import 'package:jaspr/jaspr.dart';
import '../util.dart';
import 'material_icon.dart';
/// A generic button component with different style variants.
class Button extends StatelessComponent {
/// Creates a button with either textual [content], an [icon] ID, or both.
const Button({
super.key,
this.icon,
this.trailingIcon,
this.href,
this.content,
this.style = ButtonStyle.text,
this.id,
this.attributes = const {},
this.classes,
this.disabled = false,
this.title,
this.asRaw = false,
this.onClick,
}) : assert(content != null || icon != null);
final String? content;
final String? title;
final ButtonStyle style;
final String? icon;
final String? trailingIcon;
final String? id;
final String? href;
final Map<String, String> attributes;
final bool disabled;
final bool asRaw;
final List<String>? classes;
final void Function()? onClick;
@override
Component build(BuildContext context) {
final mergedAttributes = <String, String>{
...attributes,
if (disabled) 'disabled': 'disabled',
'title': ?title,
};
final mergedClasses = [
style.cssClass,
if ((icon != null || trailingIcon != null) && content == null)
'icon-button',
...?classes,
].toClasses;
final children = <Component>[
if (icon case final iconId?) MaterialIcon(iconId),
if (content case final contentText?)
asRaw ? RawText(contentText) : .text(contentText),
if (trailingIcon case final iconId?) MaterialIcon(iconId),
];
if (href case final href?) {
return a(
id: id,
href: href,
classes: mergedClasses,
attributes: mergedAttributes,
onClick: onClick,
children,
);
} else {
return button(
id: id,
classes: mergedClasses,
attributes: mergedAttributes,
onClick: onClick,
children,
);
}
}
}
enum ButtonStyle {
filled,
outlined,
text;
String get cssClass => switch (this) {
ButtonStyle.filled => 'filled-button',
ButtonStyle.outlined => 'outlined-button',
ButtonStyle.text => 'text-button',
};
}