Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add YaruBackButtonTheme & YaruBackButtonStyle (rounded vs. square) #531

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/src/controls/yaru_back_button.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import 'package:flutter/material.dart';
import 'package:yaru_icons/yaru_icons.dart';

import 'yaru_back_button_theme.dart';
import 'yaru_icon_button.dart';

/// A Yaru style back button.
class YaruBackButton extends StatelessWidget {
/// Creates a [YaruBackButton].
const YaruBackButton({super.key, this.onPressed});
const YaruBackButton({
super.key,
this.onPressed,
this.style,
});

/// An optional callback that is called when the button is pressed.
///
/// By default, [Navigator.maybePop] is called.
final VoidCallback? onPressed;

/// The style of the button. Overrides [YaruBackButtonThemeData.style] and
/// defaults to [YaruBackButtonStyle.square].
final YaruBackButtonStyle? style;

@override
Widget build(BuildContext context) {
return YaruIconButton(
final theme = YaruBackButtonTheme.of(context);
final round = (style ?? theme?.style) == YaruBackButtonStyle.rounded;
final shape = round ? const CircleBorder() : const BeveledRectangleBorder();
final button = YaruIconButton(
icon: const Icon(YaruIcons.go_previous),
style: ButtonStyle(
shape: ButtonStyleButton.allOrNull(const BeveledRectangleBorder()),
shape: ButtonStyleButton.allOrNull(shape),
),
onPressed: () {
if (onPressed != null) {
Expand All @@ -28,5 +40,6 @@ class YaruBackButton extends StatelessWidget {
}
},
);
return round ? Center(child: button) : button;
}
}
80 changes: 80 additions & 0 deletions lib/src/controls/yaru_back_button_theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

enum YaruBackButtonStyle { rounded, square }

@immutable
class YaruBackButtonThemeData extends ThemeExtension<YaruBackButtonThemeData>
with Diagnosticable {
/// Creates a theme that can be used with [YaruBackButton].
const YaruBackButtonThemeData({
this.style,
});

/// The button style to use.
final YaruBackButtonStyle? style;

/// Creates a copy of this object but with the given fields replaced with the
/// new values.
@override
YaruBackButtonThemeData copyWith({
YaruBackButtonStyle? style,
}) {
return YaruBackButtonThemeData(
style: style ?? this.style,
);
}

@override
ThemeExtension<YaruBackButtonThemeData> lerp(
ThemeExtension<YaruBackButtonThemeData>? other,
double t,
) {
final o = other as YaruBackButtonThemeData?;
return YaruBackButtonThemeData(
style: t < 0.5 ? style : o?.style,
);
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(EnumProperty('style', style));
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is YaruBackButtonThemeData && other.style == style;
}

@override
int get hashCode => style.hashCode;
}

class YaruBackButtonTheme extends InheritedTheme {
const YaruBackButtonTheme({
super.key,
required this.data,
required super.child,
});

final YaruBackButtonThemeData data;

static YaruBackButtonThemeData? of(BuildContext context) {
final theme =
context.dependOnInheritedWidgetOfExactType<YaruBackButtonTheme>();
return theme?.data ??
Theme.of(context).extension<YaruBackButtonThemeData>();
}

@override
Widget wrap(BuildContext context, Widget child) {
return YaruBackButtonTheme(data: data, child: child);
}

@override
bool updateShouldNotify(YaruBackButtonTheme oldWidget) {
return data != oldWidget.data;
}
}
1 change: 1 addition & 0 deletions lib/yaru_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ library yaru_widgets;
export 'src/constants.dart';
// Controls
export 'src/controls/yaru_back_button.dart';
export 'src/controls/yaru_back_button_theme.dart';
export 'src/controls/yaru_check_button.dart';
export 'src/controls/yaru_checkbox.dart';
export 'src/controls/yaru_checkbox_list_tile.dart';
Expand Down