flutter 如何返回colorScheme的颜色,错误:未定义的名称'context'

jk9hmnmh  于 2023-03-19  发布在  Flutter
关注(0)|答案(1)|浏览(183)

我想请你帮忙解决我的问题。
问题的关键在于:我需要使用Theme类返回文本的颜色主题,该类在应返回Color的函数中使用ColorScheme.secondary类型。尝试实现此操作时,出现Undefined name 'context'错误。请尝试将名称更正为已定义的名称,或定义名称
我怎样才能解决这个问题。

import 'package:app_calculator/calculator.dart';

class ButtonWidget extends StatelessWidget {
  final String text;
  final VoidCallback onClicked;
  final VoidCallback onClickedLong;

  const ButtonWidget({
    super.key,
    required this.text,
    required this.onClicked,
    required this.onClickedLong,
  });

  @override
  Widget build(BuildContext context) {
    final color = getTextColor(text);
    final double fontSize = Utils.isOperator(text, hasEquals: true) ? 26 : 22;
    final style = TextStyle(
      color: color,
      fontSize: fontSize,
      fontWeight: FontWeight.bold,
    );

    return Expanded(
      child: Container(
        height: double.infinity,
        margin: const EdgeInsets.all(6),
        child: ElevatedButton(
          onPressed: onClicked,
          onLongPress: onClickedLong,
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.transparent,
            elevation: 0,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
            ),
          ),
          child: text == '<'
              ? Icon(Icons.backspace_outlined, color: color)
              : Text(text, style: style),
        ),
      ),
    );
  }

  Color getTextColor(String buttonText) {
    switch (buttonText) {
      case '+':
      case '-':
      case '⨯':
      case '÷':
      case '=':
        return Colors.red;
      case 'AC':
      case '<':
        return Colors.red;
      default:
        return Theme.of(context).colorScheme.secondary; // <---- error
    }
  }
}
class MyDarkTheme {
  static final dark = ThemeData(
    colorScheme: ColorScheme.fromSwatch().copyWith(
      secondary: Pallete.white,
    ),
}

我试着查找信息并重写方法,但我仍然不知道如何解决它。

a0zr77ik

a0zr77ik1#

你正在访问context超出了它的作用域。
尝试将其移动到方法build

import 'package:app_calculator/calculator.dart';

class ButtonWidget extends StatelessWidget {
  final String text;
  final VoidCallback onClicked;
  final VoidCallback onClickedLong;

  const ButtonWidget({
    super.key,
    required this.text,
    required this.onClicked,
    required this.onClickedLong,
  });

  @override
  Widget build(BuildContext context) {

     Color getTextColor(String buttonText,) {
    switch (buttonText) {
      case '+':
      case '-':
      case '⨯':
      case '÷':
      case '=':
        return Colors.red;
      case 'AC':
      case '<':
        return Colors.red;
      default:
        return Theme.of(context).colorScheme.secondary; // <---- error
    }
  }
    final color = getTextColor(text,);
    final double fontSize = Utils.isOperator(text, hasEquals: true) ? 26 : 22;
    final style = TextStyle(
      color: color,
      fontSize: fontSize,
      fontWeight: FontWeight.bold,
    );

    return Expanded(
      child: Container(
        height: double.infinity,
        margin: const EdgeInsets.all(6),
        child: ElevatedButton(
          onPressed: onClicked,
          onLongPress: onClickedLong,
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.transparent,
            elevation: 0,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
            ),
          ),
          child: text == '<'
              ? Icon(Icons.backspace_outlined, color: color)
              : Text(text, style: style),
        ),
      ),
    );
  }

 
}

你也可以给予它一个参数context,如下所示:

import 'package:app_calculator/calculator.dart';

class ButtonWidget extends StatelessWidget {
  final String text;
  final VoidCallback onClicked;
  final VoidCallback onClickedLong;

  const ButtonWidget({
    super.key,
    required this.text,
    required this.onClicked,
    required this.onClickedLong,
  });

  @override
  Widget build(BuildContext context) {
//-----------------------------------------
    final color = getTextColor(text,context);
    final double fontSize = Utils.isOperator(text, hasEquals: true) ? 26 : 22;
    final style = TextStyle(
      color: color,
      fontSize: fontSize,
      fontWeight: FontWeight.bold,
    );

    return Expanded(
      child: Container(
        height: double.infinity,
        margin: const EdgeInsets.all(6),
        child: ElevatedButton(
          onPressed: onClicked,
          onLongPress: onClickedLong,
          style: ElevatedButton.styleFrom(
            backgroundColor: Colors.transparent,
            elevation: 0,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
            ),
          ),
          child: text == '<'
              ? Icon(Icons.backspace_outlined, color: color)
              : Text(text, style: style),
        ),
      ),
    );
  }
//-------------------------------------------

  Color getTextColor(String buttonText,context) {
    switch (buttonText) {
      case '+':
      case '-':
      case '⨯':
      case '÷':
      case '=':
        return Colors.red;
      case 'AC':
      case '<':
        return Colors.red;
      default:
        return Theme.of(context).colorScheme.secondary; // <---- error
    }
  }
}

相关问题