Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检测用户是否在颤振中进入或退出选项卡_Android_Flutter_Flutter Layout_Flutter Web - Fatal编程技术网

Android 检测用户是否在颤振中进入或退出选项卡

Android 检测用户是否在颤振中进入或退出选项卡,android,flutter,flutter-layout,flutter-web,Android,Flutter,Flutter Layout,Flutter Web,嗨,有没有办法使用WidgetBindingObservators来检测用户何时退出或在Flatter中输入选项卡,然后在重新输入后刷新选项卡 我用这种方式使用它(参考代码),但它不工作,所以我需要一些指导。多谢各位 我想要的输出与Facebook的应用程序类似,特别是新闻提要选项卡。当您退出该选项卡很长时间或从新闻源观看视频很长时间时,该选项卡将自动刷新 我的main.dart代码如下: class MyApp extends StatefulWidget { @override St

嗨,有没有办法使用WidgetBindingObservators来检测用户何时退出或在Flatter中输入选项卡,然后在重新输入后刷新选项卡

我用这种方式使用它(参考代码),但它不工作,所以我需要一些指导。多谢各位

我想要的输出与Facebook的应用程序类似,特别是新闻提要选项卡。当您退出该选项卡很长时间或从新闻源观看视频很长时间时,该选项卡将自动刷新

我的main.dart代码如下:

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}


class MyAppState extends State<MyApp> with WidgetsBindingObserver {
  var lastloaded;
bool isInactive;
int _selectedPage = 0;
final _pageOptions = [
  HomePage(),
  CategoriesPage(),
  Deals(),
  ProfileAccount(),
  CartPage(),
];

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch(state){
      case AppLifecycleState.paused:
      print("p");
      break;
      case AppLifecycleState.resumed:
      print("r");
      break;
      case AppLifecycleState.inactive:
      print("I");
      break;
      case AppLifecycleState.detached:
      print("D");
      break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Sync Shop',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
      body: IndexedStack(
        index: _selectedPage,
        children: _pageOptions,
      ),
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: _selectedPage,
          onTap: (int index) {
            setState((){
      _selectedPage = index;
            });
          },
          items: [
          BottomNavigationBarItem(icon: Image.asset("assets/home.png",
            height: 25,
            width: 25,
          ),
          title: Text("Home",),
          activeIcon:
          Image.asset("assets/home-active.png",
            height: 25,
            width: 25,
          ) 
          ),
          BottomNavigationBarItem(icon: Image.asset("assets/categories.png",
            height: 22,
            width: 22,
          ),
          title: Text("Categories",),
          activeIcon:
          Image.asset("assets/categories-active.png",
            height: 22,
            width: 22,
          ), 
          ),
          BottomNavigationBarItem(icon: Image.asset("assets/deals.png",
            height: 25,
            width: 25,
          ),
          title: Text("Deals",),
          activeIcon: 
            Image.asset("assets/deals-active.png",
            height: 25,
            width: 25,
          )
          ),
          BottomNavigationBarItem(icon: Image.asset("assets/profile.png",
            height: 25,
            width: 25,
          ), title: Text("Profile",),
          activeIcon: 
         Image.asset("assets/profile-active.png",
            height: 25,
            width: 25,
          )
          ),
          BottomNavigationBarItem(icon: Image.asset("assets/cart.png",
            height: 25,
            width: 25,
          ), title: Text("Cart"),
          activeIcon:
          Image.asset("assets/cart-active.png",
            height: 25,
            width: 25,
          ),
          ),
          ],

          unselectedLabelStyle: TextStyle(color: Colors.grey[600], fontSize: 11),
          selectedItemColor: Color(0xFFEF5021),
          selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold, fontSize: 12),
          ),
      ),
      );
  }
}
类MyApp扩展StatefulWidget{
@凌驾
状态createState(){
返回MyAppState();
}
}
类MyAppState使用WidgetsBindingObserver扩展状态{
最后加载的变量;
布尔是主动的;
int _selectedPage=0;
最终页面选项=[
首页(),
CategoriesPage(),
交易(),
ProfileAccount(),
CartPage(),
];
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
@凌驾
无效处置(){
WidgetsBinding.instance.removeObserver(此);
super.dispose();
}
@凌驾
void didchangeAppifecyclestate(AppLifecycleState状态){
super.didChangeAppLifecycleState(州);
开关(状态){
案例AppLifecycleState.paused:
印刷品(“p”);
打破
案例AppLifecycleState.resumed:
印刷品(“r”);
打破
案例AppLifecycleState.inactive:
印刷品(“I”);
打破
案例AppLifecycleState.0:
印刷品(“D”);
打破
}
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
标题:“同步商店”,
主题:主题数据(
主样本:颜色。蓝色,
),
家:脚手架(
正文:IndexedStack(
索引:\ u所选页面,
子项:_pageOptions,
),
底部导航栏:底部导航栏(
类型:BottomNavigationBarType.fixed,
currentIndex:_selectedPage,
onTap:(int索引){
设置状态(){
_selectedPage=索引;
});
},
项目:[
BottomNavigationBarItem(图标:Image.asset(“assets/home.png”),
身高:25,
宽度:25,
),
标题:文本(“主页”),
活动图标:
Image.asset(“assets/home-active.png”,
身高:25,
宽度:25,
) 
),
BottomNavigationBarItem(图标:Image.asset(“assets/categories.png”),
身高:22,
宽度:22,
),
标题:文本(“类别”),
活动图标:
Image.asset(“assets/categories active.png”,
身高:22,
宽度:22,
), 
),
BottomNavigationBarItem(图标:Image.asset(“assets/deals.png”),
身高:25,
宽度:25,
),
标题:文本(“交易”),
活动图标:
Image.asset(“assets/deals active.png”,
身高:25,
宽度:25,
)
),
BottomNavigationBarItem(图标:Image.asset(“assets/profile.png”),
身高:25,
宽度:25,
),标题:文本(“简介”),
活动图标:
Image.asset(“assets/profile active.png”,
身高:25,
宽度:25,
)
),
BottomNavigationBarItem(图标:Image.asset(“assets/cart.png”),
身高:25,
宽度:25,
),标题:文本(“购物车”),
活动图标:
Image.asset(“assets/cart active.png”,
身高:25,
宽度:25,
),
),
],
未选择的标签样式:文本样式(颜色:Colors.grey[600],字体大小:11),
选择EditemColor:Color(0xFFEF5021),
selectedLabelStyle:TextStyle(fontWeight:fontWeight.bold,fontSize:12),
),
),
);
}
}
在子页面中(页面选项列表中的那些元素)

将函数添加到“On enter”的initState中,并将函数添加到“On leave”的dispose中

范例

首页.dart

class _homeState extends State<HomePage>{
   @override
   void initState(){
     if(this.mounted){
        *your_function_here*
     }
     super.initState();
   }

   @override
   void dispose(){
      if(this.mounted){
        *your_onleave_function_here*
     }
      super.dispose();
   }
}
class\u homeState扩展状态{
@凌驾
void initState(){
如果(本安装){
*你的函数在这里*
}
super.initState();
}
@凌驾
无效处置(){
如果(本安装){
*您的_onleave _函数*
}
super.dispose();
}
}
在子页面中(页面选项列表中的那些元素)

将函数添加到“On enter”的initState中,并将函数添加到“On leave”的dispose中

范例

首页.dart

class _homeState extends State<HomePage>{
   @override
   void initState(){
     if(this.mounted){
        *your_function_here*
     }
     super.initState();
   }

   @override
   void dispose(){
      if(this.mounted){
        *your_onleave_function_here*
     }
      super.dispose();
   }
}
class\u homeState扩展状态{
@凌驾
void initState(){
如果(本安装){
*你的函数在这里*
}
super.initState();
}
@凌驾
无效处置(){
如果(本安装){
*您的_onleave _函数*
}
super.dispose();
}
}

您的页面是否在单独的文件(.dart)中?@Naveen Avidi yesIt可能会帮助您!您的页面是否在单独的文件(.dart)中?@Naveen Avidi yesIt可能会帮助您!您正在谈论的js函数中有哪些我应该添加到我的子页面中?(我对kimda感到困惑)刷新的那一个,你不需要WidgetSBindingobserver,你在说什么js函数,我应该添加到我的子页面?(我对kimda感到困惑)刷新的那个,你不需要WidgetsBindingObserver