2021年1月23日星期六

How to replace initState in Stateful Widget with provider mode and replace stateless to stateful

How to use initState() in provider mode instead of using statefull widget,

this is the provider class I have:

import 'package:flutter/material.dart';    class NavigationBBarData extends ChangeNotifier {    int currentIndex = 0;    PageController pageController;      final GlobalKey<FormFieldState<String>> orderFormKey = GlobalKey();      void pageChanged(int index) {      currentIndex = index;      notifyListeners();    }      void animateToPage() {      pageController.animateToPage(currentIndex,          duration: Duration(milliseconds: 500), curve: Curves.linear);      notifyListeners();    }  }  

and this is the Stateful widget I have which contains the initState() method:

import 'package:flutter/material.dart';  import 'package:ipetv1/providers/navigation_bbar_data.dart';  import 'package:ipetv1/widgets/ipet_bottom_nav_bar.dart';  import 'package:ipetv1/widgets/ipet_page_view.dart';  import 'package:provider/provider.dart';    class IPetNavHomeScreen extends StatefulWidget {    @override    _IPetNavHomeScreenState createState() => _IPetNavHomeScreenState();  }    class _IPetNavHomeScreenState extends State<IPetNavHomeScreen> {    @override    void initState() {      Provider.of<NavigationBBarData>(context, listen: false).pageController =          PageController(        initialPage:            Provider.of<NavigationBBarData>(context, listen: false).currentIndex,        keepPage: true,      );      super.initState();    }      @override    Widget build(BuildContext context) {      return Stack(        children: [          IPetPageView(),          Positioned(            left: 0,            right: 0,            bottom: 0,            child: IPetBottomNavBar(),          ),        ],      );    }  }  

So I want to use here the stateless widget instead of the Stateful one using provider statemanagement..

https://stackoverflow.com/questions/65866262/how-to-replace-initstate-in-stateful-widget-with-provider-mode-and-replace-state January 24, 2021 at 09:18AM

没有评论:

发表评论