英文:
The ElevatedButton isn't changing the text value
问题
I'm trying to change the value of the text in the button whenever I press it, but it stays the same. Tried using Navigator.push
but I kept getting errors from it so I deleted it off.
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
int myVariableNumber = 0;
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Hello World"),
backgroundColor: Colors.black,
),
backgroundColor: Colors.black,
body: Scaffold(
body: Container(
alignment: Alignment.center,
width: 300,
height: 300,
color: Colors.white,
child: SizedBox(
width: 140,
height: 70,
child: ElevatedButton(
onPressed: () {
myVariableNumber = myVariableNumber + 1;
Navigator.push(context, -)
},
child: Text('Hello $myVariableNumber'),
),
),
),
),
);
}
}
Tried Navigator.push
and I expected it to refresh and change numbers, but it only gave me errors.
英文:
I'm trying to change the value of the text in the button whenever I press it, but it stays the same. Tried using Naviagation.push but I kept getting errors from it so I deleted it off.
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
int myVariableNumber = 0;
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Hello World"),
backgroundColor: Colors.black,
),
backgroundColor: Colors.black,
body: Scaffold(
body: Container(
alignment: Alignment.center,
width: 300,
height: 300,
color: Colors.white,
child: SizedBox(
width: 140,
height: 70,
child: ElevatedButton(
onPressed: () {
myVariableNumber = myVariableNumber + 1;
Navigator.push(context, -)
},
child: Text('Hello $myVariableNumber'),
),
),
),
),
);
}
}
Tried navigation.push and I expected it to refresh and change numbers, but it only gave me errors.
答案1
得分: 1
要更新此按钮的值,您需要通过调用setState
方法重建您的小部件。并将您的状态移到State
子类中(在这种情况下是_MyHomePageState
)。
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int myVariableNumber = 0; // 将您的状态移到这里
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Hello World"),
backgroundColor: Colors.black,
),
backgroundColor: Colors.black,
body: Container(
alignment: Alignment.center,
width: 300,
height: 300,
color: Colors.white,
child: SizedBox(
width: 140,
height: 70,
child: ElevatedButton(
onPressed: () => setState(() { // 这将使用新的myVariableNumber值更新您的小部件
myVariableNumber = myVariableNumber + 1;
}),
child: Text('Hello $myVariableNumber'),
),
),
),
);
}
}
英文:
To update value of this button, you need to rebuild your widget by calling setState
method. And move your state into State
subclass (in this case is _MyHomePageState
).
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int myVariableNumber = 0; // move your state here
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Hello World"),
backgroundColor: Colors.black,
),
backgroundColor: Colors.black,
// body: Scaffold( // remove this unnecessary Scaffold
body: Container(
alignment: Alignment.center,
width: 300,
height: 300,
color: Colors.white,
child: SizedBox(
width: 140,
height: 70,
child: ElevatedButton(
onPressed: () => setState(() { // this will update your widget with new myVariableNumber value
myVariableNumber = myVariableNumber + 1;
}),
child: Text('Hello $myVariableNumber'),
),
),
),
// ),
);
}
}
答案2
得分: 0
使用 setState()
方法来操作 myVariableNumber
。
英文:
Use setState()
method for myVariableNumber
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论