Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : Open Page from page inside bottomnavigationview

Tags:

flutter

dart

i want to open profile page from home page without press 'profile' button inside bottomnavigation item. Thanks for help. this is a main class of my project. i put all page here and i already import all page here.

MainClass

int _currentIndex = 0;
    final List<Widget> _children = [
        HomePage(),
        MessagePage(),
        ProfilePage()
    ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
     appBar: AppBar(
       title: Text('My Flutter App'),
     ),
    body: _children[_currentIndex],
     bottomNavigationBar: BottomNavigationBar(
       currentIndex: _currentIndex,
       onTap: onTabTapped,
       items: [
         BottomNavigationBarItem(
           icon: new Icon(Icons.home),
           title: new Text('Home'),

         ),
         BottomNavigationBarItem(
           icon: new Icon(Icons.mail),
           title: new Text('Messages'),
         ),
         BottomNavigationBarItem(
           icon: Icon(Icons.person),
           title: Text('Profile')
         )
       ],
     ),
   );
 }

and this my home page. this page same as profile and message page. i need to open profile page from home page without pressing profile menu in bottom. i just wanna press profile button inside home page.

HomePage

import 'package:flutter/material.dart';
import 'package:navigation/profile_page.dart';

class HomePage extends StatefulWidget {
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: <Widget>[
            Text("Home Page"),
            RaisedButton(
              child: Text("GO TO PROFILE PAGE"),
              onPressed: (){

                );
              },
            )
          ],
        ),
      ),
    );
  }
}

enter image description here

like image 344
Cong Fandi Avatar asked Apr 11 '26 09:04

Cong Fandi


1 Answers

This can be done by directly calling the class name. For example consider Profile() be the class extending statefull class.

Home class

....

int _index = 1;

  final options = [
    Text('Home'),
    Text('Message'),
    Profile() //directly call the class to load
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("My flutter app"),
      ),
      body: Center(
        child: options.elementAt(_index),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          new BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Home'),
          ),
          new BottomNavigationBarItem(
            icon: Icon(Icons.message),
            title: Text('message'),
          ),
          new BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text('profile')
          )
        ],
        currentIndex: _index,
        onTap: _onTapped,
      ),
    );
  }

  void _onTapped(int index) {
    setState(() {
      _index = index;
    });
  }

Similarly you can load other classes too. hope it helped:)

like image 137
Yogesh kataria Avatar answered Apr 13 '26 02:04

Yogesh kataria



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!