如何使ElevatedButton导航到BottomNavigationBar上的第二页?

huangapple go评论67阅读模式
英文:

How I can make the ElevatedButton navigate to a second page on the BottomNavigationBar?

问题

我的代码问题在于它确实跳转到了“Dashbored()”页面,但图标仍然属于主页,而且只有一个页面。

home_page.dart

ElevatedButton(
  style: ElevatedButton.styleFrom(
    alignment: Alignment.center,
    fixedSize: const Size(160, 50),
    backgroundColor: const Color.fromARGB(255, 0, 93, 186),
    foregroundColor: const Color.fromARGB(255, 69, 164, 243),
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(28),
    ),
  ),
  onPressed: () {
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder: (context) => const Layout(children: {1: Dashbored()}),
      ),
    );
  },
  child: const Text(
    "START ALGORITHM",
    style: TextStyle(
      color: Colors.white,
      fontWeight: FontWeight.bold,
      fontSize: 14,
    ),
  ),
),

main.dart

class MyApp extends StatelessWidget {
  const MyApp({Key? key});

  @override
  Widget build(BuildContext context) {
    Map<int, Widget> pages = {
      0: const HomePage(),
      1: const Dashbored(),
      2: const Products(),
      3: const Profile(),
    };
    return Layout(
      children: pages,
    );
  }
}

Layout.dart

class Layout extends StatefulWidget {
  const Layout({Key? key, required this.children});
  final Map<int, Widget> children;

  @override
  State<Layout> createState() => _LayoutState();
}

class _LayoutState extends State<Layout> {
  int pageIndex = 0;
  onNavigationChanged(int index) {
    setState(() {
      pageIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const MyAppBar(),
      endDrawer: const MyDrawer(),
      bottomNavigationBar: MyNavigationBar(
        onNavigationChanged: onNavigationChanged,
        pageIndex: pageIndex,
      ),
      body: widget.children[pageIndex],
    );
  }
}

我期望当我按下按钮时,能够导航到BottomNavigationBar的第二个索引页,而不会丢失其他页面。

你可以使用以下方式使ElevatedButton导航到BottomNavigationBar的第二个页面:

onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => Layout(children: {1: Dashbored()}),
    ),
  );
},

这将使用Navigator.push方法而不是Navigator.pushReplacement,以便您可以返回到前一个页面而不会丢失其他页面。

英文:

The problem with my code is it does go to Dashbored() page but the icon belong to the home page and it has only one page.

home_page.dart

ElevatedButton(
                      
                      style: ElevatedButton.styleFrom(alignment: Alignment.center,
                        fixedSize: const Size(160, 50),
                        backgroundColor:
                            const Color.fromARGB(255, 0, 93, 186),
                        foregroundColor:
                            const Color.fromARGB(255, 69, 164, 243),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(28),
                        ),
                      ),
                      onPressed: () {
                        Navigator.pushReplacement(
                            context,
                            MaterialPageRoute(
                              builder: (context) =&gt; const Layout(children: {1:Dashbored()}),
                            ));
                      },
                      child: const Text(
                        &quot;START ALGORITHM&quot;,
                        style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            fontSize: 14),
                      ),
                    ),

main.dart

class myapp extends StatelessWidget {
  const myapp({super.key});

  @override
  Widget build(BuildContext context) {
    Map&lt;int,Widget&gt; pages = {
      0:const HomePage(),
      1:const Dashbored(),
      2:const Products(),
      3:const Profile(),
    };
    return Layout(
      children: pages,
    );
  }
}

Layout.dart

class Layout extends StatefulWidget {
  const Layout({super.key, required this.children});
  final Map&lt;int,Widget&gt; children;

  @override
  State&lt;Layout&gt; createState() =&gt; _LayoutState();
}

class _LayoutState extends State&lt;Layout&gt; {
  int pageIndex = 0;
  onNavigationChanged(int index) {
    setState(() {
      pageIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    // int index =1;
    return Scaffold(
      appBar: const MyAppBar(),
      endDrawer: const MyDrawer(),
      bottomNavigationBar: MyNavigationBar(
        onNavigationChanged: onNavigationChanged,
        pageIndex: pageIndex,
      ),
      body: widget.children[pageIndex],
    );
  }
}

I expect when I press the button should navigate to the second index in the BottomNavigationBar without lose the other pages.

How I can make the ElevatedButton navigate to a second page on the BottomNavigationBar?

答案1

得分: 0

这个按钮应该调用这个函数:

onNavigationChanged(int index) {
  setState(() {
    pageIndex = index;
  });
}

但你正在使用Navigator,这是完全不同的,它们没有关联,因为你是手动更改屏幕的。
解决方案是将我提到的函数传递给你创建的MyNavigationBar(),然后在那里使用所需的索引触发该函数。

英文:

Bro this button is supposed to call this function :

onNavigationChanged(int index) {
    setState(() {
      pageIndex = index;
    });
  }

But you are using Navigator which is totally different and they are not related, because you are changing the screens manually.
The solution is to pass down the function i mentioned to MyNavigationBar() that you created and there trigger the function with the index you want on each button.

huangapple
  • 本文由 发表于 2023年6月6日 17:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定