[英]How can I send onTab signal from nested stateful widget to main.dart
In my main.dart I have a timer and GestureDetector. 在我的main.dart中,我有一个计时器和GestureDetector。 The GestureDetector onTap etc. handle the user interaction with _handleUserInteraction(). GestureDetector onTap等使用_handleUserInteraction()处理用户交互。 Every time user tabs the app reset the timer. 每次用户使用标签页时,应用都会重置计时器。 My problem is I need to send signal the onTab (or similar) from form_a.dart to home.dart . 我的问题是我需要将信号onTab(或类似信号)从form_a.dart发送到home.dart 。
How can I send onTab signal from nested stateful widget FormA (**form_a.dart ) to home.dart?** 如何从嵌套的有状态小部件FormA(** form_a.dart )向home.dart 发送onTab信号 ?**
I need to access _timer abd void _handleUserInteraction() function from any widget (as deep as gets) under home.dart 我需要从home.dart下的任何小部件(尽可能深入)访问_timer abd void _handleUserInteraction()函数
import 'package:flutter/material.dart';
import 'dart:async';
import 'main.dart';
import 'details.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Timer _timer;
// TODO: 3 - INITIALIZE TIMER
void _initializeTimer() {
_timer = Timer.periodic(const Duration(minutes: 5), (__) {
_logOutUser();
});
}
// TODO: 4 - LOG OUT USER
void _logOutUser() {
_timer.cancel();
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new MyApp()));
}
// TODO: 5 - HANDLE USER INTERACTION
void _handleUserInteraction([_]) {
if (!_timer.isActive) {
return;
}
_timer.cancel();
_initializeTimer();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _handleUserInteraction,
onDoubleTap: _handleUserInteraction,
onLongPress: _handleUserInteraction,
onScaleEnd: _handleUserInteraction,
onScaleStart: _handleUserInteraction,
onScaleUpdate: _handleUserInteraction,
onTapCancel: _handleUserInteraction,
onTapDown: _handleUserInteraction,
onTapUp: _handleUserInteraction,
child: new Scaffold(
appBar: AppBar(
title: Text("HOME PAGE"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'GOTO DETAILS PAGE',
),
new RaisedButton(
child: new Text("Details"),
onPressed: (){
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new Details()));
}
)
],
),
),
));
}
}
class LoginState extends InheritedWidget{
LoginState({Key key, Widget child});
@override
bool updateShouldNotify(InheritedWidget oldWidget) => true;
}
One way to do this would be by using the capabilities of InheritedWidget
. 一种方法是使用InheritedWidget
的功能。
First create your InheritedWidget
: 首先创建您的InheritedWidget
:
class MyInheritedWidget extends InheritedWidget {
const MyInheritedWidget({
Key key,
VoidCallBack handleOnTap,
Widget child,
}) : assert(color != null),
assert(child != null),
super(key: key, child: child);
final VoidCallBack handleOnTap;
static MyInheritedWidget of(BuildContext context) {
return context.inheritFromWidgetOfExactType(MyInheritedWidget);
}
@override
bool updateShouldNotify(MyInheritedWidget old) => handleOnTap != old.handleOnTap;
}
That widget is usefull to retrieve common data between child Widgets and one of their parent in the hierarchy. 该小部件可用于检索子小部件及其层次结构中的父小部件之间的公共数据。
In your main file, create your handler to match a VoidCallBack
signature (or change the signature if you want). 在主文件中,创建您的处理程序以匹配VoidCallBack
签名(或根据需要更改签名)。
void _handleUserInteraction() {
// do you stuff to reset your timer
}
So you should now wrap your PageView
with your InheritedWidget
to include it as high in the hierarchy as possible (as close as you main as you can) and give it your handling method 因此,您现在应该使用InheritedWidget
封装PageView
,以将其尽可能高地包含在层次结构中(尽可能靠近主体),并为其提供处理方法
MyInheritedWidget(child: PageView(), handleOnTap: _handleUserInteraction)
Finally, in your onTap
call your InheritedWidget handler: 最后,在onTap
调用InheritedWidget处理程序:
onTap: () {MyInheritedWidget.of(context).handleOnTap}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.