重置孩子的状态小部件

时间:2018-12-06 16:03:37

标签: dart flutter state

以下是我遇到问题的代码摘要:

父部件

class HomePage extends StatefulWidget {
  @override
    State<HomePage> createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  final GlobalKey<AsyncLoaderState> _asyncLoaderState = GlobalKey<AsyncLoaderState>();
  List<DateTime> rounds;
  List<PickupModel> pickups;
  DateTime currentDate;

  Widget build(BuildContext context) {
    var _asyncLoader = AsyncLoader(
      key: _asyncLoaderState,
      initState: () async => await _getData(),
      renderLoad: () => Scaffold(body: Center(child: CircularProgressIndicator())),
      renderError: ([error]) => Text('Sorry, there was an error loading'),
      renderSuccess: ({data}) => _buildScreen(context),
    );

    return _asyncLoader;
  }

  Widget _buildScreen(context) {
    return Scaffold(
      body: PickupList(pickups),
    );
  }


  Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
      context: context,
    );

    if (picked != null && picked != currentDate) {
      currentDate = picked;
      pickups = await api.fetchPickupList(currentDate);
      setState(() {
      });
    }
  }

  _getData() async {
    rounds =  await api.fetchRoundsList();
    currentDate = _getNextRound(rounds);
    pickups = await api.fetchPickupList(currentDate);
  }
}

儿童小工具

(Listview构建图块)

class PickupTile extends StatefulWidget{
  final PickupModel pickup;

  PickupTile(this.pickup);

  @override
    State<StatefulWidget> createState() {
      return PickupTileState();
    }
}

class PickupTileState extends State<PickupTile> {
  Duration duration;
  Timer timer;
  bool _isUnavailable;
  bool _isRunning = false;
  bool _isDone = false;

  @override
    void initState() {
      duration = widget.pickup.duration;
      _isUnavailable = widget.pickup.customerUnavailable;
      super.initState();
    }

  @override
    Widget build(BuildContext context) {
    return  Row(
      children: [
          // UI widgets
      ]
}

因此,我有一个父小部件,其中包含子代PickupTile中显示的代答的初始列表。可以使用_selectDate更改显示取件的日期。这将获取存储在父状态中的拾取的新列表,并且将使用其正确属性重建子代。但是,未重置子级窗口小部件的状态(持续时间,isRunning,isDone ...),因此在更改日期时它们会停留在屏幕上。

如果感觉我缺少明显的东西,但是我不知道如何重置子级状态或创建新的PickupTiles,以便在更改日期时得到新的单独状态。

0 个答案:

没有答案