SimpleCloudNotifier/flutter/lib/components/bottom_fab/fab_bottom_app_bar.dart

108 lines
2.8 KiB
Dart
Raw Permalink Normal View History

2024-02-10 19:57:17 +01:00
import 'package:flutter/material.dart';
class FABBottomAppBarItem {
FABBottomAppBarItem({required this.iconData, required this.text});
IconData iconData;
String text;
}
2024-02-18 16:23:10 +01:00
class FABBottomAppBar extends StatelessWidget {
2024-02-10 19:57:17 +01:00
FABBottomAppBar({
super.key,
required this.items,
this.centerItemText,
this.height = 60.0,
this.iconSize = 24.0,
this.backgroundColor,
this.color,
this.selectedColor,
this.notchedShape,
this.onTabSelected,
2024-02-18 16:23:10 +01:00
this.selectedIndex = 0,
2024-02-10 19:57:17 +01:00
}) {
assert(items.length == 2 || items.length == 4);
}
final List<FABBottomAppBarItem> items;
final String? centerItemText;
final double height;
final double iconSize;
final Color? backgroundColor;
final Color? color;
final Color? selectedColor;
final NotchedShape? notchedShape;
final ValueChanged<int>? onTabSelected;
2024-02-18 16:23:10 +01:00
final int selectedIndex;
2024-02-10 19:57:17 +01:00
@override
Widget build(BuildContext context) {
2024-02-18 16:23:10 +01:00
List<Widget> items = List.generate(this.items.length, (int index) {
2024-02-10 19:57:17 +01:00
return _buildTabItem(
2024-02-18 16:23:10 +01:00
item: this.items[index],
2024-02-10 19:57:17 +01:00
index: index,
onPressed: _updateIndex,
);
});
items.insert(items.length >> 1, _buildMiddleTabItem());
return BottomAppBar(
2024-02-18 16:23:10 +01:00
shape: notchedShape,
color: backgroundColor,
2024-02-10 19:57:17 +01:00
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: items,
),
);
}
Widget _buildMiddleTabItem() {
return Expanded(
child: SizedBox(
2024-02-18 16:23:10 +01:00
height: height,
2024-02-10 19:57:17 +01:00
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2024-02-18 16:23:10 +01:00
SizedBox(height: iconSize),
2024-02-10 19:57:17 +01:00
Text(
2024-02-18 16:23:10 +01:00
centerItemText ?? '',
style: TextStyle(color: color),
2024-02-10 19:57:17 +01:00
),
],
),
),
);
}
Widget _buildTabItem({required FABBottomAppBarItem item, required int index, required ValueChanged<int> onPressed}) {
2024-02-18 16:23:10 +01:00
Color color = (selectedIndex == index ? selectedColor : this.color) ?? Colors.black;
2024-02-10 19:57:17 +01:00
return Expanded(
child: SizedBox(
2024-02-18 16:23:10 +01:00
height: height,
2024-02-10 19:57:17 +01:00
child: Material(
type: MaterialType.transparency,
child: InkWell(
onTap: () => onPressed(index),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2024-02-18 16:23:10 +01:00
Icon(item.iconData, color: color, size: iconSize),
2024-02-10 19:57:17 +01:00
Text(
item.text,
style: TextStyle(color: color),
)
],
),
),
),
),
);
}
2024-02-18 16:23:10 +01:00
void _updateIndex(int index) {
if (onTabSelected != null) onTabSelected!(index);
}
2024-02-10 19:57:17 +01:00
}