如何从 Slidable 的小部件树外部关闭它?

问题描述 投票:0回答:2

我正在使用包

flutter_slidable
,在列表项中获得额外的功能真是太棒了。但是,我仍然不知道如何从树外部控制
Slidable
小部件。

简单的应用程序:

我有一个

ListView
,它的每一件物品都用
Slidable
包裹。这些图块由
TextFormField
组成。我希望能够通过点击另一个图块来关闭可滑动的。更准确地说,通过点击另一个图块的 TextFormField。

有三块瓷砖,上面附有可滑动的东西。

下图中,从左到右:

  1. 我滑动第二块瓷砖。
  2. 我点击第三个图块的
    TextFormField
  3. 然后,第二个图块
    Slidable 应关闭

主页:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
            elevation: 0,
            title: const Text('Slidable from outside'),
        ),
        body: SlidableAutoCloseBehavior(
          closeWhenOpened: true,
          closeWhenTapped: false,
          child: ListView.builder(
            itemCount: 3,
            itemBuilder: (context, index) {
              return const MyTile();
            },
          ),
        ),
      ),
    );
  }
}

瓷砖:

class MyTile extends StatelessWidget {
  const MyTile({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Slidable(
      closeOnScroll: false,
      startActionPane: const ActionPane(
        dragDismissible: false,
        motion: ScrollMotion(),
        children: [
          SlidableAction(
            backgroundColor: Color(0xFFe0e0e0),
            icon: Icons.remove_circle_outline_outlined,
            autoClose: false,
            onPressed: null,
          ),
          SlidableAction(
            backgroundColor: Color(0xFFe0e0e0),
            icon: Icons.add_circle_outline_outlined,
            autoClose: false,
            onPressed: null,
          ),
        ],
      ),
      child: Container(
        padding: const EdgeInsets.all(24),
        child: TextFormField(
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w600,
            color: Colors.grey[800],
          ),
          decoration: const InputDecoration(
            isDense: true,
            border: InputBorder.none,
            contentPadding: EdgeInsets.zero,
          ),
          initialValue: '25.000',
          onTap: () {
            //Some code that triggers the close action of another Slidable
          },
        ),
      ),
    );
  }
}

据我了解,在这个软件包的旧版本中,您使用了

SlidableController
,但现在它已经改变了。推荐的方法是用
SlidableAutoCloseBehavior
包裹列表,但它无法独立控制每个 Slidable。

参数

closeWhenTapped
是最接近解决方案的,因为如果我将其设置为
true
,它可以让我在点击另一个图块后关闭该图块,但是,我必须点击两次,因此 TextFormField 不可选第一次接触时。所以我将其设置为
false
,以便让我选择 TextFormField,尽管无法自动关闭 Slidable。

flutter dart flutter-dependencies
2个回答
0
投票

可滑动( 控制器: ....,) 这个控制器属性可以帮助你。


-1
投票

SlidableAutoCloseBehavior( 孩子:列表视图( 孩子们: [ 滑动式( 组标签:'0', ), 滑动式( 组标签:'0', ), 滑动式( 组标签:'1', ), ], ), )

© www.soinside.com 2019 - 2024. All rights reserved.