Flutter:在其他有状态小部件中共享日历状态

时间:2020-06-14 20:47:57

标签: flutter dart calendar stateful

我试图在flutter中使用Included Calendar窗口小部件,并且我做了所有正确的代码,但是我需要将其用作模板来共享其他有状态窗口小部件中的数据,因为当我尝试从其他状态调用它时遇到了此错误有状态的小部件

“ setState()在构造函数中调用:DateTime State#be478(lifecycle 状态:已创建,没有窗口小部件,未安装)“

这是完整的代码,我将包括在

中调用它的函数
import 'package:fixtry/shared/const.dart';
import 'package:flutter/material.dart';
import 'dart:async';

class DateTimeDialoge extends StatefulWidget {


 onDateSelected(context) =>createState()._onDateSelected(context);
 onTimeSelected(context) =>createState()._onTimeSelected(context);
 DateTime date = DateTime.now();
 TimeOfDay time = TimeOfDay.now();
DateTimeDialoge({this.date,this.time});
  @override
  _DateTimeDialogeState createState() => _DateTimeDialogeState();
}

class _DateTimeDialogeState extends State<DateTimeDialoge> {

  DateTime date = DateTime.now();
  TimeOfDay time = TimeOfDay.now();

  Future<Null> _onDateSelected(BuildContext context) async {
    final DateTime picked = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(1980),
      lastDate: DateTime(2050),
      builder: (BuildContext context, Widget child) {
        return Theme(
          data: ThemeData.light().copyWith(
            colorScheme: ColorScheme.light(
                primary: mOrange, secondary: mLightColor),
            primaryColor: mOrange,
            accentColor: mLightColor,
            buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.primary),

            //dialogBackgroundColor:mOffWhite,
            backgroundColor: Colors.white,
          ),
          child: child,
        );
      },
    );
    if (picked != null && picked != date) {
      setState(() {
        date = picked;
      });
    }
  }

  Future<Null> _onTimeSelected(BuildContext context) async {
    final TimeOfDay picked =
    await showTimePicker(context: context,
      initialTime: time,
      builder: (BuildContext context, Widget child) {
        return Theme(
          data: ThemeData.light().copyWith(
            colorScheme: ColorScheme.light(
                primary: mOrange, secondary: mLightColor),
            primaryColor: mOrange,
            accentColor: mLightColor,
            buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.primary),

            //dialogBackgroundColor:mOffWhite,
            backgroundColor: Colors.white,
          ),
          child: child,
        );
      },);

    if (picked != null && picked != time) {
      setState(() {
        time = picked;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('test'),
      ),
      body: Center(
        child: Column(
          children: [
            FlatButton(
              child: Text(
                '${date.toString()}',
              ),
              onPressed: () {
                _onDateSelected(context){
                  print(date);
                }
              },
            ),
            FlatButton(
              child: Text(
                '${time.toString()}',
              ),
              onPressed: () {
                _onTimeSelected(context);
              },
            ),
          ],
        ),
      ),
    );
  }
}


这是我要调用的另一个有状态小部件

import 'package:flutter/material.dart';

import 'package:fixtry/shared/date_time.dart';

class Form extends StatefulWidget {
  @override
  _FormState createState() => _FormState();
}

class _FormState extends State<Form> {
  @override
  Widget build(BuildContext context) {
    return Container(child: FlatButton.icon(onPressed: (){}, icon: Icon(Icons.calendar_today), label: Text('Pick Day'),),);
  }
}

0 个答案:

没有答案