Class 颤振:从另一个文件的父类调用子类函数

Class 颤振:从另一个文件的父类调用子类函数,class,flutter,dart,widget,setstate,Class,Flutter,Dart,Widget,Setstate,问题: 如何从IconButton的onPressed()调用methodA()。 我尝试使用GlobalKey来实现这一点: GlobalKey GlobalKey=GlobalKey(); 但它返回一个错误 我读过很多关于这方面的论坛,我尝试过所有的解决方案,但没有一个对我有效 代码: main.dart import 'package:flutter/material.dart'; import 'button.dart'; void main() { runApp(Material

问题: 如何从IconButton的onPressed()调用methodA()。 我尝试使用GlobalKey来实现这一点: GlobalKey GlobalKey=GlobalKey(); 但它返回一个错误

我读过很多关于这方面的论坛,我尝试过所有的解决方案,但没有一个对我有效

代码: main.dart

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

void main() {
  runApp(MaterialApp( title: 'My app', home: MyApp(),));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Icon(Icons.help),
            onPressed: () {
              // how can I call methodA from here?
            },
          ),
        ),
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: MyButton(),
    );
  }
}
import 'package:flutter/material.dart';

class MyButton extends StatefulWidget {
  @override
  _MyButtonState createState() => _MyButtonState();
}

class _MyButtonState extends State<MyButton> {

  @override
  Widget build(BuildContext context) {
    return Container( );
  }

  void methodA(){
    print('methodA');
  }

}
导入“包装:颤振/材料.省道”;
导入“button.dart”;
void main(){
runApp(MaterialApp(标题:“我的应用”,主页:MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
领先:IconButton(
图标:图标(Icons.help),
已按下:(){
//我怎么能在这里叫methodA?
},
),
),
正文:主页(),
),
);
}
}
类主页扩展了StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回中心(
子项:MyButton(),
);
}
}
按钮。省道

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

void main() {
  runApp(MaterialApp( title: 'My app', home: MyApp(),));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Icon(Icons.help),
            onPressed: () {
              // how can I call methodA from here?
            },
          ),
        ),
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: MyButton(),
    );
  }
}
import 'package:flutter/material.dart';

class MyButton extends StatefulWidget {
  @override
  _MyButtonState createState() => _MyButtonState();
}

class _MyButtonState extends State<MyButton> {

  @override
  Widget build(BuildContext context) {
    return Container( );
  }

  void methodA(){
    print('methodA');
  }

}
导入“包装:颤振/材料.省道”;
类MyButton扩展StatefulWidget{
@凌驾
_MyButtonState createState()=>\u MyButtonState();
}
类_MyButtonState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器();
}
void methodA(){
打印(“methodA”);
}
}

我已经阅读了许多关于此的论坛,我尝试了所有提出的解决方案,但没有一个对我有效。

首先,您必须将文件作为
包导入
main.dart

Main.dart:(正在编写导入文件的方法)

注意:这适用于
lib
目录下的文件。 如果您的文件位于
lib中的不同目录下
然后相应地添加路径,

例如:Button.dart位于lib文件夹中的widgets文件夹中:

lib
|____widgets
     |____Button.dart
那么导入语句将如下所示:

import 'package:prioject_name/widgets/Button.dart';
然后尝试使用全局键方法调用函数:


如果它仍然不起作用,那么你可以使用我的方法, 如何在
onPressed
onTapped
中从不同的
类调用方法:

你的Button.dart文件

import 'package:flutter/material.dart';

// changed the method definition class
class MyButton extends StatefulWidget {
void methodA(){
    print('methodA');
  }
  @override
  _MyButtonState createState() => _MyButtonState();
}

class _MyButtonState extends State<MyButton> {

  @override
  Widget build(BuildContext context) {
  ...
  widget.methodA(); // this would call the method A, anywhere inside the Widget build() function.
    return Container( );
  }
  

}

首先,您必须将文件作为
main.dart
中的
包导入:

Main.dart:(正在编写导入文件的方法)

注意:这适用于
lib
目录下的文件。 如果您的文件位于
lib中的不同目录下
然后相应地添加路径,

例如:Button.dart位于lib文件夹中的widgets文件夹中:

lib
|____widgets
     |____Button.dart
那么导入语句将如下所示:

import 'package:prioject_name/widgets/Button.dart';
然后尝试使用全局键方法调用函数:


如果它仍然不起作用,那么你可以使用我的方法, 如何在
onPressed
onTapped
中从不同的
类调用方法:

你的Button.dart文件

import 'package:flutter/material.dart';

// changed the method definition class
class MyButton extends StatefulWidget {
void methodA(){
    print('methodA');
  }
  @override
  _MyButtonState createState() => _MyButtonState();
}

class _MyButtonState extends State<MyButton> {

  @override
  Widget build(BuildContext context) {
  ...
  widget.methodA(); // this would call the method A, anywhere inside the Widget build() function.
    return Container( );
  }
  

}
看一看课堂(并观看视频)

用于小部件的基类,这些小部件可以有效地向下传播信息 树

您可以查看如何创建一个包含
ValueNotifier
InheritedWidget

class MyInheritedWidget extends InheritedWidget {
  
  final ValueNotifier<int> buttonTapCountNotifier;

  const MyInheritedWidget({
    Key key,
    @required this.buttonTapCountNotifier,
    @required Widget child,
  })  : assert(child != null),
        super(key: key, child: child);

  static MyInheritedWidget of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();
  }

}
类MyInheritedWidget扩展了InheritedWidget{
“最终价值通知人”按钮“最终价值通知人”;
常量MyInheritedWidget({
关键点,
@需要此.buttonTapCountNotifier,
@必需的小部件子项,
}):assert(child!=null),
super(key:key,child:child);
的静态MyInheritedWidget(BuildContext上下文){
返回context.dependonheritedwidgetofexacttype();
}
}
您的
MyButton
类可以调用
MyInheritedWidget.of(context).ButtontAppCountNotifier
来获取
ValueNotifier
并向其添加侦听器。
每次
ValueNotifier
通知您的
MyButton
类该值已增加时,您可以执行
methodA

查看该类(并观看视频)

用于小部件的基类,这些小部件可以有效地向下传播信息 树

您可以查看如何创建一个包含
ValueNotifier
InheritedWidget

class MyInheritedWidget extends InheritedWidget {
  
  final ValueNotifier<int> buttonTapCountNotifier;

  const MyInheritedWidget({
    Key key,
    @required this.buttonTapCountNotifier,
    @required Widget child,
  })  : assert(child != null),
        super(key: key, child: child);

  static MyInheritedWidget of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();
  }

}
类MyInheritedWidget扩展了InheritedWidget{
“最终价值通知人”按钮“最终价值通知人”;
常量MyInheritedWidget({
关键点,
@需要此.buttonTapCountNotifier,
@必需的小部件子项,
}):assert(child!=null),
super(key:key,child:child);
的静态MyInheritedWidget(BuildContext上下文){
返回context.dependonheritedwidgetofexacttype();
}
}
您的
MyButton
类可以调用
MyInheritedWidget.of(context).ButtontAppCountNotifier
来获取
ValueNotifier
并向其添加侦听器。

每次
ValueNotifier
通知您的
MyButton
类该值已增加时,您可以执行
methodA

您可以使用Provider包,这是管理flatter应用程序中状态的首选方法。这将帮助你以一种巧妙的方式组织和发展应用程序

请看下面的工作代码

  • 定义将保存的更改通知程序(按提供程序) 应用程序在唯一位置的当前状态以及onPress功能的行为
  • 你包装你的应用程序 使用ChangeNotifierProvider小部件
  • 你把收条包起来 具有消费者的小部件
  • 当您 需要做点什么并调用一个方法
  • 它将通知消费者更改
  • 代码:

    导入“包装:颤振/材料.省道”;
    “导入”包:提供程序/provid