如何更改提升按钮主题中的提升按钮文本颜色?

DIG*_*UAD 4 flutter flutter-layout

我正在尝试更改主题中提升按钮主题属性中的提升按钮文本颜色,但无法更改。我知道 Text 子节点中的 TextStyle 可以更改 Text 的颜色,但我更喜欢在提升的ButtonTheme中定义。

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')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Ami*_*rya 8

如果您想使用主题,这是代码片段,那么它是:

MaterialApp(
        theme: ThemeData(
          elevatedButtonTheme: ElevatedButtonThemeData(
              style: ElevatedButton.styleFrom(
            onPrimary: Colors.yellow,
          )),
        ),
        home: MyWidget());
Run Code Online (Sandbox Code Playgroud)

无需设置文本主题,您就可以像这样更改文本的颜色。

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),
                        ),
                      ),
                    ),
                  ),
Run Code Online (Sandbox Code Playgroud)

  • onPrimary:在新版本中已弃用,您能给出新答案吗? (2认同)

小智 7

事实上,我看到了米哈伊答案的相反情况:

ElevatedButton(
    style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white, // text color
        backgroundColor: Colors.black), // background color
  ),
Run Code Online (Sandbox Code Playgroud)


Mih*_*hai 5

由于 primay 和 on Primary 已被弃用,因此以下是定义按钮颜色和按钮文本颜色的新方法:

ElevatedButton(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white, // change background color of button
        backgroundColor: Colors.purple, // change text color of button
      ),
      child: Text('Your Text Here'),
      onPressed: () {},
    );
Run Code Online (Sandbox Code Playgroud)