Reputation: 77
I'm learning flutter but there are some things that I cannot find anywhere.
For example, I want to make a group of ChoiceChips, similar to the picture
but I don't know how to put custom labels in this kind of chips.
How can I make it possible?
import 'package:flutter/material.dart';
class MyThreeOptions extends StatefulWidget {
@override
_MyThreeOptionsState createState() => _MyThreeOptionsState();
}
class _MyThreeOptionsState extends State<MyThreeOptions> {
int _value = 0;
// ----What I want to appear----//
/*void v (int index){
switch (index){
case 0: Text('Phones');
break;
case 1: Text('Computers');
break;
case 2: Text('Accessories');
break;
}}*/
@override
Widget build(BuildContext context) {
return Wrap(
alignment: WrapAlignment.center,
spacing: 12.0,
children: List<Widget>.generate(
3,
(int index) {
return ChoiceChip(
pressElevation: 0.0,
selectedColor: Colors.blue,
backgroundColor: Colors.grey[100],
label: Text("item $index"),
selected: _value == index,
onSelected: (bool selected) {
setState(() {
_value = selected ? index : null;
});
},
);
},
).toList(),
);}
}
Upvotes: 4
Views: 23722
Reputation: 844
int defaultChoiceIndex;
List<String> _choicesList = ['All', 'Pending', 'Accepted'];
@override
void initState() {
// TODO: implement initState
super.initState();
defaultChoiceIndex = 0;
}
Wrap(
spacing: 8,
children: List.generate(_choicesList.length, (index) {
return ChoiceChip(
labelPadding: EdgeInsets.all(2.0),
label: Text(
_choicesList[index],
style: Theme.of(context)
.textTheme
.bodyText2!
.copyWith(color: Colors.white, fontSize: 14),
),
selected: defaultChoiceIndex == index,
selectedColor: Colors.deepPurple,
onSelected: (value) {
setState(() {
defaultChoiceIndex = value ? index : defaultChoiceIndex;
});
},
// backgroundColor: color,
elevation: 1,
padding: EdgeInsets.symmetric(
horizontal: SizeConfig.widthMultiplier * 4),
);
}),
);
Upvotes: 9
Reputation: 61
import 'package:flutter/material.dart';
class ChoiceChips extends StatefulWidget {
final List chipName;
ChoiceChips({Key key, this.chipName}) : super(key: key);
@override
_ChoiceChipsState createState() => _ChoiceChipsState();
}
class _ChoiceChipsState extends State<ChoiceChips> {
String _isSelected = "";
_buildChoiceList() {
List<Widget> choices = List();
widget.chipName.forEach((item) {
choices.add(Container(
child: ChoiceChip(
label: Text(item),
labelStyle: TextStyle(color: Colors.white),
selectedColor: Colors.pinkAccent,
backgroundColor: Colors.deepPurpleAccent,
selected: _isSelected == item,
onSelected: (selected) {
setState(() {
_isSelected = item;
});
},
),
));
});
return choices;
}
@override
Widget build(BuildContext context) {
return Wrap(
spacing: 5.0,
runSpacing: 3.0,
children: _buildChoiceList(),
);
}
}
Upvotes: 3
Reputation: 77
Ok, I have discovered a way, but I think there has to be some more efficient, creating a list of Strings and passing the values, but I can not encode it. If someone knows any more efficient way to carry out this action, their contribution is appreciated.
class MyThreeOptions extends StatefulWidget {
@override
_MyThreeOptionsState createState() => _MyThreeOptionsState();
}
class _MyThreeOptionsState extends State<MyThreeOptions> {
int _value = 0;
@override
Widget build(BuildContext context) {
return Wrap(
alignment: WrapAlignment.center,
spacing: 12.0,
children: <Widget>[
ChoiceChip(
pressElevation: 0.0,
selectedColor: Colors.blue,
backgroundColor: Colors.grey[100],
label: Text("Phones"),
selected: _value == 0,
onSelected: (bool selected) {
setState(() {
_value = selected ? 0 : null;
});
},
),
ChoiceChip(
pressElevation: 0.0,
selectedColor: Colors.blue,
backgroundColor: Colors.grey[100],
label: Text("Computers"),
selected: _value == 1,
onSelected: (bool selected) {
setState(() {
_value = selected ? 1 : null;
});
},
),
ChoiceChip(
pressElevation: 0.0,
selectedColor: Colors.blue,
backgroundColor: Colors.grey[100],
label: Text("Accesories"),
selected: _value == 2,
onSelected: (bool selected) {
setState(() {
_value = selected ? 2 : null;
});
},
),
],
);
}
}
Upvotes: 1
Reputation: 4741
You just need create your own widget with a layout of your desire and put as label of your Chip.
// Your custom widget...
class CustomChipLabel extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Card(
elevation: 4.0,
child: Row(
children: <Widget>[
IconButton(
iconSize: 40.0,
icon: Icon(Icons.person),
onPressed: null),
Text("My Custom Chip", style: TextStyle(
fontSize: 20.0,
),),
],
)
);
}
}
Wrap(
children: <Widget>[
ChoiceChip(
selected: _isSelected
label: CustomChipLabel(), // your custom label widget
),
],
);
Upvotes: 2