简体   繁体   中英

How can I change the color of text in a StatefulWidget or StatelessWidget?

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!

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)),
  ),

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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