|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Flutter code sample for ToggleButtons |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | + |
| 9 | +const List<Widget> fruits = <Widget>[ |
| 10 | + Text('Apple'), |
| 11 | + Text('Banana'), |
| 12 | + Text('Orange') |
| 13 | +]; |
| 14 | + |
| 15 | +const List<Widget> vegetables = <Widget>[ |
| 16 | + Text('Tomatoes'), |
| 17 | + Text('Potatoes'), |
| 18 | + Text('Carrots') |
| 19 | +]; |
| 20 | + |
| 21 | +const List<Widget> icons = <Widget>[ |
| 22 | + Icon(Icons.sunny), |
| 23 | + Icon(Icons.cloud), |
| 24 | + Icon(Icons.ac_unit), |
| 25 | +]; |
| 26 | + |
| 27 | +void main() => runApp(const MyApp()); |
| 28 | + |
| 29 | +class MyApp extends StatelessWidget { |
| 30 | + const MyApp({Key? key}) : super(key: key); |
| 31 | + |
| 32 | + static const String _title = 'ToggleButtons Sample'; |
| 33 | + |
| 34 | + @override |
| 35 | + Widget build(BuildContext context) { |
| 36 | + return const MaterialApp( |
| 37 | + title: _title, |
| 38 | + home: ToggleButtonsSample(title: _title), |
| 39 | + ); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +class ToggleButtonsSample extends StatefulWidget { |
| 44 | + const ToggleButtonsSample({Key? key, required this.title}) : super(key: key); |
| 45 | + |
| 46 | + final String title; |
| 47 | + |
| 48 | + @override |
| 49 | + State<ToggleButtonsSample> createState() => _ToggleButtonsSampleState(); |
| 50 | +} |
| 51 | + |
| 52 | +class _ToggleButtonsSampleState extends State<ToggleButtonsSample> { |
| 53 | + final List<bool> _selectedFruits = <bool>[true, false, false]; |
| 54 | + final List<bool> _selectedVegetables = <bool>[false, true, false]; |
| 55 | + final List<bool> _selectedWeather = <bool>[false, false, true]; |
| 56 | + bool vertical = false; |
| 57 | + |
| 58 | + @override |
| 59 | + Widget build(BuildContext context) { |
| 60 | + final ThemeData theme = Theme.of(context); |
| 61 | + |
| 62 | + return Scaffold( |
| 63 | + appBar: AppBar(title: Text(widget.title)), |
| 64 | + body: Center( |
| 65 | + child: SingleChildScrollView( |
| 66 | + child: Column( |
| 67 | + mainAxisSize: MainAxisSize.min, |
| 68 | + mainAxisAlignment: MainAxisAlignment.center, |
| 69 | + children: <Widget>[ |
| 70 | + // ToggleButtons with a single selection. |
| 71 | + Text('Single-select', style: theme.textTheme.subtitle2), |
| 72 | + const SizedBox(height: 5), |
| 73 | + ToggleButtons( |
| 74 | + direction: vertical ? Axis.vertical : Axis.horizontal, |
| 75 | + onPressed: (int index) { |
| 76 | + setState(() { |
| 77 | + // The button that is tapped is set to true, and the others to false. |
| 78 | + for (int i = 0; i < _selectedFruits.length; i++) { |
| 79 | + _selectedFruits[i] = i == index; |
| 80 | + } |
| 81 | + }); |
| 82 | + }, |
| 83 | + borderRadius: const BorderRadius.all(Radius.circular(8)), |
| 84 | + selectedBorderColor: Colors.red[700], |
| 85 | + selectedColor: Colors.white, |
| 86 | + fillColor: Colors.red[200], |
| 87 | + color: Colors.red[400], |
| 88 | + constraints: const BoxConstraints( |
| 89 | + minHeight: 40.0, |
| 90 | + minWidth: 80.0, |
| 91 | + ), |
| 92 | + isSelected: _selectedFruits, |
| 93 | + children: fruits, |
| 94 | + ), |
| 95 | + const SizedBox(height: 20), |
| 96 | + // ToggleButtons with a multiple selection. |
| 97 | + Text('Multi-select', style: theme.textTheme.subtitle2), |
| 98 | + const SizedBox(height: 5), |
| 99 | + ToggleButtons( |
| 100 | + direction: vertical ? Axis.vertical : Axis.horizontal, |
| 101 | + onPressed: (int index) { |
| 102 | + // All buttons are selectable. |
| 103 | + setState(() { |
| 104 | + _selectedVegetables[index] = |
| 105 | + !_selectedVegetables[index]; |
| 106 | + }); |
| 107 | + }, |
| 108 | + borderRadius: const BorderRadius.all(Radius.circular(8)), |
| 109 | + selectedBorderColor: Colors.green[700], |
| 110 | + selectedColor: Colors.white, |
| 111 | + fillColor: Colors.green[200], |
| 112 | + color: Colors.green[400], |
| 113 | + constraints: const BoxConstraints( |
| 114 | + minHeight: 40.0, |
| 115 | + minWidth: 80.0, |
| 116 | + ), |
| 117 | + isSelected: _selectedVegetables, |
| 118 | + children: vegetables, |
| 119 | + ), |
| 120 | + const SizedBox(height: 20), |
| 121 | + // ToggleButtons with icons only. |
| 122 | + Text('Icon-only', style: theme.textTheme.subtitle2), |
| 123 | + const SizedBox(height: 5), |
| 124 | + ToggleButtons( |
| 125 | + direction: vertical ? Axis.vertical : Axis.horizontal, |
| 126 | + onPressed: (int index) { |
| 127 | + setState(() { |
| 128 | + // The button that is tapped is set to true, and the others to false. |
| 129 | + for (int i = 0; i < _selectedWeather.length; i++) { |
| 130 | + _selectedWeather[i] = i == index; |
| 131 | + } |
| 132 | + }); |
| 133 | + }, |
| 134 | + borderRadius: const BorderRadius.all(Radius.circular(8)), |
| 135 | + selectedBorderColor: Colors.blue[700], |
| 136 | + selectedColor: Colors.white, |
| 137 | + fillColor: Colors.blue[200], |
| 138 | + color: Colors.blue[400], |
| 139 | + isSelected: _selectedWeather, |
| 140 | + children: icons, |
| 141 | + ), |
| 142 | + ], |
| 143 | + ), |
| 144 | + ), |
| 145 | + ), |
| 146 | + floatingActionButton: FloatingActionButton.extended( |
| 147 | + onPressed: () { |
| 148 | + setState(() { |
| 149 | + // When the button is pressed, ToggleButtons direction is changed. |
| 150 | + vertical = !vertical; |
| 151 | + }); |
| 152 | + }, |
| 153 | + icon: const Icon(Icons.screen_rotation_outlined), |
| 154 | + label: Text(vertical ? 'Horizontal' : 'Vertical'), |
| 155 | + ), |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
0 commit comments