【问题标题】:It is Showing context is undefined它正在显示上下文未定义
【发布时间】:2021-11-15 23:52:36
【问题描述】:

这个飞镖项目它显示一个错误上下文未定义,当我将它作为参数发送时它显示错误我是新来的,我的导师写了同样的(正如我所见) 我正面临这个问题

导入'package:flutter/material.dart';

class Category_item extends StatelessWidget {
  final String title;
  final Color color;
  Category_item(this.title, this.color);
  void selectCategory() {
    Navigator.of(context);
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: selectCategory,
      splashColor: Theme.of(context).primaryColor,
      borderRadius: BorderRadius.circular(15), //wave ripple waves
      child: Container(
        padding: const EdgeInsets.all(15),
        child: Text(
          title,
          style: Theme.of(context).textTheme.headline6,
        ),
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              color.withOpacity(0.7),
              color,
            ],
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          ),
          borderRadius: BorderRadius.circular(15),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart visual-studio-code flutter-dependencies


    【解决方案1】:
     void selectCategory() {
        Navigator.of(context);
      }
    

    这个函数在构建之外。 试试这个:

    import 'package:flutter/material.dart';
    
    class Category_item extends StatelessWidget {
      final String title;
      final Color color;
      Category_item(this.title, this.color);
      void selectCategory() {
        Navigator.of(context);
      }
    
      @override
      Widget build(BuildContext context) {
        return InkWell(
          onTap: selectCategory,
          splashColor: Theme.of(context).primaryColor,
          borderRadius: BorderRadius.circular(15), //wave ripple waves
          child: Container(
            padding: const EdgeInsets.all(15),
            child: Text(
              title,
              style: Theme.of(context).textTheme.headline6,
            ),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  color.withOpacity(0.7),
                  color,
                ],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
              borderRadius: BorderRadius.circular(15),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个-

      selectCategory(BuildContext context) {
          Navigator.of(context);
        }
      

      【讨论】:

        【解决方案3】:

        函数需要有上下文的输入,才能知道上下文是什么。

        void selectCategory(BuildContext context) {
          Navigator.of(context);
        }
        

        然后这样称呼它:

        onTap: () => selectCategory(context),
        

        【讨论】:

          猜你喜欢
          • 2020-04-11
          • 2012-12-28
          • 2019-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多