我正在尝试扩展BuildContext,这对于我的学习来说比其他任何事情都更多。但是,我收到此错误:

我已将此添加到我在其中使用它的文件中:

extension AppBuildContext on BuildContext {
  ThemeData get theme {
    return Theme.of(this);
  }

  FocusScopeNode get focusScope {
    return FocusScope.of(this);
  }

  NavigatorState get navigator {
    return Navigator.of(this);
  }

  T args<T>() {
    return ModalRoute.of(this).settings.arguments as T;
  }
}
我这样使用它:
class LoginView extends StatelessWidget {
  @override
  Widget build(AppBuildContext context) {
    return Scaffold(
        body: Center(
            child: VpGradientContainer(
      beginColor: initialGradientColor,
      endColor: Theme.of(context).colorScheme.primary,
      child: Column(...
我究竟做错了什么?
Flutter和Dart版本:
Flutter 1.22.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 84f3d28555 (3 days ago) • 2020-10-15 16:26:19 -0700
Engine • revision b8752bbfff
Tools • Dart 2.10.2

最佳答案

AppBuildContext名称仅用于声明导入限制。要使用它,您仍然可以调用BuildContext

import 'AppBuildContext.dart'; // import the file that contains the extension manully

class LoginView extends StatelessWidget {
  @override
  Widget build(BuildContext context) { // just use normal BuildContext
    return Scaffold(
        body: Center(
            child: VpGradientContainer(
      beginColor: initialGradientColor,
      endColor: context.theme.colorScheme.primary,
      child: Column(...),

08-05 11:17