Skip to content

Commit

Permalink
Custom Icons in appBar
Browse files Browse the repository at this point in the history
  • Loading branch information
Overman775 committed Jun 5, 2020
1 parent a5c2e5e commit 1b29e51
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
53 changes: 53 additions & 0 deletions lib/src/theme/app_bar.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'package:flutter_neumorphic/flutter_neumorphic.dart';

@immutable
Expand All @@ -10,6 +13,7 @@ class NeumorphicAppBarThemeData {
final EdgeInsets buttonPadding;
final bool centerTitle;
final TextStyle textStyle;
final NeumorphicAppBarIcons icons;

const NeumorphicAppBarThemeData({
this.color = Colors.transparent,
Expand All @@ -18,5 +22,54 @@ class NeumorphicAppBarThemeData {
this.buttonStyle = const NeumorphicStyle(),
this.centerTitle,
this.buttonPadding = const EdgeInsets.all(0),
this.icons = const NeumorphicAppBarIcons(),
});
}

class NeumorphicAppBarIcons {
final Icon closeIcon;
final Icon menuIcon;
final Icon _backIcon;

const NeumorphicAppBarIcons({
this.menuIcon = const Icon(Icons.menu),
this.closeIcon = const Icon(Icons.close),
Icon backIcon,
}) : _backIcon = backIcon;

//if back icon null then get platform oriented icon
Icon get backIcon => _backIcon ?? _getBackIcon;
Icon get _getBackIcon => Platform.isIOS || Platform.isMacOS
? const Icon(Icons.arrow_back_ios)
: const Icon(Icons.arrow_back);

NeumorphicAppBarIcons copyWith({
Icon backIcon,
Icon closeIcon,
Icon menuIcon,
}) {
return NeumorphicAppBarIcons(
backIcon: backIcon ?? this.backIcon,
closeIcon: closeIcon ?? this.closeIcon,
menuIcon: menuIcon ?? this.menuIcon,
);
}

@override
bool operator ==(Object o) {
if (identical(this, o)) return true;

return o is NeumorphicAppBarIcons &&
o.backIcon == backIcon &&
o.closeIcon == closeIcon &&
o.menuIcon == menuIcon;
}

@override
int get hashCode =>
backIcon.hashCode ^ closeIcon.hashCode ^ menuIcon.hashCode;

@override
String toString() =>
'NeumorphicAppBarIcons(backIcon: $backIcon, closeIcon: $closeIcon, menuIcon: $menuIcon)';
}
4 changes: 2 additions & 2 deletions lib/src/widget/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class NeumorphicAppBarState extends State<NeumorphicAppBar> {
leading = NeumorphicButton(
padding: widget.buttonPadding,
style: widget.buttonStyle,
child: Icon(Icons.menu),
child: nTheme.current.appBarTheme.icons.menuIcon,
onPressed: _handleDrawerButton,
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
);
Expand Down Expand Up @@ -211,7 +211,7 @@ class NeumorphicAppBarState extends State<NeumorphicAppBar> {
child: NeumorphicButton(
padding: widget.buttonPadding,
style: widget.buttonStyle,
child: Icon(Icons.menu),
child: nTheme.current.appBarTheme.icons.menuIcon,
onPressed: _handleDrawerButtonEnd,
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
),
Expand Down
24 changes: 9 additions & 15 deletions lib/src/widget/back_button.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math' as math show pi;

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
Expand All @@ -6,36 +8,28 @@ class NeumorphicBackButton extends StatelessWidget {
final VoidCallback onPressed;
final NeumorphicStyle style;
final EdgeInsets padding;
final bool reversedArrow;
final bool reversedIcon;

const NeumorphicBackButton({
Key key,
this.onPressed,
this.style,
this.padding,
this.reversedArrow = false,
this.reversedIcon = false,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final nThemeIcons = NeumorphicTheme.of(context).current.appBarTheme.icons;
return NeumorphicButton(
style: style,
padding: padding,
tooltip: MaterialLocalizations.of(context).backButtonTooltip,
child: Icon(_getIcon(theme)),
child: reversedIcon
? Transform.rotate(
angle: 180 * math.pi / 180, child: nThemeIcons.backIcon)
: nThemeIcons.backIcon,
onPressed: onPressed ?? () => Navigator.maybePop(context),
);
}

IconData _getIcon(ThemeData theme) {
if (reversedArrow) {
return [TargetPlatform.iOS, TargetPlatform.macOS].contains(theme.platform)
? Icons.arrow_forward_ios
: Icons.arrow_forward;
}
return [TargetPlatform.iOS, TargetPlatform.macOS].contains(theme.platform)
? Icons.arrow_back_ios
: Icons.arrow_back;
}
}
12 changes: 9 additions & 3 deletions lib/src/widget/close_button.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math' as math show pi;

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
Expand All @@ -6,23 +8,27 @@ class NeumorphicCloseButton extends StatelessWidget {
final VoidCallback onPressed;
final NeumorphicStyle style;
final EdgeInsets padding;
final bool reversedIcon;

const NeumorphicCloseButton({
Key key,
this.onPressed,
this.style,
this.padding,
this.reversedIcon = false,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final nThemeIcons = NeumorphicTheme.of(context).current.appBarTheme.icons;
return NeumorphicButton(
style: style,
padding: padding,
tooltip: MaterialLocalizations.of(context).closeButtonTooltip,
child: Icon(
Icons.close,
),
child: reversedIcon
? Transform.rotate(
angle: 180 * math.pi / 180, child: nThemeIcons.closeIcon)
: nThemeIcons.closeIcon,
onPressed: onPressed ?? () => Navigator.maybePop(context),
);
}
Expand Down

0 comments on commit 1b29e51

Please sign in to comment.