Flutter:如何通过GetxController控制PageView?

标签 flutter model-view-controller flutter-pageview flutter-getx

主题:PageView 和 GetX

我在将 PageView 小部件 上的控件与 HomeView 模块分离时遇到问题。我有一个 GlobalController 及其各自的 GlobalBinding,它们在打开 HomeView 时实例化。我想将 setPage(int page) 方法带到 GlobalController 中,最终使 HomeView 的 PageView 更改页面。我不知道如何将 PageControllerPageView 获取到 GlobalController 以便使其工作。我应该如何进行?

最佳答案

类似这样的事情吗? 我在入职时使用综合浏览量

class Onboard{


 final headTitle;
  final secondarytitle;
  final discription;
  final pngimage;
 Onboardslist(this.headTitle, this.secondarytitle, this.discription, this.pngimage);

}

然后是 Controller

class OnboardController extends GetxController{


  var selectedPagexNumber = 0.obs;
bool get isLastPage => selectedPagexNumber.value == onBoardPages.length -1;
var pageControll = PageController();


   forwardAct()
  {
    if(isLastPage) Get.offNamedUntil(signin, (route)=> false);

    else pageControll.nextPage(duration: 300.milliseconds, curve: Curves.ease);
  }

  List<Onboardslist> onBoardPages = 
  [
    Onboardslist("title",
    "short description",
    "long description",
    imageString),
     Onboardslist("title",
    "short description",
    "long description",
    imageString),
    Onboardslist("title",
    "short description",
    "long description",
    imageString),
    Onboardslist("title",
    "short description",
    "long description",
    imageString)
  ];

}

然后我做的 View 就是这样

class Onboarding extends StatelessWidget {

   final yourController= OnboardController();

  @override
  Widget build(BuildContext context) {
    SizeXGet().init(context);
     return Scaffold(
       backgroundColor: decent_white,
       appBar: AppBarCustom(
        title: 'Skip',
        button: ()=>Get.offNamedUntil(signin,(route)=>false),
      ),
      body: WillPopScope(
        onWillPop: () async => false,
          child: SafeArea(
            child: Stack(
              children: [
                PageView.builder(
                  controller: yourController.pageControll,
                  onPageChanged: yourController.selectedPagexNumber,
                  itemCount: yourController.onBoardPages.length,
                  itemBuilder: (context, index)
                  =>Padding(
                    padding: const EdgeInsets.only(left: 10,right: 10),
                    child: Container(
                      child:  SingleChildScrollView(
                          physics: BouncingScrollPhysics(),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                            Container(
                              height: getHeight(150),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                            mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                     Padding(
                       padding: const EdgeInsets.only(left: 20,right: 20),
                       child: Text(yourController.onBoardPages[index].headTitle,
                       style: TextStyle(
                       color: darkish_color,
                        fontSize: getHeight(20),
                       fontFamily: 'Metropolis-SemiBold' ,
                       fontWeight: FontWeight.bold
                       ),),
                     ),
                     SizedBox(height: 15,),
                     Padding(
                     padding: const EdgeInsets.only(left: 50,right: 50),
                     child: Text(yourController.onBoardPages[index].secondarytitle,
                     style: TextStyle(
                                  color: not_sopure_black,
                                  fontSize: getHeight(26),
                                  fontFamily: 'Metropolis-Bold' ,
                                  fontWeight: FontWeight.bold
                                ),
                                ),
                              ),
                              SizedBox(height: 15,),
                              Padding(
                               padding: const EdgeInsets.only(left: 40,right: 40),
                                child: Text(yourController.onBoardPages[index].discription,
                                style: TextStyle(
                                  color: not_sopure_black,
                                  fontSize: getHeight(15),
                                  fontFamily: 'Metropolis-Regular' ,
                                ),
                                ),
                              ),
                                  ],
                                ),
                              ),
                              SizedBox(height: 15,),

                              Image.asset(yourController.onBoardPages[index].pngimage),
                             
                              
                              
                            ],
                          ),
                        ),
                    ),
                  ),
                ),
             
              ],
            ),
          
        ),
        
      ),
      bottomNavigationBar: BottomAppBar(
        color: Colors.transparent,
        elevation: 0,
        child: Container(
          height: 75,
          width: MediaQuery.of(context).size.width,
          child:  Padding(
        padding: const EdgeInsets.only(left: 25,right:25,),
        child: Container(
          width: MediaQuery.of(context).size.width,
          child: Stack(
            children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Container(
                      child: Row(
                        children: List.generate(yourController.onBoardPages.length,
                        (index)=>Obx(()=>
                        AnimatedContainer(
                          duration: Duration(milliseconds: 200),
                          margin: EdgeInsets.only(right: 5),
                          height: 10,
                          width: yourController.selectedPagexNumber.value == index ? 20 : 10,
                              decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(60),
                              color: yourController.selectedPagexNumber.value == index
                              ? darkish_color
                              : not_sopure_black,),
                        ),
                        )),
                      ),
                    ),
                  ),
                
                Align(
                  alignment: Alignment.centerRight,
                  child: Container(
                    width: 130,
                    height: 52,
                    child: RaisedButton(
                      elevation: 0,
                      onPressed: yourController.forwardAct,
                      splashColor: not_sopure_black,
                      color: darkish_color,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(100)
                      ),
                      child: Obx(() =>
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(yourController.isLastPage ? 'Next' : 'Next',
                          style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'Metropolis-Semibold',
                            fontSize: 16,
                          ),
                          ),
                        ],
                      ),
                      ),
                    ),
                  ),
                )
            ],
          ),
        ),
    ),
        ),
      ),
    );
  }
}

关于Flutter:如何通过GetxController控制PageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67853858/

相关文章:

android - 在适当的外部 map 应用程序中打开 KML/GPX 文件

Flutter Streams 的使用

function - self 更新的 Flutter 模型

flutter - 阻止 PageView 从 ScrollNotification 监听器 Flutter

flutter - 在 Flutter 中处理 Stateful 小部件后,const 实例会保留在内存中吗?

java - 命令行java中的MVC

asp.net - 尝试使用 Paypal API 时出现错误 500.19

javascript - jsblocks更新数据更新 View

android - 在 flutter 中如何使带有平移手势的内部小部件不会与PageView冲突

flutter - Flutter 中的 PageView 模块中的 PageController 未正确设置。弹出位置非空错误