Toggle Bar In Flutter :
Screenshot :

toggle_bar.dart
library toggle_bar; import 'dart:collection'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; class ToggleBar extends StatefulWidget { final TextStyle labelTextStyle; final Color backgroundColor; final BoxBorder backgroundBorder; final Color selectedTabColor; final Color selectedTextColor; final Color textColor; final List<String> labels; final Function(int) onSelectionUpdated; ToggleBar( {@required this.labels, this.backgroundColor = Colors.black, this.backgroundBorder, this.selectedTabColor = Colors.deepPurple, this.selectedTextColor = Colors.white, this.textColor = Colors.black, this.labelTextStyle = const TextStyle(), this.onSelectionUpdated}); @override State<StatefulWidget> createState() { return _ToggleBarState(); } } class _ToggleBarState extends State<ToggleBar> { LinkedHashMap<String, bool> _hashMap = LinkedHashMap(); int _selectedIndex = 0; @override void initState() { _hashMap = LinkedHashMap.fromIterable(widget.labels, value: (value) => value = false); _hashMap[widget.labels[0]] = true; super.initState(); } @override Widget build(BuildContext context) { return Container( height: 48, margin: EdgeInsets.all(8.0), padding: EdgeInsets.all(8.0), decoration: BoxDecoration( color: widget.backgroundColor, border: widget.backgroundBorder, borderRadius: BorderRadius.circular(50)), child: ListView.builder( itemCount: widget.labels.length, scrollDirection: Axis.horizontal, itemBuilder: (context, index) { return GestureDetector( child: Container( width: (MediaQuery.of(context).size.width - 32) / widget.labels.length, padding: EdgeInsets.only(left: 8, right: 8, top: 8, bottom: 8), child: Text(_hashMap.keys.elementAt(index), textAlign: TextAlign.center, style: widget.labelTextStyle.apply( color: _hashMap.values.elementAt(index) ? widget.selectedTextColor : widget.textColor)), decoration: BoxDecoration( color: _hashMap.values.elementAt(index) ? widget.selectedTabColor : null, borderRadius: BorderRadius.circular(50))), onHorizontalDragUpdate: (dragUpdate) async { int calculatedIndex = ((widget.labels.length * (dragUpdate.globalPosition.dx / (MediaQuery.of(context).size.width - 32))) .round() - 1) .clamp(0, widget.labels.length - 1); if (calculatedIndex != _selectedIndex) { _updateSelection(calculatedIndex); } }, onTap: () async { if (index != _selectedIndex) { _updateSelection(index); } }); })); } _updateSelection(int index) { setState(() { _selectedIndex = index; widget.onSelectionUpdated(_selectedIndex); _hashMap.updateAll((label, selected) => selected = false); _hashMap[_hashMap.keys.elementAt(index)] = true; }); } }
main.dart
import 'package:flutter/material.dart'; import 'toggle_bar.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Tutorial', theme: ThemeData( primarySwatch: Colors.deepPurple, ), debugShowCheckedModeBanner: false, home: MyHomePage(title: 'Toggle Bar in Flutter'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { List<String> labels = ["Apples", "Bananas", "Oranges", "Papayas"]; int currentIndex = 0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Column( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ ToggleBar( labels: labels, backgroundColor: Colors.grey.withOpacity(0.1), onSelectionUpdated: (index) => setState(() => currentIndex = index), ), Text( labels[currentIndex], style: Theme.of(context).textTheme.display1, ) ])); } }
The flutter tutorial is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application
For more information about Flutter. visit www.fluttertutorial.in