Home > Software design >  Flutter invalide constant value using Theme.of(context).textTheme
Flutter invalide constant value using Theme.of(context).textTheme

Time:05-03

Hi why do I get an error "invalid constant value" when using Theme.of(context).themeText.headline4 inside a widget. It works everywhere else except inside my widget.

Here's my code: This one works

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.title,
          style: Theme.of(context).textTheme.headline1,
        ),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          const SizeBox(size: 20),
          buildCalculator(),
          const Text(
            'You have pushed this buttons this many times:'
,
          ),
          Text(
            '$_counter',
            style: Theme.of(context).textTheme.headline4, //No error here
          ),
        ],
      ),
      // ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

But this one doesnt, it says invalid constant value

  Widget buildConvertor() {
    ThemeData:
    defaultTheme;
    return Row(
      children: [
        Container(
          child: const Text(
            "Text here",
            style: Theme.of(context).textTheme.headline4, //this is where the error is
          ),
        ),
        const Spacer(
          flex: 1,
        ),
        Column(
          children: const <Widget>[Text("More text")],
        ),
      ],
    );
  }

There are both under the same class so why are they behaving different. And please help me with a solution. I just need help with the theme

CodePudding user response:

As type of Theme.of(context).textTheme.headline4 is not constant.

for using const for Text, the value of style must be constant.

Here, is the solution.

Widget buildConvertor() {
    ThemeData:
    defaultTheme;
    return Row(
      children: [
        Container(
          child: Text(
            "Text here",
            style: Theme.of(context).textTheme.headline4,
          ),
        ),
        const Spacer(
          flex: 1,
        ),
        Column(
          children: const <Widget>[Text("More text")],
        ),
      ],
    );
  }

CodePudding user response:

A const is a constant that does not change, so when there are arguments that can change you cannot declare it as const.

Text(
  "Text here",
  style: Theme.of(context).textTheme.headline4, //these are arguments so the Text cannot be const
)

but here, you can declare it as const

const Text(
  "Text here",
  style: TextStyle(fontSize: 15), //style cannot change here so you can declare the Text as const
)

you can even declare the style as const because it wont change

const Text(
   "Text here",
   style: const TextStyle(fontSize: 15),
)
  • Related