【问题标题】:Dart-How to toggle swiper on and off dynamically when the button is pressed?Dart-按下按钮时如何动态打开和关闭滑动器?
【发布时间】:2020-07-20 09:13:25
【问题描述】:

我正在使用 flutter_swiper 在我的颤振应用程序中滑动图像。我想在按下按钮时关闭滑动器,我该如何在现有代码中做到这一点?

这是我的代码:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {


  var eImage = [
    "img/eyes/1.png",
    "img/eyes/2.png",
    "img/eyes/3.png",
  ];

  double height = 200;
  int itemNo;
  double eh = 200;
  double ew = 200;
  double nh = 100;
  double nw = 300;
  double lh = 100;
  double lw = 100;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("img/wal.jpg"),
              fit: BoxFit.fill,
            ),
          ),
          child: Container(
            child: Stack(

                            children: <Widget>[
                              Align(
                                alignment: Alignment(0, -0.6),
                                child: Container(
                                  width: ew,
                                  height: eh,
                                  //color: Colors.purple,
                                  child: new SizedBox(
                                    child: Swiper(
                                      itemBuilder:
                                          (BuildContext context, int index) {
                                        return Image.asset(
                                          eImage[index],
                                        );
                                      },
                                      itemCount: eImage.length,
                                      itemWidth: 200,
                                      itemHeight: 200,
                                      control: new SwiperControl(),
                                      layout: SwiperLayout.DEFAULT,
                                      customLayoutOption: CustomLayoutOption(
                                          startIndex: 1, stateCount: 3) ///<--- here i am trying to start from 1st index
                                          .addRotate([
                                        0 / 180,
                                        0.0,
                                        0 / 180
                                      ]).addTranslate([
                                        Offset(-400.0, 0.0),
                                        Offset(0.0, 0.0),
                                        Offset(370, -40.0),
                                      ]),
                                    ),
                                    height: 200,
                                    width: 350,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ))));
                      }
  }

在此代码中,我能够从列表中滑动图像,但我想在按下按钮(代码中未给出)时禁用它,如何在按钮单击时动态切换打开/关闭此滑动器?

【问题讨论】:

    标签: flutter dart flutter-layout swiper flutter-animation


    【解决方案1】:

    只需在触摸到达 Swiper 之前使用吸收指针即可

    Stack(
          children: <Widget>[
            TheSwiperWidget(),
            AbsorbPointer(
              absorbing: _disabled,
            ),
          ],
        )
    

    并在您的按钮中更改状态

    FlatButton(
     onPressed: (){
          setState(() {
            _disabled = !=_disabled;
          });
        }, child: Text('Disable'),
       )
    

    【讨论】:

    • 但箭头仍然可见。我也想删除箭头,所以这个解决方案对我不起作用
    • 查看 SwipeController,它可能有办法手动更改页面,使用相同的 _disabled 变量来呈现或不呈现按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多