im new in flutter. so this is my case :
I want to rebuild my apps form Android Studio to Flutter, and im facing some problem in flutter.
In Android Studio there is methode called radio.setOnCheckedListener so when i tried to change value from radioButton my spinner(dropdown) automatically reset to null, and i didnt know how to implement automatic reset dropdownmenuitem in flutter when i change value of RadioListTile.
can u guys help me ?
here is my code :
class AnakScreen extends StatefulWidget {
AnakScreen({Key key}) : super(key: key);
@override
_AnakScreenState createState() => _AnakScreenState();
}
class _AnakScreenState extends State<AnakScreen>
with AutomaticKeepAliveClientMixin {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
bool _autoValidate = false;
String nama;
String usia;
int kelaminValue = -1;
int caraValue = -1;
static const String _baseUrl = 'http://192.168.64.2/gizi/api';
int i;
List data = List();
List caraDefault = ['Pilih cara ukur dahulu'];
Future<Usia> getUsia24() async {
final url = '$_baseUrl/antro/usia24';
final response = await http.get(url);
if (response.statusCode == 200) {
final jsonResponse = json.decode(response.body);
Usia usia = Usia.fromJson(jsonResponse);
if (usia.status) {
for (i = 0; i < usia.data.length; i++) {
setState(() {
data = usia.data.toList();
});
print(usia.data[i].usia);
}
}
}
return null;
}
Future<Usia> getUsia60() async {
final url = '$_baseUrl/antro/usia60';
final response = await http.get(url);
if (response.statusCode == 200) {
final jsonResponse = json.decode(response.body);
Usia usia = Usia.fromJson(jsonResponse);
if (usia.status) {
for (i = 0; i < usia.data.length; i++) {
setState(() {
data = usia.data.toList();
});
print(usia.data[i].usia);
}
}
}
return null;
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
super.build(context);
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: Theme.of(context).primaryColor,
shadowColor: Colors.transparent,
title: Text(
'Hitung Gizi Anak',
style: TextStyle(
color: Theme.of(context).primaryColorDark,
fontSize: 25,
fontWeight: FontWeight.bold,
letterSpacing: 2,
),
),
centerTitle: true,
),
body: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(70), topRight: Radius.circular(70)),
color: Colors.white,
),
height: double.infinity,
width: double.infinity,
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(70), topRight: Radius.circular(70)),
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
child: Form(
key: _formKey,
autovalidate: _autoValidate,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(
left: 10, right: 10, bottom: 5),
child: Text('Nama Anak'),
),
TextFormField(
onSaved: (String value) {
nama = value;
},
validator: _validateNama,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 15.0),
fillColor: Colors.white,
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
),
hintText: 'Isi nama anak',
prefixIcon: Icon(
Icons.account_circle_rounded,
color: Theme.of(context).primaryColorDark,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.only(
left: 10, right: 10, bottom: 5, top: 20),
child: Text('Jenis kelamin :'),
),
],
),
Expanded(
child: Column(
children: [
RadioListTile(
title: Text('Laki-laki'),
value: 0,
groupValue: kelaminValue,
onChanged: handleRadioValueChange,
activeColor:
Theme.of(context).primaryColorDark,
),
RadioListTile(
title: Text('Perempuan'),
value: 1,
groupValue: kelaminValue,
onChanged: handleRadioValueChange,
activeColor:
Theme.of(context).primaryColorDark,
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.only(
left: 10, right: 10, bottom: 5, top: 20),
child: Text('Cara ukur :'),
),
],
),
Expanded(
child: Column(
children: [
RadioListTile(
title: Text('Terlentang (0-24 bulan)'),
value: 0,
groupValue: caraValue,
onChanged: handleCaraValueChange,
activeColor:
Theme.of(context).primaryColorDark,
),
RadioListTile(
title: Text('Berdiri (24-60 bulan'),
value: 1,
groupValue: caraValue,
onChanged: handleCaraValueChange,
activeColor:
Theme.of(context).primaryColorDark,
),
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(
left: 10, right: 10, bottom: 5, top: 20),
child: Text('Usia anak (bulan)'),
),
DropdownButtonFormField(
hint: Text('Pilih usia anak'),
decoration: InputDecoration(
contentPadding:
EdgeInsets.symmetric(vertical: 15.0),
fillColor: Colors.white,
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
),
prefixIcon: Icon(
Icons.account_circle_rounded,
color: Theme.of(context).primaryColorDark,
),
),
items: data.map((item) {
return new DropdownMenuItem(
child: Text(item.usia),
value: item.usia,
);
}).toList(),
onChanged: (value) {},
validator: (value) =>
value == null ? 'Usia anak masih kosong' : null),
SizedBox(height: 30),
Container(
width: double.infinity,
child: RaisedButton(
padding: EdgeInsets.all(15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
onPressed: _validateInputs,
child: new Text('Cek hasil'),
textColor: Colors.white,
color: Theme.of(context).primaryColorDark,
),
),
],
),
),
),
),
),
),
),
);
}
String _validateNama(String value) {
if (value.isEmpty) {
return 'Nama bayi harus diisi';
} else {
return null;
}
}
void ha