flutter 如何在elevatedButtonTheme中更改elevatedButton文本颜色?

wfypjpf4  于 2023-01-31  发布在  Flutter
关注(0)|答案(4)|浏览(285)

我尝试在主题的elevatedButtonTheme属性中更改ElevatedButton文本颜色,但无法更改。我知道Text的子级中的TextStyle可以更改Text的颜色,但我更喜欢在elevatedButtonTheme中定义。

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')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

nfs0ujit

nfs0ujit1#

这里是代码片段,如果你想使用主题,然后在这里:

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(
                        foregroundColor: Colors.pinkAccent,//change background color of button
                        backgroundColor: 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),
                        ),
                      ),
                    ),
                  ),
7xllpg7q

7xllpg7q2#

由于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: () {},
    );
7gcisfzg

7gcisfzg3#

`Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
                        foregroundColor: Colors.white,//change background color of button
                        backgroundColor: Colors.blue,//change text color of button
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(25),
                        ),
                        elevation: 15.0,
                      ),
        child: Text('Answer 1'),
        onPressed: selectHandler,
      ),
    );
  }`

primary and onprimary are not in use
7nbnzgx9

7nbnzgx94#

实际上,我看到了与Mihai的answer相反的情况:

ElevatedButton(
    style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white, // text color
        backgroundColor: Colors.black), // background color
  ),

相关问题