从小部件外部读取状态

时间:2018-08-06 00:36:40

标签: flutter

我很难弄清楚如何从小部件外部读取小部件状态下的变量值。简而言之,我需要从RatableListItem外部访问rating变量。我想在另一个小部件中迭代一组RatableListItem,以收集评级。

我该如何访问每个Ra​​tableListItem的状态以获取评分变量?

以下是该代码的精简版本:

class RatableListItem extends StatefulWidget {

  @override
  State<StatefulWidget> createState() => new RatableEmotionListItemState();

}

class RatableListItemState extends State<RatableListItem> {

   String _title = "Test";

   var _ratings = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];

   String _rating = '0';

   @override
  Widget build(BuildContext context) {
    return new ListTile(
    leading: new Text(_title),
    trailing: new DropdownButton<String>(

      items: _ratings.map((String rating) {
        return new DropdownMenuItem<String>(value: rating, child: new Text(rating));}).toList(),

    value: _rating,

    onChanged: (String value) {
        setState(() {
          _rating = value;
        });
    },

   )
);
}

String getRating() {
  return _rating;
}

@override
void initState() {
  super.initState();
}

@override
void dispose() {
  super.dispose();
}

}

1 个答案:

答案 0 :(得分:0)

只需传递这样的参数函数:

        class _TestingState extends State<Testing> {
          List<int> values = [0,0,0,0,0,0];

          _getData() {
            //here you have your data
            values.forEach((item) => print("value: $item"));
          }

          _rateByIndex(int index, int value) {
            values[index] = value;
          }

          @override
          Widget build(BuildContext context) {
            return new MaterialApp(
                title: 'Flutter Demo',
                theme: new ThemeData(
                  primarySwatch: Colors.blue,
                ),
                home: Material(
                  child: Column(
                    children: <Widget>[
                      ListView.builder(
                        shrinkWrap: true,
                        itemCount: values.length,
                        itemBuilder: (context, index) {
                          return RatableListItem(rateByIndex: _rateByIndex, index: index);
                        },
                      ),
                      MaterialButton(
                        child: Text("ACTION"),
                        onPressed: _getData,
                      )
                    ],
                  ),
                ));
          }
        }

        typedef void RateByIndex(int index, int value);

        class RatableListItem extends StatefulWidget {
          final RateByIndex rateByIndex;
          final int index;

          RatableListItem({this.index,this.rateByIndex});

          @override
          State<StatefulWidget> createState() => new RatableListItemState();
        }

        class RatableListItemState extends State<RatableListItem> {
          String _title = "Test";

          var _ratings = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];

          String _rating = '0';

          @override
          Widget build(BuildContext context) {
            return new ListTile(
                leading: new Text(_title),
                trailing: new DropdownButton<String>(
                  items: _ratings.map((String rating) {
                    return new DropdownMenuItem<String>(
                        value: rating, child: new Text(rating));
                  }).toList(),
                  value: _rating,
                  onChanged: (String value) {
                    setState(() {
                      _rating = value;
                    });
                    widget.rateByIndex(widget.index, int.parse(value));
                  },
                ));
          }

          String getRating() {
            return _rating;
          }

          @override
          void initState() {
            super.initState();
          }

          @override
          void dispose() {
            super.dispose();
          }
        }

我正在初始化全局声明中的值List,您可以对其进行修改。 当您按下ACTION按钮时,您将获得所有值。

参数函数将接收项目的索引以及每个项目的值。 您将使用“列表”值存储项目中的所有值。