Home > Mobile >  how do i move from the first page, to another page by using items from the first page
how do i move from the first page, to another page by using items from the first page

Time:08-10

enter image description here

how do i move from the first page, to another page by using items from the first page

I am using pageview on the page menu, can someone help me

CodePudding user response:

So basically you can create a variable that contains the value on your first page and while you add your pages in any list you can simply pass that value like

int test=0
  final List<Widget> _list = [];
  @override
  void initState() {
    super.initState();
    _list.addAll(
        const [DashboardScreen(test), WishListScreen(),   CartScreen(), ProfileScreen()]);
  }

And other side in dashboardscreen

  final int test;
  const DashboardScreen(this.test, {Key? key}) : super(key: key);

if your page is statful widget then in build file you can simple use widget.test or if you have stateless widget you can directly use test variable

CodePudding user response:

You can use page controller

// through the page controller you can do using animateToPage or _pageController.jumpToPage(0). here 0 is page index

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: MyPageView(),
    );
  }
}

class MyPageView extends StatefulWidget {
  const MyPageView({Key? key}) : super(key: key);

  @override
  State<MyPageView> createState() => _MyPageViewState();
}

class _MyPageViewState extends State<MyPageView> {
  final PageController _pageController = PageController();

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: PageView(
          controller: _pageController,
          children: <Widget>[
            Container(
              color: Colors.red,
              child: Center(
                child: ElevatedButton(
                  onPressed: () {
                    if (_pageController.hasClients) {
                      
                      _pageController.animateToPage(
                        1,
                        duration: const Duration(milliseconds: 400),
                        curve: Curves.easeInOut,
                      );
                      
                      // you can use animateToPage or _pageController.jumpToPage(0). here 0 is page index
                    }
                  },
                  child: const Text('Next'),
                ),
              ),
            ),
            Container(
              color: Colors.blue,
              child: Center(
                child: ElevatedButton(
                  onPressed: () {
                    if (_pageController.hasClients) {
                      _pageController.animateToPage(
                        0,
                        duration: const Duration(milliseconds: 400),
                        curve: Curves.easeInOut,
                      );
                    }
                  },
                  child: const Text('Previous'),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
  • Related