Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.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
Flutter 在颤振中使用PageTransitionSwitcher时,如何保持每个页面的状态?_Flutter_Flutter Animation - Fatal编程技术网

Flutter 在颤振中使用PageTransitionSwitcher时,如何保持每个页面的状态?

Flutter 在颤振中使用PageTransitionSwitcher时,如何保持每个页面的状态?,flutter,flutter-animation,Flutter,Flutter Animation,我正在尝试将库添加到我的项目中,但遇到了问题。 前面的代码是这样的 Widget _buildScaffold(BuildContext context) { return Scaffold( body: PageView.builder(itemBuilder: (context,index)=>_pageList[index]), extendBody: true, bottomNavigationBar: Opacity( opacity: 0.98,

我正在尝试将库添加到我的项目中,但遇到了问题。 前面的代码是这样的

  Widget _buildScaffold(BuildContext context) {
return Scaffold(
  body: PageView.builder(itemBuilder: (context,index)=>_pageList[index]),
  extendBody: true,
  bottomNavigationBar: Opacity(
    opacity: 0.98,
    child: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Theme.of(context).accentColor,
        currentIndex: index,
        onTap: (index) {
          setState(() {
            this.index = index;
          });
          // _pageController.jumpToPage(index);
        },
        items: _bottomList),
  ),
);
} 当我将
PageView.builder
更改为
PageTransitionSwitcher
时,根据示例,child的值为
\u pageList[index]
,动画可用,切换页面时状态丢失。当我尝试将
PageView
作为子项使用时,可以设置状态
KeepAlive
,但动画会消失(因为它被判断为同一个小部件)

Widget\u buildScaffold(BuildContext上下文){
返回脚手架(
正文:页面转换开关(
transitionBuilder:(
孩子,
动画初级动画,
动画二级动画,
) {
回程过渡(
动画:primaryAnimation,
第二动画:第二动画,
孩子:孩子,
);
},
子项:PageView.builder(itemBuilder:(u,index)=>pageList[index]),
),
扩展体:是的,
底部导航栏:不透明度(
不透明度:0.98,
子项:底部导航栏(
类型:BottomNavigationBarType.fixed,
selectedItemColor:Theme.of(context).accentColor,
当前索引:索引,
onTap:(索引){
设置状态(){
这个指数=指数;
});
//_pageController.jumpToPage(索引);
},
项目:[
底部导航气压计(
图标:图标(Icons.home),标题:文本(I18n.of(context.home)),
底部导航气压计(
图标:图标(
CustomIcons.leaderboard,
),
标题:文本(I18n.of(context.rank)),
底部导航气压计(
图标:图标(Icons.favorite),
标题:文本(I18n.of(context).quick_view),
底部导航气压计(
图标:图标(Icons.search),
标题:文本(I18n.of(context.search)),
底部导航气压计(
图标:图标(图标.设置),
标题:文本(I18n.of(context.setting)),
]),
),
);
}

那么,有没有一种方法可以同时保留页面的动画和状态呢?

我就是这么做的

在有状态小部件中,如果在页面中显示项目列表,可以在要保留的列表前面添加静态变量,以便在切换页面时不会重新获取

如果您当前正在使用FutureBuilder,我认为您必须改为使用它,因为您需要引用一个静态列表

  static List<YourObject> yourList = new List();


  @override
  void initState() {
    super.initState();
    //some async task to fetch data and populate yourList
    fetchData();
  }



  @override
  Widget build(BuildContext context) {
    if(yourList.length == 0){
      return CircularProgressIndicator();
    }else{
      return ListView.builder(){
        itemCount: yourList.length,
        itemBuilder: (BuildContext context, int index){
          ...yourcodehere
        }
      }
    }
  }
静态列表yourList=新列表();
@凌驾
void initState(){
super.initState();
//一些异步任务来获取数据并填充列表
fetchData();
}
@凌驾
小部件构建(构建上下文){
如果(yourList.length==0){
返回循环ProgressIndicator();
}否则{
返回ListView.builder(){
itemCount:yourList.length,
itemBuilder:(构建上下文,int索引){
…你的代码在这里
}
}
}
}
以下是我所做的

在有状态小部件中,如果在页面中显示项目列表,可以在要保留的列表前面添加静态变量,以便在切换页面时不会重新获取

如果您当前正在使用FutureBuilder,我认为您必须改为使用它,因为您需要引用一个静态列表

  static List<YourObject> yourList = new List();


  @override
  void initState() {
    super.initState();
    //some async task to fetch data and populate yourList
    fetchData();
  }



  @override
  Widget build(BuildContext context) {
    if(yourList.length == 0){
      return CircularProgressIndicator();
    }else{
      return ListView.builder(){
        itemCount: yourList.length,
        itemBuilder: (BuildContext context, int index){
          ...yourcodehere
        }
      }
    }
  }
静态列表yourList=新列表();
@凌驾
void initState(){
super.initState();
//一些异步任务来获取数据并填充列表
fetchData();
}
@凌驾
小部件构建(构建上下文){
如果(yourList.length==0){
返回循环ProgressIndicator();
}否则{
返回ListView.builder(){
itemCount:yourList.length,
itemBuilder:(构建上下文,int索引){
…你的代码在这里
}
}
}
}