1/ 我对这段代码有问题,但 3 天后我仍然不明白为什么。 请参阅图片以更好地了解软件给我的错误(错误使用 ParentDataWidget 错误)。 Error code in Android Studio program
2/ 然后我有另一个关于初始化的问题:到 void Initstate 中,变量visualedText 不能用函数 getTranslated 翻译(它不接受上下文)。出于什么原因?
class WarmUpPage extends StatefulWidget {
WarmUpPage({Key key}) : super(key: key);
@override
_WarmUpPageState createState() => _WarmUpPageState();
}
class _WarmUpPageState extends State<WarmUpPage> {
int counter = 8;
dynamic visualizedText; //text to be visualized above button
Image imgButton;
Image imgButtonUp = Image.asset("lib/images/unpressed.png");
Image imgButtonDown = Image.asset("lib/images/pressed.png");
//Inizialize the state of the picture into imgButtonUp
void initState() {
super.initState();
imgButton = imgButtonUp;
visualizedText ='Warm Up'; //initial text but I cannot translate it because context gives error
}
//Function to increment the counter
void _decrementCounter() {
setState(() {
counter--;
if (counter == 0) {
randompagegenerator(context);
} else if (counter == 99) {
visualizedText = getTranslated(context, 'warmup1');
} else if (counter == 70) {
visualizedText = getTranslated(context, 'warmup2');
} else if (counter == 40) {
visualizedText = getTranslated(context, 'warmup3');
} else if (counter == 20) {
visualizedText = getTranslated(context, 'warmup4');
} else if (counter == 10) {
visualizedText = getTranslated(context, 'warmup5');
} else if (counter == 5) {
visualizedText = getTranslated(context, 'warmup6');
} else if (counter == 1) {
visualizedText = getTranslated(context, 'warmup7');
} else {}
});
}
@override
Widget build(BuildContext context) {
TextStyle _textStyleText = TextStyle(
color: Colors.white,
fontSize: 40,
);
TextStyle _textStyleNumber = TextStyle(
color: Colors.white,
fontSize: 70,
);
return Scaffold(
//backgroundColor: Colors.white,
appBar: ReusableBar(),
body: SafeArea(
child: Stack(
alignment: Alignment.topCenter,
children: [
Container(
width: MediaQuery.of(context).size.width / 1,
height: MediaQuery.of(context).size.height / 1,
padding: EdgeInsets.all(100),
alignment: Alignment.center,
//color: Colors.grey[850],
//color: Theme.of(context).colorScheme.background,
child: Center(
child: Positioned(
width: MediaQuery.of(context).size.width / 1,
child: GestureDetector(
child: imgButton,
onTap:() {
_decrementCounter();
incrementAppBarCounter();
},
onTapDown: (tap) {
setState(() {
imgButton = imgButtonDown;
});
},
onTapUp: (tap) {
setState(() {
imgButton = imgButtonUp;
});
},
),
),
),
),
Positioned(
top: 0,
width: MediaQuery.of(context).size.width * 0.7,
height: MediaQuery.of(context).size.height * 0.28,
child: Container(
//width: MediaQuery.of(context).size.width / 1,
//height: MediaQuery.of(context).size.height * 0.25,
padding: EdgeInsets.all(8),
alignment: Alignment.bottomCenter,
//color:Colors.grey[500],
child: Text(
visualizedText,
style: _textStyleText,
textAlign: TextAlign.center,
),
),
),
Positioned(
width: MediaQuery.of(context).size.width / 1,
top: MediaQuery.of(context).size.height * 0.58,
child: Container(
width: MediaQuery.of(context).size.width / 1,
//height: MediaQuery.of(context).size.height *0.2,
padding: EdgeInsets.all(1),
alignment: Alignment.topCenter,
//color: Colors.grey[200],
child: Text('$counter', style: _textStyleNumber),
),
),
Positioned(
bottom: 0,
width: MediaQuery.of(context).size.width / 1,
child: Container(
height: MediaQuery.of(context).size.height / 15,
padding: EdgeInsets.all(1),
alignment: Alignment.center,
),
),
],
),
),
);
}
}
答案 0 :(得分:1)
定位小部件必须是堆栈的后代,并且从定位小部件到其封闭 Stack
的路径必须仅包含 StatelessWidgets
或 StatefulWidgets
,显然您正在包装一个Positioned()
带有 Container()
和 Center()