SimpleCloudNotifier/flutter/lib/bottom_fab/fab_bottom_app_bar.dart
2024-02-18 16:23:10 +01:00

108 lines
2.8 KiB
Dart

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