flutter 当我用谷歌导航栏在页面之间导航时,我想重新加载页面,就像我第一次打开它一样

pkwftd7m  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(69)

屏幕和它下面有控制器当我导航第一次页面加载,但当我导航第二次它不像第一次重新加载我面临这个问题,因为要从API请求数据每次我打开页面控制器是下面的屏幕代码苏任何想法

Widget build(BuildContext context) {
    Get.put(MainpageConrolloerImp());
    return GetBuilder<MainpageConrolloerImp>(
        builder: (controller) => Scaffold(
            drawer: NavigationDrawer(
              name: thecontrol.thename,
            ),
            body: controller.pageslist.elementAt(controller.currentpage),
            bottomNavigationBar: Container(
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(16),
                      topRight: Radius.circular(16)),
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      blurRadius: 20,
                      color: Colors.black.withOpacity(.1),
                    )
                  ],
                ),
                child: SafeArea(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 4.0, vertical: 16),
                    child: GNav(
                      tabBorderRadius: 24,
                      rippleColor: Colors.grey[300]!,
                      hoverColor: Colors.grey[100]!,
                      gap: 4,
                      activeColor: const Color.fromRGBO(227, 29, 26, 1),
                      iconSize: 24,
                      padding: const EdgeInsets.symmetric(
                          horizontal: 12, vertical: 12),
                      duration: const Duration(milliseconds: 500),
                      tabBackgroundColor:
                          const Color.fromRGBO(227, 29, 26, 0.15),
                      color: Colors.grey,
                      tabs: [
                        GButton(
                          onPressed: () {
                            controller.navigationPage(0);
                          },
                          icon: Icons.home,
                          text: '',
                        ),
                       
                      ],
                    ),
                  ),
                ))));
  }
                          
 ////////////////////////

abstract class MainpageConrolloer extends GetxController {
  navigationPage(int currentpage);
}

class MainpageConrolloerImp extends MainpageConrolloer {
  int currentpage = 0;
  List<Widget> pageslist = [
    const HomePage(),
    const cartPage(),
    const CategoryPage(),
    const FavoritePage(),
    const MaintainPage(),
  ];

  @override
  navigationPage(int i) {
    currentpage = i;
    update();
  }

}
tzcvj98z

tzcvj98z1#

你可以让你的主页和其他页面变成StatefulWidget。提供initState方法-〉每次在页面之间导航时,它都会再次调用initState

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }

  @override
  void initState() {
    super.initState();
    print("init home page");
  }
}

class CartPage extends StatefulWidget {
  const CartPage({super.key});

  @override
  State<CartPage> createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  @override
  void initState() {
    super.initState();
    print("init cart page");
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

如果你坚持使用无状态-〉请提供Gnav包的onTabChanged函数

onTabChange: (index) {
            if (index == 1) {
             // Do Cart's work  
            }
            else if (index ==0) {
             //do home's work
            }
},

相关问题