【问题标题】:How to close a Slidable from outside its widget tree?如何从其小部件树外部关闭 Slidable?
【发布时间】:2023-01-04 17:35:31
【问题描述】:

我正在使用包flutter_slidable,在列表项中获得额外的功能真是太棒了。但是,我仍然无法弄清楚如何从其树外部控制 Slidable 小部件。

简单的应用程序:

我有一个ListView,它的每一项都用Slidable包裹着。这些磁贴由 TextFormField 组成。我希望能够通过点击另一个图块来关闭 Slidable。更准确地说,通过点击另一个磁贴的 TextFormField。

共有三个贴有 Slidable 的贴图。

在下面的图片中,从左到右:

  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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      相关资源
      最近更新 更多