Created
January 2, 2023 05:37
-
-
Save dhruvilp/38d6e5e65a63e85f23d955d6557a4282 to your computer and use it in GitHub Desktop.
Flutter SIngle Select Buttonnbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
final List<Widget> buttons = <Widget>[ | |
Row( | |
children: const [ | |
Text('APPLE'), | |
SizedBox(width: 5), | |
Icon(Icons.sunny), | |
], | |
), | |
Row( | |
children: const [ | |
Text('CLOUD'), | |
SizedBox(width: 5), | |
Icon(Icons.cloud), | |
], | |
), | |
Row( | |
children: const [ | |
Text('AC UNIT'), | |
SizedBox(width: 5), | |
Icon(Icons.ac_unit), | |
], | |
), | |
]; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
static const String _title = 'ToggleButtons Sample'; | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
title: _title, | |
home: ToggleButtonsSample(title: _title), | |
); | |
} | |
} | |
class ToggleButtonsSample extends StatefulWidget { | |
const ToggleButtonsSample({super.key, required this.title}); | |
final String title; | |
@override | |
State<ToggleButtonsSample> createState() => _ToggleButtonsSampleState(); | |
} | |
class _ToggleButtonsSampleState extends State<ToggleButtonsSample> { | |
final List<bool> _selectedButtons = <bool>[true, false, false]; | |
@override | |
Widget build(BuildContext context) { | |
final ThemeData theme = Theme.of(context); | |
Widget multiSelectButtons() { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Text('Single-select', style: theme.textTheme.titleSmall), | |
const SizedBox(height: 5), | |
ToggleButtons( | |
direction: Axis.horizontal, | |
onPressed: (int index) { | |
setState(() { | |
for (int i = 0; i < _selectedButtons.length; i++) { | |
print('${_selectedButtons[i]} | $i | $index'); | |
_selectedButtons[i] = i == index; | |
} | |
}); | |
}, | |
borderRadius: const BorderRadius.all(Radius.circular(8)), | |
selectedBorderColor: Colors.blue[700], | |
selectedColor: Colors.white, | |
fillColor: Colors.blue[600], | |
color: Colors.blue[400], | |
constraints: const BoxConstraints( | |
minHeight: 40.0, | |
minWidth: 120.0, | |
), | |
isSelected: _selectedButtons, | |
children: buttons, | |
), | |
], | |
); | |
} | |
return Scaffold( | |
appBar: AppBar(title: Text(widget.title)), | |
body: SafeArea( | |
child: SingleChildScrollView( | |
scrollDirection: Axis.horizontal, | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
multiSelectButtons(), | |
const SizedBox(width: 15), | |
multiSelectButtons(), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment