如何在另一个类小部件中获取自定义有状态小部件值?

时间:2019-03-01 17:29:34

标签: dart flutter

在My Applicatios的第一页中,用户将选择1到10之间的编号,如果选择了5,则第二页将显示5 GradeScreen小部件,例如,我可以使用以下代码创建小部件,但是我可以。无法获取用户从我的GradeScreen小部件中的下拉按钮小部件中选择的值,

请帮助我从GradeGrade小部件中获取价值。 我是Flutter的新手。 预先感谢。

import 'package:flutter/material.dart';

class CalculateCGPAScreen extends StatelessWidget {
  final int noOfSubjectsCount;

  CalculateCGPAScreen(this.noOfSubjectsCount);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: noOfSubjects(noOfSubjectsCount),
      ),
    );
  }

  List<Widget> noOfSubjects(int subjectCount) {
    List<Widget> list = [];
    for (int i = 1; i <= subjectCount; i++) {
      list.add(GradeScreen("Subject $i", 1));
    }
    list.add(RaisedButton(
      onPressed: () {},
      color: Colors.amber,
      splashColor: Colors.amberAccent,
      child: Text("Calculate CGPA"),
    ));
    return list;
  }
}

class GradeScreen extends StatefulWidget {
  final String subjectName;
  final int gradeSystem;

  GradeScreen(this.subjectName, this.gradeSystem);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _GradeScreenState();
  }
}

class _GradeScreenState extends State<GradeScreen> {
  String selectedGrade = "S";
  List<int> creditList = [1, 2, 3, 4, 5, 6];
  int selectedCredit = 1;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        Text(widget.subjectName),
        DropdownButton(
          items: getGradeList(widget.gradeSystem).map((String loopValue) {
            return DropdownMenuItem(
              child: Text(loopValue),
              value: loopValue,
            );
          }).toList(),
          onChanged: (String value) {
            setState(() {
              selectedGrade = value;
            });
          },
          value: selectedGrade,
        ),
        DropdownButton(
          items: creditList.map((int credit) {
            return DropdownMenuItem(
              child: Text(credit.toString()),
              value: credit,
            );
          }).toList(),
          onChanged: (int value) {
            setState(() {
              selectedCredit = value;
            });
          },
          value: selectedCredit,
        )
      ],
    );
  }

  List<String> getGradeList(int option) {
    List<String> gradeSystem;
    if (option == 1) {
      gradeSystem = ['S', 'A', 'B', 'C', 'D', 'E'];
    } else {
      gradeSystem = ['O', 'A+', 'A', 'B+', 'B'];
    }
    return gradeSystem;
  }
}

3 个答案:

答案 0 :(得分:1)

import 'package:flutter/material.dart';

class CalculateCGPAScreen extends StatelessWidget {
  final int noOfSubjectsCount;

  CalculateCGPAScreen(this.noOfSubjectsCount);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: noOfSubjects(noOfSubjectsCount),
      ),
    );
  }

  List<Widget> noOfSubjects(int subjectCount) {
    List<Widget> list = [];
    for (int i = 1; i <= subjectCount; i++) {
      list.add(GradeScreen("Subject $i", 1));
    }
    list.add(RaisedButton(
      onPressed: () {
        for (var item in list) {
          if (item is GradeScreen) {
            GradeScreen gs = item;
            print(gs.grade);
            print(gs.credit);
          }
        }
      },
      color: Colors.amber,
      splashColor: Colors.amberAccent,
      child: Text("Calculate CGPA"),
    ));
    return list;
  }
}

class GradeScreen extends StatefulWidget {
  final String subjectName;
  final int gradeSystem;
  int _credit;
  String _grade;
  GradeScreen(this.subjectName, this.gradeSystem);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _GradeScreenState();
  }

  set credit(int value) {
    _credit = value;
  }

  get credit => _credit;

  set grade(String value) {
    _grade = value;
  }

  get grade => _grade;
}

class _GradeScreenState extends State<GradeScreen> {
  String selectedGrade = "S";
  List<int> creditList = [1, 2, 3, 4, 5, 6];
  int selectedCredit = 1;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        Text(widget.subjectName),
        DropdownButton(
          items: getGradeList(widget.gradeSystem).map((String loopValue) {
            return DropdownMenuItem(
              child: Text(loopValue),
              value: loopValue,
            );
          }).toList(),
          onChanged: (String value) {
            setState(() {
              selectedGrade = value;
              widget.grade = value;
            });
          },
          value: selectedGrade,
        ),
        DropdownButton(
          items: creditList.map((int credit) {
            return DropdownMenuItem(
              child: Text(credit.toString()),
              value: credit,
            );
          }).toList(),
          onChanged: (int value) {
            setState(() {
              selectedCredit = value;
              widget.credit = value;
            });
          },
          value: selectedCredit,
        )
      ],
    );
  }

  List<String> getGradeList(int option) {
    List<String> gradeSystem;
    if (option == 1) {
      gradeSystem = ['S', 'A', 'B', 'C', 'D', 'E'];
    } else {
      gradeSystem = ['O', 'A+', 'A', 'B+', 'B'];
    }
    return gradeSystem;
  }
}

更改下拉值,并将结果输入调试控制台。

答案 1 :(得分:0)

您想在哪里获得下拉值?

答案 2 :(得分:0)

$sql = "
SELECT 
    `cuname` AS `name`, 
    `date`,
     count(`date`) as `datecount`
FROM 
    $table_name
GROUP BY 
    `name`, `date`
";


$query = $wpdb->get_results($sql);

$res = array();
$i = 0;
$name = null;
foreach ($query as $row) {
    if ($name!=$row->name && $name != null) {
        $i++;
    }
    $res[$i][$row->name] = $row->name;
    $res[$i]['dates'][$row->date] = $row->datecount;

    $name = $row->name;
}

//Verify the array structure:

echo "<pre>";
print_r($res);
echo "</pre>";

//Build JSON:
$json = json_encode($res);