Files
HiddingselAppOriginal/lib/packages/icon_switch/icon_switch.dart
2026-02-13 15:53:22 +01:00

61 lines
1.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:hiddingsel_app/constants/constant.dart';
/**
* Inspiration: ToggleButtons https://www.youtube.com/watch?v=kVEguaQWGAY
*
*/
class IconSwitch extends StatelessWidget {
final Future<bool> _initFavorite;
final Function(bool favorite) _onFavorite;
final Gradient _gradientColor;
final Color _defaultColor;
final IconData _iconFavorite;
final IconData _iconDefavorite;
IconSwitch(this._onFavorite, {initFavorite, gradientColor, defaultColor = UIColors.grey5, iconFavorite = Icons.star, iconDefavorite = Icons.star_border_outlined})
: _initFavorite = initFavorite ?? Future.value(false),
_gradientColor = gradientColor ??
LinearGradient(colors: [
UIColors.white,
UIColors.white,
]),
_defaultColor = defaultColor,
_iconFavorite = iconFavorite,
_iconDefavorite = iconDefavorite;
@override
Widget build(BuildContext context) => FutureBuilder(
future: _initFavorite,
builder: (context, AsyncSnapshot snapshot) {
bool _favorite = snapshot.data ?? false;
return StatefulBuilder(
builder: (context, setState) => IconButton(
icon: _favorite
? ShaderMask(
shaderCallback: (Rect bounds) {
return _gradientColor.createShader(bounds);
},
child: Icon(
_iconFavorite,
color: Colors.white,
),
)
: Icon(
_iconDefavorite,
color: _defaultColor,
),
tooltip: 'Favorite',
onPressed: () {
_onFavorite(!_favorite);
setState(() {
_favorite = !_favorite;
});
},
),
);
},
);
}