dart 未为类型“_QuoteListState”定义方法“_activityForm”

xxb16uws  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(88)

我一直在尝试使用flutter创建一个简单的弹出窗口,并希望将弹出窗口部件分离到一个单独的类中,但我得到了以下错误:

错误:

The method '_activityForm' isn't defined for the type '_QuoteListState'.
Try correcting the name to the name of an existing method, or defining a method named '_activityForm'.

字符串

表单.dart

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Add Activity'),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: Text('ADD'),
        ),
      ],
    );
  }
}

主省道

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

void main() => runApp(MaterialApp(
  home: QuoteList(),
));

class QuoteList extends StatefulWidget {
  const QuoteList({super.key});

  @override
  State<QuoteList> createState() => _QuoteListState();
}

class _QuoteListState extends State<QuoteList> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Lista'),
        backgroundColor: Colors.blue[300],
      ),
      body: ListView(
        scrollDirection: Axis.vertical,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context, 
            builder:(context) => _activityForm(),
          );
        },
        backgroundColor: Colors.blue[300],
        foregroundColor: Colors.white,
        child: Icon(
          Icons.add,
        ),
      ),
    );
  }
}


为什么会发生这种情况?
有人能帮我解决这个问题吗?
以后还怎么避免这样卡住?

hmae6n7t

hmae6n7t1#

The method '_activityForm' isn't defined for the type '_QuoteListState'.
Try correcting the name to the name of an existing method, or defining a method named '_activityForm'.

字符串
概念基本上是使用“_”作为访问修饰符。现在您将_activityForm定义为顶级私有类。dart语言中的这些类不能在其他文件中访问。你可以使用_activityForm类,如果它在同一个文件中。

相关问题