简体   繁体   English

Flutter:单击上一个和前进按钮时如何水平滚动左右移动

[英]Flutter : how to Horizontal scroll moving left and right when clicking previous and forward button

I added two button in a ListView.我在 ListView 中添加了两个按钮。 It will be horizontal scroll moving left and right when I will click preview and forward button.当我单击预览和前进按钮时,它将左右移动水平滚动。 I want to left side scrolling on click left button, and I want to right side scrolling on click right side button.我想在单击左侧按钮时向左滚动,我想在单击右侧按钮时向右滚动。 but I do not have an idea how to do it.但我不知道该怎么做。 here is source code.这是源代码。

class ScrolllingOnClickButton extends StatefulWidget {
  @override
  _ScrolllingOnClickButtonState createState() => _ScrolllingOnClickButtonState();
}

class _ScrolllingOnClickButtonState extends State<ScrolllingOnClickButton> {
  @override
  Widget build(BuildContext context) {
    var sizeDevice = MediaQuery.of(context).size;

    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: <Widget>[
            Expanded(
                flex: 1,
                child: Container(
                  color: Colors.green,
                )),
            Expanded(
                flex: 2,
                child: Row(
                  children: <Widget>[
                    ClipOval(
                      child: Material(
                        color: Colors.blue,
                        child: InkWell(
                          splashColor: Colors.red,
                          child: SizedBox(
                              width: 56,
                              height: 56,
                              child: Icon(Icons.arrow_back)),
                          onTap: () {},
                        ),
                      ),
                    ),
                    Expanded(
                        flex: 2,
                        child: Container(
                          color: Colors.transparent,
                        )),
                    ClipOval(
                      child: Material(
                        color: Colors.blue,
                        child: InkWell(
                          splashColor: Colors.red,
                          child: SizedBox(
                              width: 56,
                              height: 56,
                              child: Icon(Icons.arrow_forward)),
                          onTap: () {},
                        ),
                      ),
                    ),
                  ],
                )),
            Expanded(
              flex: 16,
              child: Container(
                child: ListView.builder(
                    scrollDirection: Axis.horizontal,
                    physics: PageScrollPhysics(),
                    itemCount: word_data.drink.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        child: Column(
                          children: <Widget>[
                            Expanded(
                                flex: 6,
                                child: GestureDetector(
                                  onTap: () {},
                                  child: Container(
                                    color: Colors.purple,
                                    child: Padding(
                                      padding: const EdgeInsets.all(10.0),
                                      child: Image.asset(
                                        "asset/drink_images/" +
                                            word_data.drink[index] +
                                            ".png",
                                        fit: BoxFit.contain,
                                      ),
                                    ),
                                  ),
                                )),
                          ],
                        ),
                        width: sizeDevice.width,
                      );
                    }),
                color: Colors.yellow,
              ),
            ),
            Expanded(
                flex: 2,
                child: Container(
                  color: Colors.lightBlueAccent,
                ))
          ],
        ),
      ),
    );
  }
}

I recommend using this package carousel_slider .我推荐使用这个包carousel_slider It would save you a lot of time implementing the slider behavior.它会为您节省大量时间来实现滑块行为。

I've created a sample app here for you to test:我在这里创建了一个示例应用程序供您测试:

import 'package:carousel_slider/carousel_options.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';

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

class CarouselDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: BasicDemo());
  }
}

class BasicDemo extends StatelessWidget {
  final controller = CarouselController();

  @override
  Widget build(BuildContext context) {
    List<int> list = [1, 2, 3, 4, 5];
    return Scaffold(
      appBar: AppBar(title: Text('Basic demo')),
      body: Container(
          height: 400,
          child: Column(
            children: [
              Expanded(
                child: CarouselSlider(
                  carouselController: controller,
                  options: CarouselOptions(),
                  items: list
                      .map((item) => Container(
                            child: Center(child: Text(item.toString())),
                            color: Colors.green,
                          ))
                      .toList(),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  IconButton(
                      icon: Icon(Icons.arrow_back_ios),
                      onPressed: () => controller.previousPage()),
                  IconButton(
                      icon: Icon(Icons.arrow_forward_ios),
                      onPressed: () => controller.nextPage())
                ],
              )
            ],
          )),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 flutter 中从左到右和从左到右两个方向滚动水平列表视图 - How to scroll horizontal listview by both direction left to right and right left in flutter 如何从右到左滚动 singlechildscrollview 水平轴 - how to scroll singlechildscrollview horizontal axis from right to left Flutter单击后退按钮时如何执行 - Flutter how to execute when clicking back button 单击另一个按钮时如何自动停用上一个按钮? - How to automatically deactivate the previous button when clicking on another button? 通过按钮单击和滚动进行水平页面交换 - Flutter - Horizontal page swap with button click and scroll - Flutter Flutter - 水平滚动旁边的固定按钮 - Flutter - Fixed button next to horizontal scroll Flutter:当我通过 Transform.rotate 旋转水平 ListView 时,左右边缘被切断 - Flutter : When I rotate a horizontal ListView by Transform.rotate, the left and right edges are cut off 在颤动中选择一个时如何向前移动选择图像按钮 - How to move select image button forward when selecting one in flutter Flutter integration_test:如何模拟鼠标右键/滚动/左键操作? - Flutter integration_test: How to simulate mouse operation like right/scroll/left clicks? 使用谷歌地图颤动点击按钮时如何激活位置? - How to activate location when clicking on button using google maps flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM