Home > Net >  Text inside ElevatedButton doesn't change | Flutter
Text inside ElevatedButton doesn't change | Flutter

Time:08-01

i have simple question. Why color of my Text inside the ElevatedButton is not changing when i click the button?

I tried to do it on 2 ways:

  • By adding variable _firColor in color : inside TextStyle as parameter.

  • By adding variable _firColor in foregroundColor : inside ElevatedButton.styleFrom as parameter.

None of them works.

Printing _firColor always has same result. It prints 2 lines after one click.

result of printing

It behave like after changing value of _firColor it always leads to change it's value to his core value.

** 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);
});})));}}

photo

CodePudding user response:

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