Home > other >  Why i can't use appbartheme to define the title in AppBar class
Why i can't use appbartheme to define the title in AppBar class

Time:08-24

I want to set the appbarTheme for the Appbar but i can't.

Here is my appbarTheme

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Helo mu baby',
      theme: ThemeData(
        primarySwatch: Colors.green,
        //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,
                  fontWeight: FontWeight.bold,
                ),
        ),
        fontFamily: "QuickSand",
        appBarTheme: AppBarTheme(
          textTheme: ThemeData.light().textTheme.copyWith(
                titleLarge: TextStyle(
                  color: Colors.indigo,
                  fontFamily: 'OpenSans',
                  fontSize: 45,
                  fontWeight: FontWeight.bold,
                ),
              ),
        ),
      ),
      home: MyHomeApp(),
    );
  }
}

Here is my AppBar and i use "titleTextStyle: Theme.of(context).appBarTheme.titleTextStyle" to define the theme but it is not correct.

 Widget build(BuildContext context) {
    return Scaffold(  
      appBar: AppBar(
     titleTextStyle: Theme.of(context).appBarTheme.titleTextStyle,
        //100px
        title: Text("Helo mu baby"),
        actions: [
          IconButton(
              onPressed: (() {
                _startAddNewTransaction(context);
              }),
              icon: Icon(Icons.add))
        ],
      ),

CodePudding user response:

Don' use textTheme because it is deprecated and shouldn't be used.

'textTheme' is deprecated and shouldn't be used. This property is no longer used, please use toolbarTextStyle and titleTextStyle instead.

titleTextStyle

import 'package:flutter/material.dart';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Helo mu baby',
      theme: ThemeData(
        primarySwatch: Colors.green,
        //Some Widget will use accentColor first and use primarySwatch like insure
        accentColor: Colors.amber,
        /*'accentColor' is deprecated and shouldn't be used. 
         * Use colorScheme.secondary instead. For more information, 
         * consult the migration guide at 
         * https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. 
         This feature was deprecated after v2.3.0-0.1.pre..*/
        textTheme: ThemeData.light().textTheme.copyWith(
              titleLarge: const TextStyle(
                fontFamily: 'OpenSans',
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
        fontFamily: "QuickSand",
        appBarTheme: const AppBarTheme(
          /*
          textTheme: ThemeData.light().textTheme.copyWith(
                titleLarge: const TextStyle(
                  color: Colors.indigo,
                  fontFamily: 'OpenSans',
                  fontSize: 45,
                  fontWeight: FontWeight.bold,
                ),
              ),
              */
          titleTextStyle: TextStyle(
            color: Colors.indigo,
            fontFamily: 'OpenSans',
            fontSize: 45,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      home: MyHomeApp(),
    );
  }
}

class MyHomeApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        titleTextStyle: Theme.of(context).appBarTheme.titleTextStyle,
        //100px
        title: const Text("Helo mu baby"),
        actions: [
          IconButton(
              onPressed: (() {
                //_startAddNewTransaction(context);
              }),
              icon: const Icon(Icons.add))
        ],
      ),
    );
  }
}

CodePudding user response:

You defined a textTheme in your appbarTheme, and use a titleTextStyle in your appbarTheme, of course it won't do the work. Try this.

 Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Helo mu baby',
      theme: ThemeData(
        primarySwatch: Colors.green,
        //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,
                  fontWeight: FontWeight.bold,
                ),
        ),
        fontFamily: "QuickSand",
        appBarTheme: AppBarTheme(
          titleTextStyle: ThemeData.light().appBarTheme.titleTextStyle.copyWith(fontFamily: 'OpenSans',
                  fontSize: 18,
                  fontWeight: FontWeight.bold,)
        ),
      ),
      home: MyHomeApp(),
    );
  }
}
  • Related