父类忽略孩子的状态(颤振)

时间:2020-07-06 13:11:35

标签: flutter dart

如何在Flutter应用程序中从孩子那里取回值?我正在将数据从父级传输到子级小部件。在那里,我修改了这些数据并将其保存在setState中。不幸的是,父窗口小部件什么也没注意到。

父母:

class HomeReportList extends StatefulWidget {
  final ScrollController scrollController;

  HomeReportList(this.scrollController);

  @override
  _HomeReportListState createState() => _HomeReportListState();
}

class _HomeReportListState extends State<HomeReportList> {
  Report newDataValue =
      Report(minutes: 0, videos: 0, returnVisits: 0, placements: 0, studies: 0);

  void saveNewData(data) {
    print(data.placements);
  }

  @override
  Widget build(BuildContext context) {
    final minutesData = Provider.of<Minutes>(context);
    final placementsData = Provider.of<Placements>(context);
    final returnVisitsData = Provider.of<ReturnVisits>(context);
    final videosData = Provider.of<Videos>(context);
    final studiesData = Provider.of<Studies>(context);

    return Container(
      child: Padding(
        padding: const EdgeInsets.only(top: 15),
        child: CupertinoScaffold(
          body: ListView(
            physics: ClampingScrollPhysics(),
            controller: widget.scrollController,
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container(
                      margin: EdgeInsets.only(
                          left: 15, right: 15, bottom: 25, top: 20),
                      child: Text("Daten hinzufügen",
                          style: Theme.of(context)
                              .textTheme
                              .headline3
                              .copyWith(color: Colors.white))),
                  CupertinoButton(
                      child: Icon(Icons.check),
                      onPressed: () => saveNewData(newDataValue))
                ],
              ),
              AddReportDataTile(
                  title: "Stunden",
                  summary: newDataValue.minutes,
                  type: "minutes"),
              AddReportDataTile(
                  title: "Abgaben",
                  summary: newDataValue.placements,
                  type: "placements"),
              AddReportDataTile(
                  title: "Rückbesuche",
                  summary: newDataValue.returnVisits,
                  type: "returnVisits"),
              AddReportDataTile(
                  title: "Studien",
                  summary: newDataValue.studies,
                  type: "studies"),
              AddReportDataTile(
                  title: "Videos",
                  summary: newDataValue.videos,
                  type: "videos"),
            ],
          ),
        ),
      ),
    );
  }
}

孩子:

class _AddReportDataTileState extends State<AddReportDataTile> {
  int summary;
  @override
  void initState() {
    summary = widget.summary;
  }

  Widget build(BuildContext context) {
    final minutesData = Provider.of<Minutes>(context);
    final placementsData = Provider.of<Placements>(context);
    final returnVisitsData = Provider.of<ReturnVisits>(context);
    final videosData = Provider.of<Videos>(context);
    final studiesData = Provider.of<Studies>(context);

    void addValue() {
      setState(() {
        summary++;
      });
    }

    void subtractValue() {
      setState(() {
        summary--;
      });
    }

    return Card(
      color: Colors.white10,
      margin: EdgeInsets.only(left: 15, right: 15, bottom: 10),
      clipBehavior: Clip.antiAlias,
      elevation: .2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.0),
      ),
      child: ListTile(
        leading: IconButton(
            icon: Icon(Icons.remove),
            color: Colors.white,
            visualDensity: VisualDensity.adaptivePlatformDensity,
            onPressed: subtractValue),
        trailing: IconButton(
            color: Colors.white, icon: Icon(Icons.add), onPressed: addValue),
        title: Column(
          children: <Widget>[
            Text(widget.title,
                textAlign: TextAlign.center,
                style: Theme.of(context)
                    .textTheme
                    .bodyText2
                    .copyWith(color: Colors.white)),
            Text(summary.toString(),
                textAlign: TextAlign.center,
                style: Theme.of(context)
                    .textTheme
                    .bodyText1
                    .copyWith(color: Colors.white)),
          ],
        ),
        contentPadding: EdgeInsets.all(10),
      ),
    );
  }
}

函数saveNewData中的数据始终具有初始值。在AddReportDataTile中,我更改了该值,但是我的父类不知道新值。可能是什么问题?

0 个答案:

没有答案