简体   繁体   English

如何实现单轮滚动列表小部件

[英]How to implement cycle wheel scroll list widget

Flutter has ListWheelScrollView widget but I want cycle wheel scroll widget. Flutter有ListWheelScrollView窗口小部件,但我想要循环滚轮滚动小部件。 Any ideas how to implement such widget. 任何想法如何实现这样的小部件。

How it should work: 它应该如何工作:

For example, I have a list with 10 items and a selected item is 1 The selected element is positioned by center above this element, you see the last element in the list below the second element 例如,我有一个包含10个项目的列表,一个选定的项目是1。选定的元素位于该元素上方的中心位置,您会看到列表中的最后一个元素位于第二个元素下方

   [10]
-> [1] <-
   [2]

scroll down 向下滚动

   [9]
-> [10] <-
   [1]

etc. 等等

Thanks! 谢谢!

You are right considering ListWheelScrollView. 您正在考虑使用ListWheelScrollView。

The exact solution is to use ListWheelScrollView.useDelegate with ListWheelChildLoopingListDelegate . 确切的解决方案是将ListWheelScrollView.useDelegateListWheelChildLoopingListDelegate一起使用。

Example: 例:

import 'package:flutter/material.dart';

const String kTitle = 'Loop Wheel Demo';

void main() => runApp(new LoopWheelDemo());

class LoopWheelDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: kTitle,
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  HomePage({Key key,}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _style = Theme.of(context).textTheme.display2;
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(kTitle),
      ),
      body: new Center(
        child: new ConstrainedBox(
          constraints: BoxConstraints(
            // Set height to one line, otherwise the whole vertical space is occupied.
            maxHeight: _style.fontSize,
          ),
          child: new ListWheelScrollView.useDelegate(
            itemExtent: _style.fontSize,
            childDelegate: ListWheelChildLoopingListDelegate(
              children: List<Widget>.generate(
                10, (index) => Text('${index + 1}', style: _style),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

I've been trying to figure it out how to do that. 我一直在努力弄清楚该怎么做。 I've tried lots of options, but the one that made me achieve what I wanted and what you are asking for was using the plugin: 我尝试了很多选择,但是使我实现了我想要的和您要的选择的那个是使用插件:

Flutter Swiper ( https://pub.dartlang.org/packages/flutter_swiper ). Flutter Swiperhttps://pub.dartlang.org/packages/flutter_swiper )。

It's pretty customizable e flexible. 这是相当可定制的e灵活性。

Here is the screenshot: https://imgur.com/a/ktxU6Hx 这是屏幕截图: https : //imgur.com/a/ktxU6Hx

This is how I implemented it: 这是我的实现方式:

 import 'package:flutter/material.dart';

 import 'package:flutter_swiper/flutter_swiper.dart';

class Looping extends StatefulWidget {
  @override
  LoopingState createState() {
    return new LoopingState();
  }
}

class LoopingState extends State<Looping> {

  List<int> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

  List<String> options = ['A', 'B', 'C', 'D'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Infinity Loop Items'),),
      body: Center(
        child: Container(
          height: 100.0,
          child: Row(
            children: <Widget>[
              mySwiper(numbers),
              mySwiper(options),
            ],
          ),
        ),
      ),
    );
  }

  Widget mySwiper(List list) {
    return Expanded(
          child: Swiper(
          itemCount: list.length,
          scrollDirection: Axis.vertical,         
          control: SwiperControl(),
          itemBuilder: (BuildContext context, int index) {
            return  Center(
                child: Text(
                  list[index].toString(),
                  style: TextStyle(fontSize: 20.0),                
                ),             
            );
          }),
    );
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM