Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

I tried to do it on 2 ways:

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

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

  • 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

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading