Home > Net >  Why i can not use "primarySwatch" in my code
Why i can not use "primarySwatch" in my code

Time:08-25

Here is my main.dart file , i define primarySwatch:Colors.deepPurple

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Helo mu baby',
      theme: ThemeData(
        primarySwatch: Colors.deepPurple,
        fontFamily: "QuickSand",
        //colorScheme: ColorScheme(secondary: Colors.deepOrange,brightness: Brightness.light),
        //Some Widget will use accentColor first and use primarySwatch like insure
        accentColor: Colors.amber,
        textTheme: ThemeData.light().textTheme.copyWith(
              titleLarge: TextStyle(
                fontFamily: 'OpenSans',
                fontSize: 18,
                color: Colors.blue[300],
                fontWeight: FontWeight.bold,
              ),
              titleMedium: TextStyle(
                color: Color.fromARGB(255, 92, 33, 201),
                  fontFamily: 'OpenSans',
                fontSize: 18,
                fontWeight: FontWeight.bold,

              )
            ),
        appBarTheme: AppBarTheme(
          titleTextStyle: TextStyle(
            color: Color.fromARGB(255, 120, 225, 68),
            fontFamily: 'OpenSans',
            fontSize: 45,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      home: MyHomeApp(),
    );
  }
}

Here is my ListView Widget and I use "title: Text(transactions[index].title,style:Theme.of(context).primarySwatch,)" to define the Text widget but i can't

 ListView.builder(
                itemCount: transactions.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    leading: CircleAvatar(
                      radius: 30,
                      child: Container(
                        padding: EdgeInsets.all(10),
                        child: FittedBox(child: Text('\$${transactions[index].amount}'))),
                    ),
                    title: Text(transactions[index].title,style:Theme.of(context).primarySwatch,),
                  );
                },
              )

CodePudding user response:

The primarySwatch is now a property object of the colorScheme property from your ThemeData widget.

You can use like so:

MaterialApp(
      title: 'Helo mu baby',
      theme: ThemeData(
        fontFamily: "QuickSand",
        colorScheme: ColorScheme.fromSwatch(
            primarySwatch: Colors.deepPurple,
        ),
        //Some Widget will use accentColor first and use primarySwatch like insure
        accentColor: Colors.amber,

You can learn more about it here:
https://api.flutter.dev/flutter/material/ThemeData-class.html

  • Related