如何制作诸如WheelView之类的圆形项目清单?

时间:2018-10-17 12:55:38

标签: view flutter

我目前正在开发一个应用,该应用需要以循环方式显示选项列表。就像一个左轮手枪? /¿车轮?我什至不知道如何用单词来定义它。

作为参考,我想要类似Glovo的东西: Screenshot_Glovo

我自己尝试了一些东西,但这很混乱,不适合每个设备的长宽比。这是代码

/* HomeBubbleItem is just a Container with BoxShape.circle and Text */

Widget _buildExperiencesSection() {
    return Center(
      child: Stack(
        children: <Widget>[
          Positioned(
            top: MediaQuery.of(context).size.height / 3,
            left: MediaQuery.of(context).size.width / 3,
            child: HomeBubbleItem(experience: _experiences[0], color: Colors.blue, icon: Icons.check,),
          ),
          Positioned(
            top: MediaQuery.of(context).size.height / 4,
            left: MediaQuery.of(context).size.width / 32,
            child: HomeBubbleItem(experience: _experiences[1], color: Colors.blue, icon: Icons.check,),
          ),
          Positioned(
            top: MediaQuery.of(context).size.height / 4,
            right: MediaQuery.of(context).size.width / 32,
            child: HomeBubbleItem(experience: _experiences[2], color: Colors.blue, icon: Icons.check,),
          ),
          Positioned(
            top: MediaQuery.of(context).size.height / 7,
            left: MediaQuery.of(context).size.width / 3,
            child: HomeBubbleItem(experience: _experiences[3], color: Colors.blue, icon: Icons.check,),
          ),
          Positioned(
            bottom: MediaQuery.of(context).size.height / 14,
            left: MediaQuery.of(context).size.width / 3,
            child: HomeBubbleItem(experience: _experiences[4], color: Colors.blue, icon: Icons.check,),
          ),
          Positioned(
            bottom: MediaQuery.of(context).size.height / 6.5,
            left: MediaQuery.of(context).size.width / 32,
            child: HomeBubbleItem(experience: _experiences[5], color: Colors.blue, icon: Icons.check,),
          ),
          Positioned(
            bottom: MediaQuery.of(context).size.height / 6.5,
            right: 11.0,
            child: HomeBubbleItem(experience: _experiences[6], color: Colors.blue, icon: Icons.check,),
          ),
        ],
      ),
    );
  }

这是我可悲的结果: My_Screenshot

我想知道是否有更好的优化方法来实现这一目标。

谢谢!

1 个答案:

答案 0 :(得分:1)

我希望以下示例可以解决您的问题。

import 'dart:math';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

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

  double radius =  125.0;

  List<Widget> list(){
    final double firstItemAngle = pi;
    final double lastItemAngle = pi;
    final double angleDiff = (firstItemAngle + lastItemAngle) / 6;
    double currentAngle = firstItemAngle;

    return data.map((int index){
      currentAngle += angleDiff;
      return _radialListItem(currentAngle,index);
    }).toList();
  }

  Widget _radialListItem(double angle, int index){

    final x = cos(angle)  * radius;
    final y = sin(angle) * radius;


    return Center(
      child: Transform(
        transform: index == 1 ? Matrix4.translationValues(0.0, 0.0 , 0.0) : Matrix4.translationValues(x, y , 0.0),
        child: InkWell(
          onTap: (){
            print(index.toString());
          },
          child: CircleAvatar(
            radius: 50.0,
            backgroundImage: AssetImage("images/c1.jpeg"),
          ),
        )
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text("hello World"),
      ),
      body: new Stack(
        children: list()
      ),
    );
  }
}