简体   繁体   English

ElevatedButton 中的文本不会改变 | Flutter

[英]Text inside ElevatedButton doesn't change | Flutter

i have simple question.我有一个简单的问题。 Why color of my Text inside the ElevatedButton is not changing when i click the button?为什么当我单击按钮时, ElevatedButton内的文本颜色没有改变?

I tried to do it on 2 ways:我试图通过两种方式做到这一点:

  • By adding variable _firColor in color: inside TextStyle as parameter.通过在color:中添加变量_firColor : TextStyle作为参数。

  • By adding variable _firColor in foregroundColor: inside ElevatedButton.styleFrom as parameter.通过在ElevatedButton.styleFrom内的foregroundColor:中添加变量_firColor作为参数。

None of them works.它们都不起作用。

Printing _firColor always has same result.打印_firColor总是有相同的结果。 It prints 2 lines after one click.一键打印2行。

打印结果

It behave like after changing value of _firColor it always leads to change it's value to his core value.它的行为就像在更改_firColor的值之后总是会导致将其值更改为他的核心值。

** CODE:** ** 代码:**

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => const MaterialApp(
        home: MyHomePage(),
      );
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    Color _firColor = const Color.fromARGB(199, 84, 84, 84);

    return Scaffold(
      backgroundColor: const Color.fromARGB(198, 188, 112, 112),
      body: Center(
        child: ElevatedButton(
          child: Text(
            "login",
            style: TextStyle(fontSize: 40, color: _firColor),
          ),
          style: ElevatedButton.styleFrom(
            foregroundColor: _firColor,
            backgroundColor: Colors.white,
          ),
          onPressed: () {
            setState(() {
              print(_firColor);
              _firColor = Colors.black;
              print(_firColor);
});})));}}

照片

class _MyHomePageState extends State<MyHomePage> {
 Color _firColor = const Color.fromARGB(199, 84, 84, 84);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM