amap_map/example/lib/widgets/amap_radio_group.dart

75 lines
1.8 KiB
Dart
Raw Permalink Normal View History

2023-12-22 21:23:24 +08:00
import 'package:amap_map_example/widgets/amap_gridview.dart';
import 'package:flutter/material.dart';
class AMapRadioGroup<T> extends StatefulWidget {
final String? groupLabel;
final T? groupValue;
final Map<String, T>? radioValueMap;
final ValueChanged<T?>? onChanged;
2023-12-22 21:23:24 +08:00
AMapRadioGroup(
2024-08-26 00:00:23 +08:00
{super.key,
2023-12-22 21:23:24 +08:00
this.groupLabel,
this.groupValue,
this.radioValueMap,
2024-08-26 00:00:23 +08:00
this.onChanged});
2023-12-22 21:23:24 +08:00
@override
2024-08-26 09:55:49 +08:00
State<AMapRadioGroup<T>> createState() => _AMapRadioGroupState<T>();
2023-12-22 21:23:24 +08:00
}
class _AMapRadioGroupState<T> extends State<AMapRadioGroup<T>> {
T? _groupValue;
2023-12-22 21:23:24 +08:00
@override
void initState() {
super.initState();
2024-08-25 23:37:44 +08:00
_groupValue = (widget.groupValue);
2023-12-22 21:23:24 +08:00
}
@override
Widget build(BuildContext context) {
List<Widget> radioList = <Widget>[];
2024-08-25 23:37:44 +08:00
_groupValue = (widget.groupValue);
2024-08-26 00:00:23 +08:00
Widget myRadio(String label, dynamic radioValue) {
2023-12-22 21:23:24 +08:00
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(label),
Radio<T>(
2023-12-22 21:23:24 +08:00
value: radioValue,
groupValue: _groupValue,
2024-08-26 00:00:23 +08:00
onChanged: (T? value) {
2023-12-22 21:23:24 +08:00
setState(() {
2024-08-26 00:00:23 +08:00
_groupValue = value;
2023-12-22 21:23:24 +08:00
});
2024-08-26 00:00:23 +08:00
widget.onChanged!(value);
2023-12-22 21:23:24 +08:00
},
),
],
);
}
if (widget.radioValueMap != null) {
widget.radioValueMap!.forEach((key, value) {
2024-08-26 00:00:23 +08:00
radioList.add(myRadio(key, value));
2023-12-22 21:23:24 +08:00
});
}
return Container(
padding: EdgeInsets.all(5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(widget.groupLabel!),
Container(
padding: EdgeInsets.only(left: 10),
child: AMapGradView(
childrenWidgets: radioList,
),
),
],
),
);
}
}