1

如何在颤动时将 CupertinoTimerPicker(timerpicker.dart) 中的数据返回给 showModalBottomSheet(form.dart)?

我想使用 navigator.pop 或 sth 但它不像 ondismissed() 函数?

timerpicker.dart

class TimerModal extends StatefulWidget {
  @override
  _TimerModalState createState() => _TimerModalState();
}

class _TimerModalState extends State<TimerModal> {
  Duration _initialtimer = new Duration();

  @override
  Widget build(BuildContext context) {
    return Container(
        height: MediaQuery.of(context).copyWith().size.height / 3,
        child: SizedBox.expand(
          child: CupertinoTimerPicker(
            mode: CupertinoTimerPickerMode.hm,
            minuteInterval: 1,
            secondInterval: 1,
            initialTimerDuration: _initialtimer,
            onTimerDurationChanged: (Duration changedtimer) {
              setState(() {
                _initialtimer = changedtimer;
              });
            },
          ),
        )
      );
  }
}

表单.dart

  FlatButton _timerPickerModal() {
    return FlatButton(
      color: Colors.white,
      textColor: Colors.grey,
      disabledColor: Colors.grey,
      disabledTextColor: Colors.black,
      // padding: EdgeInsets.all(8.0),
      // splashColor: Colors.blueAccent,
      onPressed: () async {
        final result = await showModalBottomSheet(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.0),
          ),
          backgroundColor: Colors.white,
          context: context,
          builder: (builder) => TimerModal()
        );
        print(result);
        // setState(() {
        //   _newTimer = result;
        // });
      },
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text(
              "Timer: " + _checkTimerText(_newTimer),
              style: TextStyle(fontSize: 20.0),
            ),
          ),
          Icon(Icons.arrow_forward_ios),
        ],
      ),
    );
  }
4

1 回答 1

0

您必须onDateTimeChangedCupertinoDatePicker.

每次选择日期时,都会执行回调。

在这种情况下,您可以添加一个回调TimerModal并将其提供给CupertinoDatePicker. 像这样的东西:

FlatButton _timerPickerModal() {
    return FlatButton(
      color: Colors.white,
      textColor: Colors.grey,
      disabledColor: Colors.grey,
      disabledTextColor: Colors.black,
      onPressed: () async {
        final result = await showModalBottomSheet(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.0),
          ),
          backgroundColor: Colors.white,
          context: context,
          builder: (builder) => TimerModal(
            onDateTimeChanged: (DateTime date) {
              // In date variable you have the new date selected
              // Here you can do anything you need with it
              print(date);
            },
          ),
        );
        print(result);
        // setState(() {
        //   _newTimer = result;
        // });
      },
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text(
              "Timer: " + _checkTimerText(_newTimer),
              style: TextStyle(fontSize: 20.0),
            ),
          ),
          Icon(Icons.arrow_forward_ios),
        ],
      ),
    );
  }
于 2020-01-30T14:04:01.933 回答