【问题标题】:Show bottomSheet beneath bottomNavigationBar在bottomNavigationBar下方显示bottomSheet
【发布时间】:2018-06-03 15:01:39
【问题描述】:

在我们的应用中,我们使用了一个bottomSheet 和一个bottomNavigationBar。

bottomSheet出现在bottomNavigationBar上方,有没有办法让它出现在下方?

这是一个示例应用:

import 'package:flutter/material.dart';

void main() {
  runApp(SampleApp());
}

class SampleApp extends StatefulWidget {
  @override
  _SampleAppState createState() => new _SampleAppState();
}

class _SampleAppState extends State<SampleApp> {
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  PersistentBottomSheetController _sheetController;

  @override
  Widget build(BuildContext context) {
    final _showBottomSheet = () {
      _sheetController = _scaffoldKey.currentState.showBottomSheet((context) {
        return Container(
            color: Colors.grey[200],
            child: Column(mainAxisSize: MainAxisSize.min, children: [
              RadioListTile(dense: true, title: Text('Test'), groupValue: 'test', onChanged: (value) {}, value: true),
              RadioListTile(dense: true, title: Text('Test'), groupValue: 'test', onChanged: (value) {}, value: true),
            ]));
      });
    };

    return MaterialApp(
      home: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: Text('Sample App'),
        ),
        bottomNavigationBar: Container(
          child: IconButton(
            icon: Icon(Icons.edit),
            onPressed: _showBottomSheet,
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 我很确定这是不可能的。我了解您的目标,但这只是其一般工作方式的问题。新的 FAB 职位也不适用于bottomNavigationBar。可能只是检查 GitHub 上的问题。

标签: flutter


【解决方案1】:

添加:useRootNavigator: true,

showModalBottomSheet(
      context: context,
      useRootNavigator: true,
      builder: (context) {},
    );

【讨论】:

  • 感谢您的建议!这是用于 showModalBottomSheet 而我想使用 showBottomSheet。
  • 这应该是答案。它对我有用。
【解决方案2】:

注意到您的问题。有同样的问题并找到了更好的解决方案。使用showModalBottomSheet()。它将覆盖底部导航。

【讨论】:

  • 不明白为什么这不是公认的答案。接受的答案看起来像一个hacky解决方法,而你的答案就像一个魅力。
  • showModalBottomSheet 覆盖整个视图,还添加了一个屏障,阻止与它下方的任何视图交互。两者都有业务案例,但所提出的问题与非模态版本 showBottomSheet 有关。
【解决方案3】:

您可以使用Column 将弹出窗口与底部导航栏结合起来,并使用Expandable 模拟底部工作表行为:

import 'package:flutter/material.dart';
import 'package:expandable/expandable.dart';

void main() {
  runApp(SampleApp());
}

class SampleApp extends StatefulWidget {
  @override
  _SampleAppState createState() => new _SampleAppState();
}

class _SampleAppState extends State<SampleApp> {

  @override
  Widget build(BuildContext context) {
    buildBottomSheet() {
      return Container(
          color: Colors.grey[200],
          child: Column(mainAxisSize: MainAxisSize.min, children: [
            RadioListTile(dense: true, title: Text('Test'), groupValue: 'test', onChanged: (value) {}, value: true),
            RadioListTile(dense: true, title: Text('Test'), groupValue: 'test', onChanged: (value) {}, value: true),
          ]));
    }

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Sample App'),
        ),
        body: Container(
          color: Colors.green,
        ),
        bottomNavigationBar: ExpandableNotifier(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ExpandableButton(
                child: SizedBox(height: 50,
                  child: Center(
                    child: Icon(Icons.edit),
                  ),
                ),
              ),
              Expandable(
                expanded: buildBottomSheet(),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

对于生产应用,请考虑使用SafeArea 在底部添加适当的填充。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2017-07-18
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多