【问题标题】:Call a void function from another file从另一个文件调用 void 函数
【发布时间】:2020-10-23 17:19:28
【问题描述】:

我正在创建一个具有开始按钮(StartButton 类)的应用程序,并且我希望该按钮调用另一个类(NewRide 类)的方法,但是如何从另一个文件调用 void 函数?

class StartButton extends StatefulWidget{
 @override
  _StartButtonState createState() => _StartButtonState();
}
class _StartButtonState extends State<StartButton> {
  String _driverState = 'offline';

  @override
  Widget build(context){
    return FlatButton(
      child: Text('Start'),
      onPressed: (){
        setState(() {
          if (_driverState == 'offline') {
            _driverState = 'online';
          } else {
            _driverState = 'offline';
          }
        });
      },
    );
  }
}

这是 New Ride 课程,它具有我想在按下开始按钮时调用的 void 函数。

class NewRide extends StatefulWidget {
  @override
  _NewRideState createState() => _NewRideState();
}

class _NewRideState extends State<NewRide> {
  int _counter = 60;
  Timer _timer;

  @override
  Widget build(BuildContext context) {
    void startTimer() {
      _counter = 60;
      if (_timer != null) {
        _timer.cancel();
      }
      _timer = Timer.periodic(Duration(seconds: 1), (timer) {
        setState(() {
          if (_counter > 0) {
            _counter--;
          } else {
            _timer.cancel();
          }
        });
      });
    }

    _showNewRideAlert() {
      if (_counter == 0) {
        return Text('Counter == 0');
      } else {
        return Text('Counter != 0');
      }
    }

    return _showNewRideAlert();
  }
}

【问题讨论】:

  • 这是为了实验目的吗?如果不是,如果我必须在两个小部件之间共享任何状态/功能,我会使用提供者之类的东西。
  • 是的,我想学习如何从另一个类中访问某个类中的某些内容。
  • startTimer 被声明为build 方法中的本地函数。不可能从另一个文件或真正从 build 方法之外的任何地方调用它。

标签: flutter dart timer


【解决方案1】:

使用 GlobalKey 并公开 startTimer 并将 NewRideState 更改为 _NewRideState

class StartButton extends StatefulWidget {
  @override
  _StartButtonState createState() => _StartButtonState();
}

class _StartButtonState extends State<StartButton> {
  String _driverState = 'offline';
  GlobalKey<NewRideState> newRideKey = GlobalKey<NewRideState>();
  @override
  Widget build(context) {
    return Column(
      children: [
        NewRide(key: newRideKey),
        FlatButton(
          child: Text('Start'),
          onPressed: () {
            setState(() {
              if (_driverState == 'offline') {
                _driverState = 'online';
              } else {
                _driverState = 'offline';
              }
            });
          },
        ),
      ],
    );
  }
}



class NewRide extends StatefulWidget {
  const NewRide({Key key}) : super(key: key);
  @override
  NewRideState createState() => NewRideState();
}

class NewRideState extends State<NewRide> {
  int _counter = 60;
  Timer _timer;

  @override
  Widget build(BuildContext context) {
    _showNewRideAlert() {
      if (_counter == 0) {
        return Text('Counter == 0');
      } else {
        return Text('Counter != 0');
      }
    }

    return _showNewRideAlert();
  }

  void startTimer() {
    _counter = 60;
    if (_timer != null) {
      _timer.cancel();
    }
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {
        if (_counter > 0) {
          _counter--;
        } else {
          _timer.cancel();
        }
      });
    });
  }
}

【讨论】:

    【解决方案2】:

    首先将另一个文件/类导入主类(我不确定你的文件叫什么)。

    import '.../newRide.dart' as newRide;

    然后像这样调用函数:

    newRide._showNewRideAlert();
    

    由于类/状态的设置方式,它可能无法正常工作(不会构建小部件)。所以你可能不得不将代码移动到一个可调用的地方,比如类的顶部或者将函数实现到使用它的页面中。

    【讨论】:

    • 谢谢!但是我想打电话给void startTimer(),所以我尝试了newRide.startTimer();但没有用。它说函数'startTimer'没有定义。
    • 如果函数直接在类中,它会工作,但它在扩展类中,这就是为什么它不起作用,你可以自定义类。
    猜你喜欢
    • 2021-04-12
    • 2014-11-13
    • 2022-10-04
    • 2013-12-17
    • 2023-01-28
    • 2021-08-30
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多