Flutter 如何在elevatedButtonTheme中更改ElevatedButton文本颜色?
我正在尝试更改主题中elevatedButtonTheme属性中的ElevatedButton文本颜色,但无法更改。 我知道文本子元素中的TextStyle可以更改文本的颜色,但我更喜欢在elevatedButtonTheme中定义Flutter 如何在elevatedButtonTheme中更改ElevatedButton文本颜色?,flutter,flutter-layout,Flutter,Flutter Layout,我正在尝试更改主题中elevatedButtonTheme属性中的ElevatedButton文本颜色,但无法更改。 我知道文本子元素中的TextStyle可以更改文本的颜色,但我更喜欢在elevatedButtonTheme中定义 import 'package:flutter/material.dart'; import 'package:hexcolor/hexcolor.dart'; void main() { runApp(MyApp()); } class MyApp ext
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page with 2.0'),
theme: ThemeData(
primaryColor: HexColor('#003f71'),
accentColor: HexColor('#e09e36'),
scaffoldBackgroundColor: HexColor('#003f71'),
textTheme: TextTheme(bodyText2: TextStyle(fontSize: 16.0), button: TextStyle(fontSize: 16.0)),
elevatedButtonTheme:
ElevatedButtonThemeData(style: ElevatedButton.styleFrom(minimumSize: Size(1, 45), primary: HexColor('#e09e36'), textStyle: TextStyle(fontSize: 16.0, color: Colors.black))),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(15.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: const EdgeInsets.symmetric(vertical: 15.0),
child: FractionallySizedBox(
alignment: Alignment.center,
widthFactor: 1.0,
child: ElevatedButton(onPressed: () {}, child: Text('ElevatedButton')),
),
),
],
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:hexcolor/hexcolor.dart”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主页:MyHomePage(标题:“2.0版颤振演示主页”),
主题:主题数据(
primaryColor:HexColor('#003f71'),
accentColor:HexColor(“#e09e36”),
脚手架背景颜色:HexColor(“#003f71”),
textTheme:textTheme(bodyText2:TextStyle(fontSize:16.0),按钮:TextStyle(fontSize:16.0)),
提升按钮主题:
ElevatedButtonThemeData(样式:ElevatedButton.styleFrom(最小大小:大小(1,45),主颜色:HexColor('#e09e36'),文本样式:文本样式(fontSize:16.0,颜色:Colors.black)),
),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
对齐:对齐.center,
边距:所有常数边集(15.0),
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
容器(
边距:常量边集。对称(垂直:15.0),
孩子:部分物理盒子(
对齐:对齐.center,
宽度系数:1.0,
子项:ElevatedButton(按下时:(){},子项:Text('ElevatedButton')),
),
),
],
),
),
);
}
}
以下是代码片段
如果您想使用主题,请参见:
MaterialApp(
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
onPrimary: Colors.yellow,
)),
),
home: MyWidget());
不设置文本主题,您可以像这样更改文本的颜色
Container(
width: MediaQuery.of(context).size.width * 0.6,
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,//change background color of button
onPrimary: Colors.yellow,//change text color of button
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
elevation: 15.0,
),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
'Proceed to Pay',
style: TextStyle(fontSize: 20),
),
),
),
),
下面是代码片段
如果您想使用主题,请参见:
MaterialApp(
theme: ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
onPrimary: Colors.yellow,
)),
),
home: MyWidget());
不设置文本主题,您可以像这样更改文本的颜色
Container(
width: MediaQuery.of(context).size.width * 0.6,
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.pinkAccent,//change background color of button
onPrimary: Colors.yellow,//change text color of button
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
elevation: 15.0,
),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
'Proceed to Pay',
style: TextStyle(fontSize: 20),
),
),
),
),
成功更改,谢谢!成功更改,谢谢!