Files
FlClash/lib/widgets/card.dart
T
chen08209 ee6a49713a feat(ui): draw the app's own glyphs and redraw the theme page
Drops the Material compatibility bridge and the icon font.

Changelog: New app glyphs, empty states and theme page
2026-10-02 02:51:32 +08:00

397 lines
11 KiB
Dart

import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/icons/icons.dart';
import 'package:fl_clash/state.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter/services.dart';
import 'fade_box.dart';
import 'scaffold.dart';
import 'text.dart';
class Info {
final String label;
final Glyph? glyph;
const Info({required this.label, this.glyph});
}
class InfoHeader extends StatelessWidget {
final Info info;
final List<Widget> actions;
final EdgeInsets? padding;
final double? space;
const InfoHeader({
super.key,
required this.info,
this.padding,
this.space,
List<Widget>? actions,
}) : actions = actions ?? const [];
@override
Widget build(BuildContext context) {
EdgeInsetsGeometry nextPadding = (padding ?? baseInfoEdgeInsets);
if (actions.isNotEmpty) {
nextPadding = nextPadding.subtract(EdgeInsets.symmetric(vertical: 8.mAp));
}
return Padding(
padding: nextPadding,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
flex: 1,
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
if (info.glyph case final glyph?) ...[
GlyphIcon(
glyph,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 8),
],
Flexible(
flex: 1,
child: TooltipText(
text: Text(
info.label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.titleSmall?.copyWith(
color: context.colorScheme.onSurfaceVariant,
),
),
),
),
],
),
),
const SizedBox(width: 8),
if (actions.isNotEmpty)
SizedBox(
height: globalState.measure.titleSmallHeight + 16.ap,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
spacing: space ?? appBarActionSpace,
children: [...actions],
),
),
],
),
);
}
}
const commonCardIconSize = 20.0;
class CommonCard extends StatelessWidget {
const CommonCard({
super.key,
bool? isSelected,
this.type = CommonCardType.plain,
this.onPressed,
this.selectWidget,
this.radius,
this.padding,
this.enterAnimated = false,
this.info,
this.infoPadding,
this.onLongPress,
this.shape,
this.isError = false,
this.enterActionsOnRight = false,
this.skipTraversal = false,
required this.child,
}) : isSelected = isSelected ?? false;
final bool enterAnimated;
final bool enterActionsOnRight;
final bool skipTraversal;
final bool isSelected;
final bool isError;
final void Function()? onPressed;
final void Function()? onLongPress;
final Widget? selectWidget;
final Widget child;
final EdgeInsets? padding;
final Info? info;
final EdgeInsets? infoPadding;
final CommonCardType type;
final double? radius;
final OutlinedBorder? shape;
BorderSide _buildBorderSide(BuildContext context, Set<WidgetState> states) {
final colorScheme = context.colorScheme;
if (isError) {
if (type == CommonCardType.filled) {
return BorderSide(color: colorScheme.error);
}
final hoverColor = isSelected
? colorScheme.error.opacity80
: colorScheme.error.opacity38;
if (states.contains(WidgetState.hovered) ||
states.contains(WidgetState.focused) ||
states.contains(WidgetState.pressed)) {
return BorderSide(color: hoverColor);
}
return BorderSide(
color: isSelected
? colorScheme.error.opacity60
: colorScheme.error.opacity30,
);
}
if (type == CommonCardType.filled) {
return BorderSide.none;
}
final hoverColor = isSelected
? colorScheme.primary.opacity80
: colorScheme.primary.opacity60;
if (states.contains(WidgetState.hovered) ||
states.contains(WidgetState.focused) ||
states.contains(WidgetState.pressed)) {
return BorderSide(color: hoverColor);
}
return BorderSide(
color: isSelected
? colorScheme.primary
: colorScheme.surfaceContainerHighest,
);
}
Color? _buildBackgroundColor(BuildContext context) {
final colorScheme = context.colorScheme;
if (type == CommonCardType.filled) {
if (isSelected) {
return colorScheme.secondaryContainer.opacity80;
}
return colorScheme.surfaceContainerHigh;
}
if (isSelected) {
return colorScheme.secondaryContainer;
}
return colorScheme.surfaceContainerLow;
}
Color? _buildForegroundColor(BuildContext context) {
final colorScheme = context.colorScheme;
if (isError) {
return colorScheme.error;
}
if (type == CommonCardType.filled) {
if (isSelected) {
return colorScheme.onSecondaryContainer;
}
return colorScheme.onSurfaceVariant;
}
if (isSelected) {
return colorScheme.onSecondaryContainer;
}
return colorScheme.onSurfaceVariant;
}
Color? _buildIconColor(BuildContext context) {
final colorScheme = context.colorScheme;
if (isError) {
return colorScheme.error;
}
return colorScheme.primary;
}
Widget _buildButton(
BuildContext context,
Widget childWidget,
FocusNode? focusNode,
) {
return switch (type == CommonCardType.filled) {
true => FilledButton(
focusNode: focusNode,
onLongPress: onLongPress,
clipBehavior: Clip.antiAlias,
style:
FilledButton.styleFrom(
padding: padding ?? EdgeInsets.zero,
shape: shape ?? AppShape.all(radius ?? AppCorner.md),
iconSize: commonCardIconSize,
iconColor: _buildIconColor(context),
foregroundColor: _buildForegroundColor(context),
side: BorderSide.none,
elevation: 0,
).copyWith(
backgroundColor: WidgetStatePropertyAll(
_buildBackgroundColor(context),
),
side: WidgetStateProperty.resolveWith(
(states) => _buildBorderSide(context, states),
),
),
onPressed: onPressed,
child: childWidget,
),
false => OutlinedButton(
focusNode: focusNode,
onLongPress: onLongPress,
clipBehavior: Clip.antiAlias,
style:
OutlinedButton.styleFrom(
padding: padding ?? EdgeInsets.zero,
shape: shape ?? AppShape.all(radius ?? AppCorner.md),
iconSize: commonCardIconSize,
iconColor: _buildIconColor(context),
backgroundColor: _buildBackgroundColor(context),
foregroundColor: _buildForegroundColor(context),
elevation: 0,
).copyWith(
side: WidgetStateProperty.resolveWith(
(states) => _buildBorderSide(context, states),
),
),
onPressed: onPressed,
child: childWidget,
),
};
}
@override
Widget build(BuildContext context) {
var childWidget = child;
if (info != null) {
childWidget = Column(
mainAxisSize: MainAxisSize.min,
children: [
InfoHeader(
padding: infoPadding ?? baseInfoEdgeInsets.copyWith(bottom: 0),
info: info!,
),
Flexible(flex: 1, child: child),
],
);
}
if (selectWidget != null && isSelected) {
final List<Widget> children = [];
children.add(childWidget);
children.add(Positioned.fill(child: selectWidget!));
childWidget = Stack(children: children);
}
final button = skipTraversal
? _SkipTraversalScope(
builder: (focusNode) =>
_buildButton(context, childWidget, focusNode),
)
: _buildButton(context, childWidget, null);
final card = !enterActionsOnRight
? button
: Focus(
canRequestFocus: false,
onKeyEvent: (_, event) {
if (event is! KeyDownEvent ||
event.logicalKey != LogicalKeyboardKey.arrowRight) {
return KeyEventResult.ignored;
}
final focusNode = FocusManager.instance.primaryFocus;
final context = focusNode?.context;
if (focusNode == null || context == null) {
return KeyEventResult.ignored;
}
final action = focusNode.descendants
.where((node) => node.skipTraversal && node.canRequestFocus)
.firstOrNull;
if (action != null) {
action.requestFocus();
return KeyEventResult.handled;
}
if (focusNode.skipTraversal ||
context.findAncestorWidgetOfExactType<IconButton>() != null) {
return KeyEventResult.ignored;
}
return focusNode.nextFocus()
? KeyEventResult.handled
: KeyEventResult.ignored;
},
child: button,
);
return switch (enterAnimated) {
true => FadeScaleEnterBox(child: card),
false => card,
};
}
}
/// A focus node left out of the traversal that walks between cards: a card
/// that must not be a stop, or an action inside a card reached by arrow right.
class SkipTraversalFocusNode extends FocusNode {
@override
bool get skipTraversal => true;
}
class _SkipTraversalScope extends StatefulWidget {
const _SkipTraversalScope({required this.builder});
final Widget Function(FocusNode focusNode) builder;
@override
State<_SkipTraversalScope> createState() => _SkipTraversalScopeState();
}
class _SkipTraversalScopeState extends State<_SkipTraversalScope> {
final FocusNode _focusNode = SkipTraversalFocusNode();
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.builder(_focusNode);
}
}
class SelectIcon extends StatelessWidget {
const SelectIcon({super.key});
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.inversePrimary,
shape: AppShape.circle,
child: Container(
padding: const EdgeInsets.all(4),
child: const GlyphIcon(AppGlyphs.check, size: 16),
),
);
}
}
class SettingsBlock extends StatelessWidget {
final String title;
final List<Widget> settings;
const SettingsBlock({super.key, required this.title, required this.settings});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
InfoHeader(info: Info(label: title)),
Card(
color: context.colorScheme.surfaceContainer,
child: Column(children: settings),
),
],
),
);
}
}