标准底板在 Flutter 中

jucafojl  于 2023-04-07  发布在  Flutter
关注(0)|答案(5)|浏览(126)

我很难在我的应用程序中实现“标准底部表单”-我的意思是底部表单,其中“header”是可见的和可拖动的(参考:https://material.io/design/components/sheets-bottom.html#standard-bottom-sheet)。更多信息:我在任何地方都找不到它的任何例子:S.我希望的结果是通过实现DraggableScrollableSheet作为bottomSheet:在Scaffold中(只有小部件具有initialChildSize),但是没有办法使标题“粘滞”,因为所有内容都是可滚动:/。
我还发现了这个:https://flutterdoc.com/bottom-sheets-in-flutter-ec05c90453e7-接缝像有关于“持久性底表”的一部分是一个我正在寻找,但artical是不详细,所以我不能弄清楚它确切的方式来实现它加上评论是preaty负有,所以我猜这是不完全正确的…
有没有人有解决办法?:S

hfyxw5xn

hfyxw5xn1#

你可以在材质等级库中看到的标准底部板材行为可以通过使用DraggableScrollableSheet来实现。
在这里我将详细地解释它。

第一步:

定义您的Scaffold

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Draggable sheet demo',
      home: Scaffold(

          ///just for status bar color.
          appBar: PreferredSize(
              preferredSize: Size.fromHeight(0),
              child: AppBar(
                primary: true,
                elevation: 0,
              )),
          body: Stack(
            children: <Widget>[
              Positioned(
                left: 0.0,
                top: 0.0,
                right: 0.0,
                child: PreferredSize(
                    preferredSize: Size.fromHeight(56.0),
                    child: AppBar(
                      title: Text("Standard bottom sheet demo"),
                      elevation: 2.0,
                    )),
              ),
              DraggableSearchableListView(),
            ],
          )),
    );
  }
}

第二步:

定义DraggableSearchableListView

class DraggableSearchableListView extends StatefulWidget {
  const DraggableSearchableListView({
    Key key,
  }) : super(key: key);

  @override
  _DraggableSearchableListViewState createState() =>
      _DraggableSearchableListViewState();
}

class _DraggableSearchableListViewState
    extends State<DraggableSearchableListView> {
  final TextEditingController searchTextController = TextEditingController();
  final ValueNotifier<bool> searchTextCloseButtonVisibility =
      ValueNotifier<bool>(false);
  final ValueNotifier<bool> searchFieldVisibility = ValueNotifier<bool>(false);
  @override
  void dispose() {
    searchTextController.dispose();
    searchTextCloseButtonVisibility.dispose();
    searchFieldVisibility.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return NotificationListener<DraggableScrollableNotification>(
      onNotification: (notification) {
        if (notification.extent == 1.0) {
          searchFieldVisibility.value = true;
        } else {
          searchFieldVisibility.value = false;
        }
        return true;
      },
      child: DraggableScrollableActuator(
        child: Stack(
          children: <Widget>[
            DraggableScrollableSheet(
              initialChildSize: 0.30,
              minChildSize: 0.15,
              maxChildSize: 1.0,
              builder:
                  (BuildContext context, ScrollController scrollController) {
                return Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(16.0),
                      topRight: Radius.circular(16.0),
                    ),
                    boxShadow: [
                      BoxShadow(
                          color: Colors.grey,
                          offset: Offset(1.0, -2.0),
                          blurRadius: 4.0,
                          spreadRadius: 2.0)
                    ],
                  ),
                  child: ListView.builder(
                    controller: scrollController,

                    ///we have 25 rows plus one header row.  
                    itemCount: 25 + 1,
                    itemBuilder: (BuildContext context, int index) {
                      if (index == 0) {
                        return Container(
                          child: Column(
                            children: <Widget>[
                              Align(
                                alignment: Alignment.centerLeft,
                                child: Padding(
                                  padding: EdgeInsets.only(
                                    top: 16.0,
                                    left: 24.0,
                                    right: 24.0,
                                  ),
                                  child: Text(
                                    "Favorites",
                                    style:
                                        Theme.of(context).textTheme.headline6,
                                  ),
                                ),
                              ),
                              SizedBox(
                                height: 8.0,
                              ),
                              Divider(color: Colors.grey),
                            ],
                          ),
                        );
                      }
                      return Padding(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          child: ListTile(title: Text('Item $index')));
                    },
                  ),
                );
              },
            ),
            Positioned(
              left: 0.0,
              top: 0.0,
              right: 0.0,
              child: ValueListenableBuilder<bool>(
                  valueListenable: searchFieldVisibility,
                  builder: (context, value, child) {
                    return value
                        ? PreferredSize(
                            preferredSize: Size.fromHeight(56.0),
                            child: Container(
                              decoration: BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                      width: 1.0,
                                      color: Theme.of(context).dividerColor),
                                ),
                                color: Theme.of(context).colorScheme.surface,
                              ),
                              child: SearchBar(
                                closeButtonVisibility:
                                    searchTextCloseButtonVisibility,
                                textEditingController: searchTextController,
                                onClose: () {
                                  searchFieldVisibility.value = false;
                                  DraggableScrollableActuator.reset(context);
                                },
                                onSearchSubmit: (String value) {
                                  ///submit search query to your business logic component
                                },
                              ),
                            ),
                          )
                        : Container();
                  }),
            ),
          ],
        ),
      ),
    );
  }
}

第三步:

定义自定义粘性搜索栏

class SearchBar extends StatelessWidget {
  final TextEditingController textEditingController;
  final ValueNotifier<bool> closeButtonVisibility;
  final ValueChanged<String> onSearchSubmit;
  final VoidCallback onClose;

  const SearchBar({
    Key key,
    @required this.textEditingController,
    @required this.closeButtonVisibility,
    @required this.onSearchSubmit,
    @required this.onClose,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return Container(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 0),
        child: Row(
          children: <Widget>[
            SizedBox(
              height: 56.0,
              width: 56.0,
              child: Material(
                type: MaterialType.transparency,
                child: InkWell(
                  child: Icon(
                    Icons.arrow_back,
                    color: theme.textTheme.caption.color,
                  ),
                  onTap: () {
                    FocusScope.of(context).unfocus();
                    textEditingController.clear();
                    closeButtonVisibility.value = false;
                    onClose();
                  },
                ),
              ),
            ),
            SizedBox(
              width: 16.0,
            ),
            Expanded(
              child: TextFormField(
                onChanged: (value) {
                  if (value != null && value.length > 0) {
                    closeButtonVisibility.value = true;
                  } else {
                    closeButtonVisibility.value = false;
                  }
                },
                onFieldSubmitted: (value) {
                  FocusScope.of(context).unfocus();
                  onSearchSubmit(value);
                },
                keyboardType: TextInputType.text,
                textInputAction: TextInputAction.search,
                textCapitalization: TextCapitalization.none,
                textAlignVertical: TextAlignVertical.center,
                textAlign: TextAlign.left,
                maxLines: 1,
                controller: textEditingController,
                decoration: InputDecoration(
                  isDense: true,
                  border: InputBorder.none,
                  hintText: "Search here",
                ),
              ),
            ),
            ValueListenableBuilder<bool>(
                valueListenable: closeButtonVisibility,
                builder: (context, value, child) {
                  return value
                      ? SizedBox(
                          width: 56.0,
                          height: 56.0,
                          child: Material(
                            type: MaterialType.transparency,
                            child: InkWell(
                              child: Icon(
                                Icons.close,
                                color: theme.textTheme.caption.color,
                              ),
                              onTap: () {
                                closeButtonVisibility.value = false;
                                textEditingController.clear();
                              },
                            ),
                          ),
                        )
                      : Container();
                })
          ],
        ),
      ),
    );
  }
}

查看最终输出截图。

状态1:

底部的表显示了它的初始大小。

状态2:

用户向上拖动底部工作表。

状态3:

底部工作表到达屏幕的顶部边缘,并显示粘性自定义SearchBar界面。

仅此而已

查看here的现场演示。

oknrviil

oknrviil2#

由于@Sergio命名了一些好的替代方案,它仍然需要更多的编码来使它工作,因为它应该说,我发现滑动_向上_面板所以对于其他任何人寻找解决方案,你可以找到它here
尽管如此,我还是觉得很奇怪,Flutter中内置的bottomSheet小部件没有提供创建www.example.com中提到的“标准底部表单”的选项material.io:

1zmg4dgp

1zmg4dgp3#

如果您正在寻找持久的底单比请参考源代码从下面的链接
Persistent Bottomsheet
您可以参考**_showBottomSheet()**了解您的需求,一些更改将满足您的需求

erhoui1w

erhoui1w4#

您可以使用堆栈和动画来完成此操作:

class HelloWorldPage extends StatefulWidget {
  @override
  _HelloWorldPageState createState() => _HelloWorldPageState();
}

class _HelloWorldPageState extends State<HelloWorldPage>
    with SingleTickerProviderStateMixin {
  final double minSize = 80;
  final double maxSize = 350;

  void initState() {
    _controller =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500))
          ..addListener(() {
            setState(() {});
          });

    _animation =
        Tween<double>(begin: minSize, end: maxSize).animate(_controller);

    super.initState();
  }

  AnimationController _controller;
  Animation<double> _animation;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          Positioned(
            bottom: 0,
            height: _animation.value,
            child: GestureDetector(
              onDoubleTap: () => _onEvent(),
              onVerticalDragEnd: (event) => _onEvent(),
              child: Container(
                color: Colors.red,
                width: MediaQuery.of(context).size.width,
                height: minSize,
              ),
            ),
          ),
        ],
      ),
    );
  }

  _onEvent() {
    if (_controller.isCompleted) {
      _controller.reverse(from: maxSize);
    } else {
      _controller.forward();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
5jvtdoz2

5jvtdoz25#

可以很容易地实现与showModalBottomSheet .代码:

void _presentBottomSheet(BuildContext context) {
  showModalBottomSheet(
    context: context,
    builder: (context) => Wrap(
      children: <Widget>[
        SizedBox(height: 8),
        _buildBottomSheetRow(context, Icons.share, 'Share'),
        _buildBottomSheetRow(context, Icons.link, 'Get link'),
        _buildBottomSheetRow(context, Icons.edit, 'Edit Name'),
        _buildBottomSheetRow(context, Icons.delete, 'Delete collection'),
      ],
    ),
  );
}

Widget _buildBottomSheetRow(
  BuildContext context,
  IconData icon,
  String text,
) =>
    InkWell(
      onTap: () {},
      child: Row(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(16),
            child: Icon(
              icon,
              color: Colors.grey[700],
            ),
          ),
          SizedBox(width: 8),
          Text(text),
        ],
      ),
    );

相关问题