Home > database >  Flutter unable to add title on AppBarTheme
Flutter unable to add title on AppBarTheme

Time:11-25

I was working on a Flutter project and I tried to add the title parameter to AppBarTheme but it gave me an error. This is the code:

@override
Widget build(BuildContext context){
  return MaterialApp(
    theme: ThemeData(
      primarySwatch: Colors.green,
      accentColor: Colors.pinkAccent,
      fontFamily: 'Quicksand',
      appBarTheme: 
        AppBarTheme(textTheme: ThemeData.light().textTheme.copyWith(
          title: const TextStyle(
            fontFamily: 'Quicksand',
            fontSize: 20,
            )
          )
        )
      )
    );
  }

The error was: The named parameter 'title' isn't defined. How can I solve this?

CodePudding user response:

Here is the way :

import 'package:flutter/material.dart';

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('AppBar titleTextStyle')),
      body: Center(child: Text('Hello World')),
    );
  }
}

void main() {
  runApp(
    MaterialApp(
      theme: ThemeData.light().copyWith(
        appBarTheme: AppBarTheme(
          backgroundColor: Colors.yellow,
          titleTextStyle: TextStyle(color: Colors.black),
          backwardsCompatibility: false, // !!!
        ),
      ),
      home: Home(),
    ),
  );
}

CodePudding user response:

You cannot add title to AppBarTheme, you can only provide the TextStyle for the title in themeData and add the title in AppBar like this:

AppBar(
    title: Text(
      'your text',
      // You need to add this line
      style: Theme.of(context).appBarTheme.TextStyle,
    ),
),
  • Related