如何在函数中将数据从函数内部传递到不同的类

作为Flutter的初学者,我正在尝试构建一个日历应用程序,在该应用程序中我想从Firestore数据库中获取特定日期的数据。现在有一个_onDaySelected函数,可在flutter控制台中从日历中打印选定的日期,例如“ flutter:2019-11-04”。我希望将此String传递给MessagesStream类,这样我可以调用.document('$ currentDay')而不是像这样对日期进行硬编码:.document('2019-11-04')。我已包含以下代码。

有人知道怎么做吗?任何帮助将不胜感激!

class RoosterTest extends StatefulWidget {

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

class _RoosterTestState extends State<RoosterTest>
    with TickerProviderStateMixin {
  Map<DateTime,List> _events;
  List _selectedEvents;
  AnimationController _animationController;
  CalendarController _calendarController;
  final messageTextController = TextEditingController();

  String messageText;

  static final now = DateTime.now();
  static final formatter = DateFormat('yyyy-MM-dd');
  static final formatted = formatter.format(now);

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

    final _selectedDay = DateTime.now();
    _events = {};

    _selectedEvents = _events[_selectedDay] ?? [];

    _calendarController = CalendarController();

    _animationController = AnimationController(
      vsync: this,duration: const Duration(milliseconds: 400),);

    _animationController.forward();
  }

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

  void _onDaySelected(DateTime day,List events) {
    String currentDay = formatter.format(day).toString();
    print('$currentDay');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisSize: MainAxisSize.max,children: <Widget>[
          _buildTableCalendar(),const SizedBox(
            height: 8.0,),MessagesStream(),],);
  }

Widget _buildTableCalendar() {
    return TableCalendar(
      calendarController: _calendarController,events: _events,startingDayOfWeek: StartingDayOfWeek.monday,calendarStyle: CalendarStyle(
        selectedColor: Colors.red,todayColor: Colors.blue,markersColor: Colors.green,outsideDaysVisible: false,weekendStyle: TextStyle().copyWith(color: Colors.red),headerStyle: HeaderStyle(
        formatButtonTextStyle:
            TextStyle().copyWith(color: Colors.white,fontSize: 15.0),formatButtonDecoration: BoxDecoration(
          color: Colors.red,borderRadius: BorderRadius.circular(16.0),onDaySelected: _onDaySelected,onVisibleDaysChanged: _onVisibleDaysChanged,);
  }

class MessagesStream extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<Querysnapshot>(
      stream: _firestore
          .collection('days')
          .document('2019-11-04')
          .collection('hours')
          .snapshots(),builder: (context,snapshot) {
        if (!snapshot.hasData)
          return CircularProgressIndicator(
            backgroundColor: Colors.lightBlueaccent,);

        return _buildList(context,snapshot.data.documents);
      },);
  }
cheungdmin19791004 回答:如何在函数中将数据从函数内部传递到不同的类

您可以将_onDaySelected方法的结果存储在状态类的变量中:

class _RoosterTestState extends State<RoosterTest>
    with TickerProviderStateMixin {
    String stringOfSelectedDay='defaultValue';
[...]

别忘了在第一次打开应用程序时应为stringOfSelectedDay提供默认值。尚未调用_onDaySelected时,您的MessagesStream应该返回什么?也许是今天的日期?

因此,在该方法上,您可以像这样存储它:

void _onDaySelected(DateTime day,List events) {
    String currentDay = formatter.format(day).toString();
    setState(() { 
        stringOfSelectedDay = currentDay; 
    });
    print('$currentDay');
  }

日期更改后,必须使用setState来使MessagesStream重建自身。

此后,必须使MessagesStream类具有最终变量,以使其基于该变量构建流,并为其赋予其值的构造函数:

class MessagesStream extends StatelessWidget {
  final String date;
  MessagesStream(this.date); //Constructor
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _firestore
          .collection('days')
          .document(date) // Using the final variable here
          .collection('hours')
          .snapshots(),builder: (context,snapshot) {
        if (!snapshot.hasData)
          return CircularProgressIndicator(
            backgroundColor: Colors.lightBlueAccent,);

        return _buildList(context,snapshot.data.documents);
      },);
  }

由于您使用了setState,因此该小部件将反映对_onDaySelected方法的不同调用。

最后,当您调用要构造的Widget时,相应地传递变量:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisSize: MainAxisSize.max,children: <Widget>[
          _buildTableCalendar(),const SizedBox(
            height: 8.0,),MessagesStream(stringOfSelectedDay),//Using the new constructor you've made.
        ],);
  }
本文链接:https://www.f2er.com/3166193.html

大家都在问