【问题标题】:Trigger slidable from button flutter触发器可从按钮颤动中滑动
【发布时间】:2021-01-14 15:15:46
【问题描述】:

我正在使用 flutter_slidable 包从我的应用程序中的列表中删除项目。但我想在列表视图之外创建一个按钮,可以触发这些项目以显示它们的可滑动项,但我不确定如何从提供的文档中执行此操作

https://pub.dev/packages/flutter_slidable


bool isClicked = false;

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        brightness: Brightness.light,
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back, color: Colors.black),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        backgroundColor: Colors.white,
        title: const Text(
          'User Cards',
          style: TextStyle(
              fontWeight: FontWeight.bold, color: Colors.black, fontSize: 20.0),
        ),
        actions: <Widget>[
          Padding(
            padding: const EdgeInsets.only(right: 20.0),
            child: Row(
              children: <Widget>[
                GestureDetector(
                    onTap: () {
                      Provider.of<CardBrandProvider>(context, listen: false).clearCardBrand();
                      Provider.of<Data>(context, listen: false).selectedBrand = null;
                      Navigator.push(context, MaterialPageRoute(builder: (context) => AddCard()));
                    },
                    child: Row(
                      children: <Widget>[
                        Icon(
                          Icons.card_giftcard,
                          color: Colors.grey[600],
                          size: 28.0,
                        ),
                        Icon(
                          Icons.add,
                          size: 20.0,
                          color: Colors.black45,
                        ),
                      ],
                    )),
              ],
            ),
          )
        ],
        elevation: 1.0,
        centerTitle: true,
      ),

      body: CustomScrollView(
        slivers: [
          Provider.of<CardProvider>(context, listen: false).cards.isNotEmpty ?
          SliverToBoxAdapter(
            child: Padding(
              padding: const EdgeInsets.only(top: 20),
              child: Padding(
                padding: const EdgeInsets.only(left: 30.0, right: 26.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      'Cards',
                      style: TextStyle(
                          fontSize: 17.0,
                          fontWeight: FontWeight.w500,
                          fontFamily: 'Lato'),
                    ),
                    GestureDetector(
                      onTap: (){
                        isClicked = true;
                      },
                      child: Text(
                        'Edit',
                        style: TextStyle(
                            fontSize: 17.0,
                            fontWeight: FontWeight.w500,
                            color: Color.fromRGBO(129, 2, 2, 1),
                            fontFamily: 'Lato'),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          )
              :
          SliverToBoxAdapter(child: Container(),),
          SliverToBoxAdapter(
            child: SizedBox(
              height: 25,
            ),
          ),
          Container(
            child: SliverList(
              delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                  
                  var card = getCards(context)[index];

                  var id = card.Id;

                  return InkWell(
                    child: Padding(
                      padding: const EdgeInsets.only(top: 15, bottom: 15),
                      child: Column(
                        children: [

                          Container(
                            child: Slidable(
                              enabled: isClicked,
                              actionPane: SlidableDrawerActionPane(),
                              secondaryActions: <Widget>[

                              IconSlideAction(
                                caption: 'Delete',
                                color: Colors.red,
                                icon: Icons.delete,
                                onTap: () async {

                                  setState(() {
                                      showSpinner = true;                              
                                  });

                                  try{
                                    
                                    print('$id');
                                    setState(() {
                                      Provider.of<Cards>(context, listen: false).removeIndividualCard(index);
                                    });

                                  } catch(e){
                                    print(e);
                                    setState(() {
                                      showSpinner = true;                              
                                  });
                                  }

                                }),
                                ],

                              child: ListTile(
                                title: Text('Username$index'),
                                trailing: Icon(Icons.person),
                              ),

                            ),
                          ),

                        ],
                      ),
                    ),
                  );
                },
                childCount: 5,
              ),
           ],
      ),
    );
  }
}

【问题讨论】:

  • 您需要一个按钮来启用或禁用可滑动吗?
  • 是的 - 启用它

标签: flutter flutter-packages


【解决方案1】:

制作一个按钮和句柄:Slidable(enabled: false,)

【讨论】:

  • 之前已经尝试过了.. 没有任何作用。如果我创建一个 bool 并通过 Slidable(enabled: false) 和我的按钮处理它,则不会触发可滑动的。谢谢你
  • 有用吗?或者你到底想要什么?你想通过一个按钮来处理可滑动的功能吗?
  • 能否请您分享您到目前为止所做的代码?
  • 添加了代码。但是,是的,伙计,这还没有发生在我身上
  • 你试过这个吗? GestureDetector( onTap: (){ setstate({ isClicked = true;}) },
猜你喜欢
  • 2017-04-10
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
相关资源
最近更新 更多