简体   繁体   English

如何在 flutter/dart 中使用另一个类属性

[英]how to use another class property in flutter/dart

i have a calendar and i want to use its date property in another class i already tried to make object but that does not work here is what i tried for instantiate我有一个日历,我想在另一个类中使用它的日期属性,我已经尝试创建对象,但这在这里不起作用是我尝试实例化的

var myCalender = MyCalendar();

and in the class i want to say something like在课堂上我想说一些类似的话

Text('$myCalendar.date');

and here is my calendar class这是我的日历课

    class MyCalendar extends StatefulWidget {
      @override
      _MyCalendarState createState() => _MyCalendarState();
    }
    
    class _MyCalendarState extends State<MyCalendar> {
      static DateTime date = DateTime.now();
      TimeOfDay timeOfday = TimeOfDay.now();
    
      Future<Null> selectDate(BuildContext context) async {
        final DateTime picked = await showDatePicker(
   //show date picker Entrances i did not copy those to make the code shorter for you :)
        if (picked != null && picked != date) {
          print('date is ${date.toString()}');
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
            child: FlatButton(
          onPressed: () {selectDate(context);},
          child: Text('calendar'),
          color: Colors.blue,
        ));
      }
    }

and here is my main.dart for global key这是我的全局键的 main.dart

void main() {
  runApp(MyApp());
}
final key = GlobalKey<State<CustomCalendar>>();
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(child: Text('data'),),
    );
  }
}

You are actually trying to access a property of the state of MyCalendar rather than of MyCalendar itself.您实际上是在尝试访问 MyCalendar状态的属性,而不是 MyCalendar 本身的属性。 To expose the state you can refer to this answer how to access an object created in one stateful widget in another stateful widget in flutter and implement it like this for example:要公开状态,您可以参考此答案如何访问在 flutter 中的另一个有状态小部件中的一个有状态小部件中创建的对象,并像这样实现它,例如:

final key = new GlobalKey<_MyCalendarState>();

void test(){
  print(key.currentState.date.toString());
}

class MyCalendar extends StatefulWidget {
  MyCalendar({Key key}) : super(key: key);

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

class _MyCalendarState extends State<MyCalendar> {
  DateTime date = DateTime.now();
  TimeOfDay timeOfday = TimeOfDay.now();

  Future<Null> selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
      //show date picker Entrances i did not copy those to make the code shorter for you :)
    if (picked != null && picked != date) {
      print('date is ${date.toString()}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: FlatButton(
          onPressed: () {selectDate(context);},
          child: Text('calendar'),
          color: Colors.blue,
        ));
  }
}

Note I've removed static in front of date as presumably you will want to change the date and static members are not accessible through currentState.注意我已经删除了日期前面的静态,因为您可能想要更改日期,并且无法通过 currentState 访问静态成员。 If you don't need date to ever be anything other than DateTime.now() then you probably don't need to access the widget state and can just refer to DateTime.now() directly from the other widget.如果您不需要 DateTime.now() 以外的任何日期,那么您可能不需要访问小部件状态,只需直接从其他小部件引用 DateTime.now() 即可。

class MyCalendar extends StatefulWidget {
 DateTime date = DateTime.now();
      @override
      _MyCalendarState createState() => _MyCalendarState();
    }

and in _MyCalendarState use widget.date并在_MyCalendarState使用widget.date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM