【问题标题】:how to use another class property in flutter/dart如何在颤振/飞镖中使用另一个类属性
【发布时间】:2021-01-28 14:09:52
【问题描述】:

我有一个日历,我想在另一个类中使用它的日期属性,我已经尝试过创建对象,但这不起作用 这是我尝试实例化的方法

var myCalender = MyCalendar();

在课堂上我想说一些类似的话

Text('$myCalendar.date');

这是我的日历课

    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,
        ));
      }
    }

这是我的 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'),),
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您实际上是在尝试访问 MyCalendar 的 state 属性,而不是 MyCalendar 本身的属性。要公开状态,您可以参考这个答案how to access an object created in one stateful widget in another stateful widget in 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,
            ));
      }
    }
    

    注意我已经删除了日期前面的静态,因为您可能想要更改日期并且静态成员无法通过 currentState 访问。如果您不需要 date 是 DateTime.now() 以外的任何内容,那么您可能不需要访问小部件状态,并且可以直接从其他小部件引用 DateTime.now()。

    【讨论】:

    • 我要在哪里定义全局键?
    • 在您的 main.dart 文件中导入之后和 void main(){} 之前。
    • 好的,当我试图定义它时,它说 MyCalendar 没有扩展“State”。尝试使用一个类型,它是 'State 为什么会出现?
    • 我认为您将密钥定义为“GlobalKey()”,而它应该是“GlobalKey<_mycalendarstate>()”
    • 我更改了它,但它无法识别它虽然我导入了位置但说the name '_MyCalendarState' isn't a type so it can't be used as a type argument. Try correcting the name to an existing type, or defining a type named '_MyCalendarState 但是当我将它更改为`GlobalKey>` 但是当我实现时我不能识别类属性并且不显示属性
    【解决方案2】:
    class MyCalendar extends StatefulWidget {
     DateTime date = DateTime.now();
          @override
          _MyCalendarState createState() => _MyCalendarState();
        }
    

    _MyCalendarState 中使用widget.date

    【讨论】:

      猜你喜欢
      • 2018-09-10
      • 2020-01-28
      • 2020-11-28
      • 2021-03-31
      • 1970-01-01
      • 2021-08-15
      • 2020-08-12
      • 2020-11-05
      • 2020-02-29
      相关资源
      最近更新 更多