简体   繁体   中英

Flutter invalide constant value using Theme.of(context).textTheme

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

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")],
        ),
      ],
    );
  }

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

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