Flutter:如何通过 GetxController 控制 PageView?

Flutter: How to control a PageView by GetxController?

主题:PageView 和 GetX

我无法从 HomeView 模块分离 PageView widget 上的控件。我有一个 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);

}

然后是控制器

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)
  ];

}

然后我做的视图就是这样

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,
                          ),
                          ),
                        ],
                      ),
                      ),
                    ),
                  ),
                )
            ],
          ),
        ),
    ),
        ),
      ),
    );
  }
}