Skip to content

Instantly share code, notes, and snippets.

@ponnamkarthik
Created June 8, 2021 05:22
Show Gist options
  • Save ponnamkarthik/5d894ca4a436377b93b60950b4453cfb to your computer and use it in GitHub Desktop.
Save ponnamkarthik/5d894ca4a436377b93b60950b4453cfb to your computer and use it in GitHub Desktop.
Flutter Widget for toggle bar
import 'package:flutter/material.dart';
class CustomToggle extends StatefulWidget {
const CustomToggle({this.activeIndex = 1, @required this.items, this.onChanged});
final int activeIndex;
final List<String> items;
final Function(int) onChanged;
@override
_CustomToggleState createState() => _CustomToggleState();
}
class _CustomToggleState extends State<CustomToggle> {
int activeIndex;
@override
void initState() {
super.initState();
activeIndex = widget.activeIndex;
}
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
color: const Color(0xFFEFF3F4),
borderRadius: BorderRadius.circular(6),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
for (int i = 0; i < widget.items.length; i++)
InkWell(
onTap: () {
setState(() {
activeIndex = i;
});
widget.onChanged?.call(i);
},
child: Container(
padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 24),
decoration: BoxDecoration(
color: activeIndex == i ? Colors.white : Colors.transparent,
borderRadius: BorderRadius.circular(6),
),
child: Text(widget.items[i]),
),
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment