我想将按钮 colored颜色 从浅蓝色更改为绿色,以及文本. 我可以改变文字,但我不能改变 colored颜色 .我还没解决. 我想在'onPressed()'中使用'setState()'来在我按下按钮时改变 colored颜色 . 如果我使用styleFrom,我可以在setState中使用styleFrom来更改 colored颜色 吗? 我该怎么办? 有它的源代码.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: const MyStatefulWidget(),
),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
String frontText = "Press the Button to see studuent infomation";
@override
Widget build(BuildContext context) { // Defining Main Widget plqcing on Scaffold
final ButtonStyle style = ElevatedButton.styleFrom();
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"$frontText", key: Key("textKey"),
style: TextStyle(color: Colors.blue, fontSize: 15),
),
const SizedBox(height: 20),
ElevatedButton(
style: ElevatedButton.styleFrom( // Setting the ElevatedButton style
backgroundColor: Colors.lightBlue[50],
foregroundColor: Colors.blue,
textStyle: TextStyle(fontSize: 20)
),
child: const Text('Push'),
onPressed: () {
setState(() { // once push the button, text printed over the button will be changed
frontText = "20190952, 윤영준";
ElevatedButton.styleFrom(
backgroundColor: Colors.green
); // I want change color to green, but i can't
});
},
),
],
),
);
}
}