【问题标题】:Flutter - Open Modal Bottom Sheet on Bottom navigation bar item clickFlutter - 在底部导航栏项目单击上打开模态底部表
【发布时间】:2021-11-27 14:47:24
【问题描述】:

如何从底部导航栏项单击打开底部工作表。这是我当前的页面代码,这是我迄今为止尝试过的,但它似乎不起作用。我已经成功创建了底部导航栏,并且使用了 Page1()、Page2()、Page3() 功能,我可以成功迁移到其他页面,现在我需要第四个项目来打开一个底部工作表,我可以在其中做更多项目.函数 showBottomSheet() 应该能够打开一个底页

class _MyNavigationBarState extends State<MyNavigationBar > {
  int _currentTabIndex = 0;

  @override
  Widget build(BuildContext context) {
    final _kTabPages = <Widget>[
      Page1(),
      Page2(),
      Page3(),
      showBottomSheet()
    ];
    final _kBottmonNavBarItems = <BottomNavigationBarItem>[
      const BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
      const BottomNavigationBarItem(icon: Icon(Icons.network_cell), label: 'Prices'),
      const BottomNavigationBarItem(icon: Icon(Icons.add_circle), label: 'Trade'),
      const BottomNavigationBarItem(icon: Icon(Icons.account_balance_wallet), label: 'Wallet'),
    ];
    assert(_kTabPages.length == _kBottmonNavBarItems.length);
    final bottomNavBar = BottomNavigationBar(
      items: _kBottmonNavBarItems,
      currentIndex: _currentTabIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentTabIndex = index;
        });
      },
    );
    return Scaffold(
      body: _kTabPages[_currentTabIndex],
      bottomNavigationBar: bottomNavBar,

      
     
      ),
    );
  }
}

showBottomSheet(){
      Container _buildBottomSheet(BuildContext context) {
    return Container(
      height: 300,
      padding: const EdgeInsets.all(8.0),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.blue, width: 2.0),
        borderRadius: BorderRadius.circular(8.0),
      ),
      child: ListView(
        children: <Widget>[
          const ListTile(title: Text('Bottom sheet')),
          const TextField(
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              icon: Icon(Icons.attach_money),
              labelText: 'Enter an integer',
            ),
          ),
          Container(
            alignment: Alignment.center,
            child: ElevatedButton.icon(
              icon: const Icon(Icons.save),
              label: const Text('Save and close'),
              onPressed: () => Navigator.pop(context),
            ),
          )
        ],
      ),
    );
  }


}

【问题讨论】:

    标签: flutter flutter-ios flutter-android


    【解决方案1】:

    试试这个:

    class _MyNavigationBarState extends State<MyNavigationBar > {
      int _currentTabIndex = 0;
    
      @override
      Widget build(BuildContext context) {
        final _kTabPages = <Widget>[
          Page1(),
          Page2(),
          Page3(),
          Container(),
        ];
        final _kBottmonNavBarItems = <BottomNavigationBarItem>[
          const BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
          const BottomNavigationBarItem(icon: Icon(Icons.network_cell), label: 'Prices'),
          const BottomNavigationBarItem(icon: Icon(Icons.add_circle), label: 'Trade'),
          const BottomNavigationBarItem(icon: Icon(Icons.account_balance_wallet), label: 'Wallet'),
        ];
        assert(_kTabPages.length == _kBottmonNavBarItems.length);
        final bottomNavBar = BottomNavigationBar(
          items: _kBottmonNavBarItems,
          currentIndex: _currentTabIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (int index) {
            if(index == 3){
              showBottomSheet();
              return;
            }
    
            setState(() {
              _currentTabIndex = index;
            });
          },
        );
        return Scaffold(
          body: _kTabPages[_currentTabIndex],
          bottomNavigationBar: bottomNavBar,
          ),
        );
      }
    }
    
    showBottomSheet(){
          Container _buildBottomSheet(BuildContext context) {
        return Container(
          height: 300,
          padding: const EdgeInsets.all(8.0),
          decoration: BoxDecoration(
            border: Border.all(color: Colors.blue, width: 2.0),
            borderRadius: BorderRadius.circular(8.0),
          ),
          child: ListView(
            children: <Widget>[
              const ListTile(title: Text('Bottom sheet')),
              const TextField(
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  icon: Icon(Icons.attach_money),
                  labelText: 'Enter an integer',
                ),
              ),
              Container(
                alignment: Alignment.center,
                child: ElevatedButton.icon(
                  icon: const Icon(Icons.save),
                  label: const Text('Save and close'),
                  onPressed: () => Navigator.pop(context),
                ),
              )
            ],
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      这样实现

      import 'package:flutter/material.dart';
      
      void main() {
        runApp(const MyApp());
      }
      
      class MyApp extends StatelessWidget {
        const MyApp({Key? key}) : super(key: key);
      
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: const MyHomePage(title: 'Flutter Demo Home Page'),
          );
        }
      }
      
      class MyHomePage extends StatefulWidget {
        const MyHomePage({Key? key, required this.title}) : super(key: key);
        final String title;
      
        @override
        State<MyHomePage> 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,
          ),
          Text(
            'Index 3: Settings',
            style: optionStyle,
          ),
        ];
      
        void _onItemTapped(int index) {
          setState(() {
            _selectedIndex = index;
          });
      
          showModalBottomSheet<void>(
            context: context,
            builder: (BuildContext context) {
              return Container(
                height: 200,
                color: Colors.amber,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text('BottomSheet $index'),
                      ElevatedButton(
                        child: Text('Close BottomSheet $index'),
                        onPressed: () => Navigator.pop(context),
                      )
                    ],
                  ),
                ),
              );
            },
          );
        }
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: const Text('BottomNavigationBar Sample'),
            ),
            body: Center(
              child: _widgetOptions.elementAt(_selectedIndex),
            ),
            bottomNavigationBar: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'Home',
                  backgroundColor: Colors.red,
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.business),
                  label: 'Business',
                  backgroundColor: Colors.green,
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.school),
                  label: 'School',
                  backgroundColor: Colors.purple,
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.settings),
                  label: 'Settings',
                  backgroundColor: Colors.pink,
                ),
              ],
              currentIndex: _selectedIndex,
              selectedItemColor: Colors.amber[800],
              onTap: _onItemTapped,
            ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-11
        • 2022-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 2020-02-07
        相关资源
        最近更新 更多