gpt4 book ai didi

flutter - 不能引用setState()flutter

转载 作者:行者123 更新时间:2023-12-03 03:55:26 25 4
gpt4 key购买 nike

我在vscode上编码此应用程序。当我尝试在createButton()函数(位于代码btw的末尾)中使用setState()时,它在下面显示一条蓝色的波浪线,表示“未引用声明'setState'。”如何使用setState()使其引用由createButton()函数创建的按钮。感谢您的帮助:)

import 'package:flutter/material.dart';

void main() => runApp(MainScreen());

class MainScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[

/* -------------------------------------------------------------------------- */
/* Display section */
/* -------------------------------------------------------------------------- */

Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text('Hello World', style: TextStyle(fontSize: 50)),
SizedBox(height: 20),
Text('I\'m small', style: TextStyle(fontSize: 20)),
Container(width: double.infinity, height: 200),
],
),

/* -------------------------------------------------------------------------- */
/* Button section */
/* -------------------------------------------------------------------------- */

Expanded(
child: Row(
children: <Widget>[

/* -------------------------------- Column 1 -------------------------------- */

createButtonColumn(buttonAmount: 4, buttonContents: [
Text('+', style: TextStyle(fontSize: 25, color: Colors.black54)),
Text('8', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('4', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('0', style: TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [Colors.redAccent, Colors.black, Colors.black, Colors.black],

),

/* -------------------------------- Column 2 -------------------------------- */

createButtonColumn(buttonAmount: 4, buttonContents: [
Text('-', style: TextStyle(fontSize: 35, color: Colors.black54)),
Text('9', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('5', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('1', style: TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [Colors.redAccent, Colors.black, Colors.black, Colors.black],
),

/* -------------------------------- Column 3 -------------------------------- */

createButtonColumn(buttonAmount: 4, buttonContents: [
Text('*', style: TextStyle(fontSize: 25, color: Colors.black54)),
Text('<--', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('6', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('2', style: TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [Colors.redAccent, Colors.black, Colors.black, Colors.black],
),

/* -------------------------------- Column 4 -------------------------------- */

createButtonColumn(buttonAmount: 4, buttonContents: [
Text('/', style: TextStyle(fontSize: 25, color: Colors.black54)),
Text('AC', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('7', style: TextStyle(fontSize: 25, color: Colors.white54)),
Text('3', style: TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [Colors.redAccent, Colors.black, Colors.black, Colors.black],
),

/* -------------------------------- Column 5 -------------------------------- */

createButtonColumn(buttonAmount: 4, buttonContents: [
Text('=', style: TextStyle(fontSize: 25, color: Colors.black54)),
Text('(', style: TextStyle(fontSize: 25, color: Colors.black54)),
Text(')', style: TextStyle(fontSize: 25, color: Colors.black54)),
Text('.', style: TextStyle(fontSize: 35, color: Colors.black54))
],
buttonColors: [Colors.yellow[300], Colors.redAccent, Colors.redAccent, Colors.redAccent],
)
],
),
)
],
)
)
)
);
}
}


/* -------------------------------------------------------------------------- */
/* Custom functions */
/* -------------------------------------------------------------------------- */

createButtonColumn({ int buttonAmount, List<Widget> buttonContents, bool buttonContentsAreSame = false, List<Color> buttonColors, bool buttonColorsAreSame = false, List<String> actionList }) {
List<Widget> buttonList = [];

if (!buttonContentsAreSame || !buttonColorsAreSame) {
for (int i = 0; i < buttonContents.length; i++) {
buttonList.add(createButton(content: buttonContents[i], color: buttonColors[i], action: actionList[i]));
}
} else {
for (int x = 1; x <= buttonAmount; x++) {
buttonList.add(createButton(content: buttonContents[0]));
}
}

return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: buttonList
),
);
}

createButton({ int flex = 1, Widget content, Color color, String action }) {
return Expanded(
child: RaisedButton(
onPressed: () {
setState() {

}
},
child: content,
color: color
),
flex: flex,
);
}```

最佳答案

问题是您正在使用无状态类。将其更改为有状态的,它将接受setstate()。

import 'package:flutter/material.dart';

void main() => runApp(MainScreen());

class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
/* -------------------------------------------------------------------------- */
/* Display section */
/* -------------------------------------------------------------------------- */

Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text('Hello World', style: TextStyle(fontSize: 50)),
SizedBox(height: 20),
Text('I\'m small', style: TextStyle(fontSize: 20)),
Container(width: double.infinity, height: 200),
],
),

/* -------------------------------------------------------------------------- */
/* Button section */
/* -------------------------------------------------------------------------- */

Expanded(
child: Row(
children: <Widget>[
/* -------------------------------- Column 1 -------------------------------- */

createButtonColumn(
buttonAmount: 4,
buttonContents: [
Text('+',
style:
TextStyle(fontSize: 25, color: Colors.black54)),
Text('8',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('4',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('0',
style:
TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [
Colors.redAccent,
Colors.black,
Colors.black,
Colors.black
],
),

/* -------------------------------- Column 2 -------------------------------- */

createButtonColumn(
buttonAmount: 4,
buttonContents: [
Text('-',
style:
TextStyle(fontSize: 35, color: Colors.black54)),
Text('9',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('5',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('1',
style:
TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [
Colors.redAccent,
Colors.black,
Colors.black,
Colors.black
],
),

/* -------------------------------- Column 3 -------------------------------- */

createButtonColumn(
buttonAmount: 4,
buttonContents: [
Text('*',
style:
TextStyle(fontSize: 25, color: Colors.black54)),
Text('<--',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('6',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('2',
style:
TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [
Colors.redAccent,
Colors.black,
Colors.black,
Colors.black
],
),

/* -------------------------------- Column 4 -------------------------------- */

createButtonColumn(
buttonAmount: 4,
buttonContents: [
Text('/',
style:
TextStyle(fontSize: 25, color: Colors.black54)),
Text('AC',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('7',
style:
TextStyle(fontSize: 25, color: Colors.white54)),
Text('3',
style:
TextStyle(fontSize: 25, color: Colors.white54))
],
buttonColors: [
Colors.redAccent,
Colors.black,
Colors.black,
Colors.black
],
),

/* -------------------------------- Column 5 -------------------------------- */

createButtonColumn(
buttonAmount: 4,
buttonContents: [
Text('=',
style:
TextStyle(fontSize: 25, color: Colors.black54)),
Text('(',
style:
TextStyle(fontSize: 25, color: Colors.black54)),
Text(')',
style:
TextStyle(fontSize: 25, color: Colors.black54)),
Text('.',
style:
TextStyle(fontSize: 35, color: Colors.black54))
],
buttonColors: [
Colors.yellow[300],
Colors.redAccent,
Colors.redAccent,
Colors.redAccent
],
)
],
),
)
],
),
),
),
);
}
/* -------------------------------------------------------------------------- */
/* Custom functions */
/* -------------------------------------------------------------------------- */

createButtonColumn(
{int buttonAmount,
List<Widget> buttonContents,
bool buttonContentsAreSame = false,
List<Color> buttonColors,
bool buttonColorsAreSame = false,
List<String> actionList}) {
List<Widget> buttonList = [];

if (!buttonContentsAreSame || !buttonColorsAreSame) {
for (int i = 0; i < buttonContents.length; i++) {
buttonList.add(createButton(
content: buttonContents[i],
color: buttonColors[i],
action: actionList[i]));
}
} else {
for (int x = 1; x <= buttonAmount; x++) {
buttonList.add(createButton(content: buttonContents[0]));
}
}

return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, children: buttonList),
);
}

createButton({int flex = 1, Widget content, Color color, String action}) {
return Expanded(
child: RaisedButton(
onPressed: () {
setState(() {
debugPrint('Hello');
});
},
child: content,
color: color),
flex: flex,
);
}
}

关于flutter - 不能引用setState()flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61499224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com