Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

rest - Flutter : How to reset DropdownMenuItem value on RadioListTile change

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...