Flutter – 如何通过曲线导航栏导航到不同页面

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

Flutter- how to navigate different pages by curved navigation bar

问题

我正在创建一个Flutter应用程序,并需要帮助如何导航到不同的页面。此外,当我点击曲线导航栏的按钮时,图标不会更改。

你能帮助我吗?

以下是我的曲线导航栏的代码:

import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

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

  @override
  State<BAR> createState() => _BARState();
}

class _BARState extends State<BAR> {
  @override
  Widget build(BuildContext context) {
    return CurvedNavigationBar(
      backgroundColor: HexColor("#F7ECCD"),
      color: HexColor("#AE621C"),
      items: [
        Icon(
          Icons.home,
          color: Colors.white,
          size: 30,
        ),
        Icon(
          Icons.favorite,
          color: Colors.white,
          size: 30,
        ),
        Icon(
          Icons.settings,
          color: Colors.white,
          size: 30,
        )
      ],
      onTap: (index) {
        setState(() {
          if (index == 0) {
            Navigator.pushNamed(context, '/');
          }

          if (index == 1) {
            Navigator.pushNamed(context, '/one');
          }
          if (index == 2) {
            Navigator.pushNamed(context, '/two');
          }
        });
      },
    );
  }
}

谢谢!

英文:

i am creating a flutter app and want help on how to navigate to different pages. Also, when i tap on buttons of curved navigation bar nothing happens the icons not changing.

can u please help me out?

here is my code for the curved navigation bar

Thank u

import &#39;package:flutter/material.dart&#39;;
import &#39;package:hexcolor/hexcolor.dart&#39;;

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

  @override
  State&lt;BAR&gt; createState() =&gt; _BARState();
}

class _BARState extends State&lt;BAR&gt; {
  @override
  Widget build(BuildContext context) {
    return CurvedNavigationBar(
          backgroundColor: HexColor(&quot;#F7ECCD&quot;),
          color: HexColor(&quot;#AE621C&quot;),
          items: [
            Icon(
              Icons.home,
              color: Colors.white,
              size: 30,
            ),
            Icon(
              Icons.favorite,
              color: Colors.white,
              size: 30,
            ),
            Icon(
              Icons.settings,
              color: Colors.white,
              size: 30,
            )
          ],
          onTap: (index){

            setState(() {
                if (index == 0) {
                

                Navigator.pushNamed(context, &#39;/&#39;);

              }

              if (index == 1) {
                
                Navigator.pushNamed(context, &#39;/one&#39;);
                

              }
              if (index == 2) {
            
                Navigator.pushNamed(context, &#39;/two&#39;);

              }
            });

          },);
  }
}



</details>


# 答案1
**得分**: 1

```dart
import 'package:flutter/cupertino.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _selectedIndex = 0;

  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Home',
      style: optionStyle,
    ),
    Text(
      'Index 1: Business',
      style: optionStyle,
    ),
    Text(
      'Index 2: School',
      style: optionStyle,
    ),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoTabScaffold(
      tabBar: CupertinoTabBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.home),
            title: Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.business),
            title: Text('Business'),
          ),
          BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.school),
            title: Text('School'),
          ),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        backgroundColor: CupertinoColors.systemGrey,
        activeColor: CupertinoColors.black,
      ),
      tabBuilder: (BuildContext context, int index) {
        return CupertinoTabView(
          builder: (BuildContext context) {
            return CupertinoPageScaffold(
              child: Center(
                child: _widgetOptions.elementAt(index),
              ),
            );
          },
        );
      },
    );
  }
}
英文:
import &#39;package:flutter/cupertino.dart&#39;;

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  int _selectedIndex = 0;

  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List&lt;Widget&gt; _widgetOptions = &lt;Widget&gt;[
    Text(
      &#39;Index 0: Home&#39;,
      style: optionStyle,
    ),
    Text(
      &#39;Index 1: Business&#39;,
      style: optionStyle,
    ),
    Text(
      &#39;Index 2: School&#39;,
      style: optionStyle,
    ),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoTabScaffold(
      tabBar: CupertinoTabBar(
        items: const &lt;BottomNavigationBarItem&gt;[
          BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.home),
            title: Text(&#39;Home&#39;),
          ),
          BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.business),
            title: Text(&#39;Business&#39;),
          ),
          BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.school),
            title: Text(&#39;School&#39;),
          ),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        backgroundColor: CupertinoColors.systemGrey,
        activeColor: CupertinoColors.black,
      ),
      tabBuilder: (BuildContext context, int index) {
        return CupertinoTabView(
          builder: (BuildContext context) {
            return CupertinoPageScaffold(
              child: Center(
                child: _widgetOptions.elementAt(index),
              ),
            );
          },
        );
      },
    );
  }
}

huangapple
  • 本文由 发表于 2023年1月9日 04:34:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051086.html
匿名

发表评论

匿名网友

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

确定