答案 0 :(得分:2)
类似的东西
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text('Title')),
body: Center(
child: RadioChips(
values: ['All', 'Tradition', 'Fiesta'],
onSelected: (index) {
print('selected: $index');
},
),
),
),
);
}
}
class RadioChips extends StatefulWidget {
final List<String> values;
final int initialPosition;
final Function(int index) onSelected;
RadioChips({@required this.values, @required this.onSelected, this.initialPosition = 0});
@override
_RadioChipsState createState() => _RadioChipsState();
}
class _RadioChipsState extends State<RadioChips> {
int current;
@override
void initState() {
super.initState();
current = widget.initialPosition;
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: List.generate(
widget.values.length,
(index) {
return GestureDetector(
onTap: () async {
setState(() => current = index);
widget.onSelected(index);
},
child: Container(
height: 36,
alignment: Alignment.center,
margin: const EdgeInsets.symmetric(horizontal: 4),
padding: const EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(18)),
color: index == current ? Colors.lightBlue : Colors.grey[400],
),
child: Text(widget.values[index], style: TextStyle(color: Colors.white)),
),
);
},
),
);
}
}