setState() 或 markNeedsBuild() 在构建期间调用。我试过WidgetsBinding.instance.addPostFrameCallback,但是我的按钮不起作用


import 'package:customizedapp/reuseable_card.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'icon_content.dart';
import 'constants.dart';
enum Gender {
male,
female,
}
class InputPage extends StatefulWidget {
const InputPage({Key? key}) : super(key: key);
@override
State<InputPage> createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
Gender? selectedGender;
double height = 0.0;
int weight = 0;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text(
'BMI Calculator',
style: TextStyle(fontSize: 20.0),
),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Row(
children: [
Expanded(
child: ReusableCard(
colour: selectedGender == Gender.male
? kActiveReusealeContainerColor
: kInactiveReusealeContainerColor,
containChild: IconContent(FontAwesomeIcons.mars, 'MALE'),
onPress: () {
setState(() {
selectedGender = Gender.male;
});
},
),
),
Expanded(
child: ReusableCard(
colour: selectedGender == Gender.female
? kActiveReusealeContainerColor
: kInactiveReusealeContainerColor,
containChild:
IconContent(FontAwesomeIcons.venus, 'FEMALE'),
onPress: () {
setState(() {
selectedGender = Gender.female;
});
},
),
),
],
),
),
Expanded(
child: ReusableCard(
colour: kActiveReusealeContainerColor,
containChild: Column(
children: [
Text(
'Height',
style: kLabelTextStyle,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Text(
height.toStringAsFixed(2),
style: kNumberDisplayTextStyle,
),
Text(
'ft',
style: kLabelTextStyle,
),
],
),
SliderTheme(
data: SliderThemeData(
activeTrackColor: Colors.white,
inactiveTrackColor: Color(0xff8E8E9C),
thumbShape: RoundSliderThumbShape(
enabledThumbRadius: 12.0, elevation: 5),
overlayShape:
RoundSliderOverlayShape(overlayRadius: 24),
thumbColor: Color(0xffDF4068),
overlayColor: Color(0x29DF4068),
),
child: Slider(
value: height,
min: kMinSliderValue,
max: kMaxSliderValue,
onChanged: (double newValue) {
setState(() {
height = newValue;
});
}),
),
],
),
),
),
Expanded(
child: Row(
children: [
Expanded(
child: ReusableCard(
colour: kActiveReusealeContainerColor,
containChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Weight',
style: kLabelTextStyle,
),
Text(
weight.toString(),
style: kNumberDisplayTextStyle,
),

Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RoundButton(
icon: FontAwesomeIcons.add,
onPressed: () {
setState(() {
weight++;
});
}),
SizedBox(
width: 10,
),
RoundButton(
icon: FontAwesomeIcons.minus,
onPressed: () {
setState(() {
weight--;
});
}),
],

),
],
),
),
),
Expanded(
child: ReusableCard(
colour: kActiveReusealeContainerColor,
containChild: Container(),
),
),
],
),
),
Container(
height: kBottomContainerHeight,
margin: EdgeInsets.only(top: 10),
width: double.infinity,
color: kBottomContainerColor,
),
],
),
),
);
}
}
class RoundButton extends StatelessWidget {
const RoundButton({Key? key, required this.icon, required this.onPressed})
: super(key: key);
final IconData icon;
final Function onPressed;
@override
Widget build(BuildContext context) {
return RawMaterialButton(
onPressed: onPressed(),
child: Icon(icon),
fillColor: kInactiveReusealeContainerColor,
constraints: BoxConstraints(minWidth: 56, minHeight: 56),
shape: CircleBorder());
}
}

"发生此错误">

此InputPage小部件不能标记为需要构建,因为框架已经在构建小部件的过程中。只有当小部件的祖先之一当前正在构建时,才可以在构建阶段将其标记为需要构建。允许出现此异常,因为框架在子窗口小部件之前构建父窗口小部件,这意味着将始终构建脏的子窗口。否则,框架可能不会在这个构建阶段访问这个小部件。调用setState((或markNeedsBuild((的小部件为:InputPage状态:_InputPageState#98c9c发出违规调用时当前正在构建的小部件是:RoundButton脏的

onPressed: onPressed(),替换为onPressed: onPressed,。您喜欢在按下按钮时调用事件,而不是在构建时调用。

相关内容

  • 没有找到相关文章

最新更新