我正在尝试使用在文件编号:1中定义的功能到文件编号:2中。我已将文件1导入文件2中,并希望使用文件1类中定义的某些函数。我可以访问类本身,但不能访问变量和函数。我想在受压但不能使用这些功能。
文件1的代码
import 'package:flutter/material.dart';
import 'dart:async';
class TimeDisplay extends StatefulWidget {
@override
_TimeDisplayState createState() => _TimeDisplayState();
}
class _TimeDisplayState extends State<TimeDisplay> {
Stopwatch watch = Stopwatch();
Timer timer;
String elapsedTime= '';
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.only(top: 15.0),
child: Text(
elapsedTime,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 80.0, color: Colors.white),
),
);
}
startWatch(){
watch.start();
setTime();
}
stopWatch(){
watch.stop();
setTime();
}
resetWatch(){
watch.reset();
setTime();
}
setTime(){
var timeSoFar = watch.elapsedMilliseconds;
setState(() {
elapsedTime = transformMilliseconds(timeSoFar);
});
}
transformMilliseconds(int milliseconds) {
int hundreds =(milliseconds / 10).truncate();
int seconds =(hundreds / 100).truncate();
int minutes =(seconds / 60).truncate();
String minutesStr = (minutes % 60).toString().padLeft(2, '0');
String secondsStr = (seconds % 60).toString().padLeft(2, '0');
String hundredsStr = (hundreds % 100).toString().padLeft(2, '0');
return "$minutesStr:$secondsStr";
}
}
文件2的代码
import 'package:flutter/material.dart';
import 'package:getfit/timedisplay.dart';
import 'dart:async';
class ChangingBtn extends StatefulWidget {
@override
_ChangingBtnState createState() => _ChangingBtnState();
}
class _ChangingBtnState extends State<ChangingBtn> {
bool playing = false;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TimeDisplay(),
Padding(
padding: EdgeInsets.only(top: 15.0, bottom: 100.0),
child: RawMaterialButton(
shape: CircleBorder(),
fillColor: Colors.red,
splashColor: Colors.yellowAccent,
highlightColor: Colors.orangeAccent.withOpacity(0.5),
elevation: 10.0,
highlightElevation: 5.0,
onPressed: () {
setState(() {
playing = !playing;
});
playing ? startWatch
: stopWatch;
},
child: Padding(
padding: EdgeInsets.all(25.0),
child: Icon(
playing ? Icons.pause
: Icons.play_arrow,
color: Colors.white,
size: 35.0,
),
),
),
),
],
);
}
}
我想我在这里做错了,我不确定
onPressed: () {
setState(() {
playing = !playing;
});
playing ? startWatch
: stopWatch;
},
如果有人可以解释,我将非常感谢。