栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何以编程方式在Flutter中选择BottomNavigationBar选项卡,而不是内置的onTap回调?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何以编程方式在Flutter中选择BottomNavigationBar选项卡,而不是内置的onTap回调?

这是有关如何实现所需目标的完整示例。

import 'package:flutter/material.dart';void main() => runApp(new MyApp());const String page1 = "Page 1";const String page2 = "Page 2";const String page3 = "Page 3";const String title = "BNB Demo";class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return new MaterialApp(      title: title,      home: new MyHomePage(title: title),    );  }}class MyHomePage extends StatefulWidget {  MyHomePage({Key key, this.title}) : super(key: key);  final String title;  @override  _MyHomePageState createState() => new _MyHomePageState();}class _MyHomePageState extends State<MyHomePage> {  List<Widget> _pages;  Widget _page1;  Widget _page2;  Widget _page3;  int _currentIndex;  Widget _currentPage;  @override  void initState() {    super.initState();    _page1 = Page1();    _page2 = Page2();    _page3 = Page3();    _pages = [_page1, _page2, _page3];    _currentIndex = 0;    _currentPage = _page1;  }  void changeTab(int index) {    setState(() {      _currentIndex = index;      _currentPage = _pages[index];    });  }  @override  Widget build(BuildContext context) {    return new Scaffold(      appBar: new AppBar(        title: new Text(widget.title),      ),      body: _currentPage,      bottomNavigationBar: BottomNavigationBar(          onTap: (index) => changeTab(index),          currentIndex: _currentIndex,          items: [ BottomNavigationBarItem(     title: Text(page1), icon: Icon(Icons.account_circle)), BottomNavigationBarItem(     title: Text(page2), icon: Icon(Icons.account_circle)), BottomNavigationBarItem(     title: Text(page3), icon: Icon(Icons.account_circle))          ]),      drawer: new Drawer(        child: new Container(          margin: EdgeInsets.only(top: 20.0),          child: new Column( children: <Widget>[   navigationItemListTitle(page1, 0),   navigationItemListTitle(page2, 1),   navigationItemListTitle(page3, 2), ],          ),        ),      ),    );  }  Widget navigationItemListTitle(String title, int index) {    return new ListTile(      title: new Text(        title,        style: new TextStyle(color: Colors.blue[400], fontSize: 22.0),      ),      onTap: () {        Navigator.pop(context);        changeTab(index);      },    );  }}class Page1 extends StatelessWidget {  @override  Widget build(BuildContext context) {    return Center(      child: Text(page1),    );  }}class Page2 extends StatelessWidget {  @override  Widget build(BuildContext context) {    return Center(      child: Text(page2),    );  }}class Page3 extends StatelessWidget {  @override  Widget build(BuildContext context) {    return Center(      child: Text(page3),    );  }}

每当您想更改为标签页时,

changeTab(YOUR_TAB_INDEX)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406988.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号