flutterdartflutter-hooks

Invalid constant value with useState in HookWidget


Use https://pub.dev/packages/flutter_hooks

There is a simple sample code that changes the letter from "B" to "A" when you tap Text as shown below. (This works fine)

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

void main() {
  runApp(const DemoPage());
}

class DemoPage extends StatelessWidget {
  const DemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Demo'),
        ),
        body: const Foo(),
      )
    );
  }
}

class Foo extends HookWidget {
  const Foo({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final isBar = useState(false);

    return GestureDetector(
      onTapDown: (details) => isBar.value = true,
      onTapUp: (details) => isBar.value = false,
      onTapCancel: () => isBar.value = false,
      child: LayoutBuilder(builder: (context, constraints) {
        return SizedBox(
          width: constraints.maxHeight * 0.5,
          height: constraints.maxHeight * 0.5,
          child: Center(
            child: Text(
              isBar.value ? "A" : "B", // 👈👈👈 Works fine
            ),
          ),
        );
      }),
    );
  }
}

I get an error when I try to change the style as shown below.

            child: Text(
              isBar.value ? "A" : "B",
              style: const TextStyle(
                color: isBar.value ? Colors.yellow : Colors.white, // 👈👈👈 Error
              ),
            ),

Error:

lib/main.dart:45:24: Error: Not a constant expression.
                color: isBar.value ? Colors.yellow : Colors.white,

enter image description here

Full Code:

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

void main() {
  runApp(const DemoPage());
}

class DemoPage extends StatelessWidget {
  const DemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Demo'),
        ),
        body: const Foo(),
      )
    );
  }
}

class Foo extends HookWidget {
  const Foo({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final isBar = useState(false);

    return GestureDetector(
      onTapDown: (details) => isBar.value = true,
      onTapUp: (details) => isBar.value = false,
      onTapCancel: () => isBar.value = false,
      child: LayoutBuilder(builder: (context, constraints) {
        return SizedBox(
          width: constraints.maxHeight * 0.5,
          height: constraints.maxHeight * 0.5,
          child: Center(
            child: Text(
              isBar.value ? "A" : "B",
              style: const TextStyle(
                color: isBar.value ? Colors.yellow : Colors.white,
              ),
            ),
          ),
        );
      }),
    );
  }
}

Solution

  • remove const keyword from the before textStyle.

      child: Text(
                  isBar.value ? "A" : "B",
                  style: const TextStyle( // remove that const
                    color: isBar.value ? Colors.yellow : Colors.white, 
                  ),
                ),