Home > Software design >  How can I change the color of text in a StatefulWidget or StatelessWidget?
How can I change the color of text in a StatefulWidget or StatelessWidget?

Time:01-26

I am trying to change the 'MyHomePage(title: 'This is my app now! Mwuah Ha Ha!')' to be white font. I don't know if i'm supposed to change it outside of the class, or from inside the class where the paramter 'title' is first described.

I am new to Flutter and practicing with a book, however it is not up-to-par with Flutters new updates.

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.black,
      ),
      home: const MyHomePage(title: 'This is my app now! Mwuah Ha Ha!'),
    );
  }
}


class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

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

I'm expecting the font to be changed to white for the title with a black background. I am not fluent enough with Flutter yet, to know where and how to put the color change. Thank you in advance!

CodePudding user response:

So to do that what you need to do is go to where the variable title is being used in this case it should be like this:

appBar: AppBar(
    title: Text(widget.title, style:TextStyle(fontColor:Colors.white)),
  ),

CodePudding user response:

Text( "This is Example, textAlign: TextAlign.center, style: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, color: Colors.red), )

  • Related