如何在Flutter中创建一个情绪随机器屏幕。

huangapple go评论62阅读模式
英文:

how to make a mood randomizer screen in flutter

问题

Here's the translated content from your provided text:

我在创建Flutter UI界面方面遇到了问题,实际上这个界面有7个圆圈,一个在中间,另外六个在它周围。当六个圆圈中的任何一个被点击时,我希望所有六个圆圈都围绕中间的圆圈开始旋转。为了更好地理解,请参考附加的图像。

如何在Flutter中创建一个情绪随机器屏幕。

到目前为止,我尝试使用Flutter包circular_rotation: ^0.0.1。这个包提供了我想要的效果,但问题是这个包只允许我创建4个围绕中心的圆圈,当我添加第5个时,它开始表现得很奇怪。

我的代码如下:

import 'dart:math';
import 'package:circular_rotation/circular_rotation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:islamicapp/app/data/constant/app_color.dart';
import 'package:islamicapp/app/data/constant/image_string.dart';
import 'package:islamicapp/app/views/widgets/button_text2.dart';
import 'package:islamicapp/app/views/widgets/ktext2.dart';

class Randomizer extends StatefulWidget {
  const Randomizer({super.key});

  @override
  State<Randomizer> createState() => _RandomizerState();
}

class _RandomizerState extends State<Randomizer> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.green,
      body: Container(
        width: Get.width,
        height: Get.height,
        alignment: Alignment.center,
        child: CircularRotation(
          circularRotationProperty: CircularRotationModel(
            defaultCircleStrokeColor: Colors.transparent,
            startAnimation: false,
            repeatAnimation: false,
            firstCircleAnimationDuration: 12000,
            centerWidget: Container(
              width: 150.w,
              height: 150.h,
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: AppColor.feel,
                shape: BoxShape.circle,
              ),
              child: KText2(
                text: "feel".tr,
                fontSize: 24.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
            visibleThirdCircle: false,
            firstCircleWidgets: [
              // 你的六个圆圈的部分
            ],
            firstCircleRadius: 330.w,
            visibleSecondCircle: true,
          ),
        ),
      ),
    );
  }
}

请注意,上述代码中包含了六个圆圈的部分,你可以在注释部分添加这些圆圈的代码。

英文:

I am having trouble in creating a UI screen in flutter, essentially this screen has 7 circles, one in the middle and six around it. When any of six circles are tapped, I want all six circles to start revolving around middle one. For more understanding plese refer to the image attached.

如何在Flutter中创建一个情绪随机器屏幕。

Uptil now I tried using flutter package circular_rotation: ^0.0.1. This package wproviding me with exctly what I want, but the issue is that this package is letting mr to only create 4 circles around middle one, just when I add 5th, it starts acting wierd.

My code

import &#39;dart:math&#39;;
import &#39;package:circular_rotation/circular_rotation.dart&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter_screenutil/flutter_screenutil.dart&#39;;
import &#39;package:get/get.dart&#39;;
import &#39;package:google_fonts/google_fonts.dart&#39;;
import &#39;package:islamicapp/app/data/constant/app_color.dart&#39;;
import &#39;package:islamicapp/app/data/constant/image_string.dart&#39;;
import &#39;package:islamicapp/app/views/widgets/button_text2.dart&#39;;
import &#39;package:islamicapp/app/views/widgets/ktext2.dart&#39;;

class Randomizer extends StatefulWidget {
  const Randomizer({super.key});

  @override
  State&lt;Randomizer&gt; createState() =&gt; _RandomizerState();
}

class _RandomizerState extends State&lt;Randomizer&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.green,
      body: Container(
        width: Get.width,
        height: Get.height,
        alignment: Alignment.center,
        child: CircularRotation(
          circularRotationProperty: CircularRotationModel(
            // defaultCircleStrokeWidth: Dimens.strokeSmall,
            defaultCircleStrokeColor: Colors.transparent,
            startAnimation: false,
            repeatAnimation: false,
            firstCircleAnimationDuration: 12000,

            // thirdCircleAnimationDuration: 10000,
        centerWidget: Container(
          width: 150.w,
          height: 150.h,
          padding: EdgeInsets.all(10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: AppColor.feel,
            shape: BoxShape.circle,
          ),
          child: KText2(
            text: &quot;feel&quot;.tr,
            fontSize: 24.sp,
            fontWeight: FontWeight.bold,
          ),
        ),

        visibleThirdCircle: false,
        firstCircleWidgets: [
          InkWell(
            // onTap: CircularRotation.eitherStartOrStopAnimation,
            child: Container(
              width: 100.w,
              // height: 100.h,
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Column(
                children: [
                  Image(
                    image: ImageString.happy,
                    width: 60.w,
                    height: 60.h,
                  ),
                  KText2(
                    text: &quot;happy&quot;.tr,
                    fontSize: 20.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ],
              ),
            ),
          ),
          InkWell(
            // onTap: CircularRotation.eitherStartOrStopAnimation,
            child: Container(
              width: 100.w,
              // height: 100.h,
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Column(
                children: [
                  Image(
                    image: ImageString.happy,
                    width: 60.w,
                    height: 60.h,
                  ),
                  KText2(
                    text: &quot;happy&quot;.tr,
                    fontSize: 20.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ],
              ),
            ),
          ),
          InkWell(
            // onTap: CircularRotation.eitherStartOrStopAnimation,
            child: Container(
              width: 100.w,
              // height: 100.h,
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Column(
                children: [
                  Image(
                    image: ImageString.happy,
                    width: 60.w,
                    height: 60.h,
                  ),
                  KText2(
                    text: &quot;happy&quot;.tr,
                    fontSize: 20.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ],
              ),
            ),
          ),
          InkWell(
            // onTap: CircularRotation.eitherStartOrStopAnimation,
            child: Container(
              width: 100.w,
              // height: 100.h,
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Column(
                children: [
                  Image(
                    image: ImageString.happy,
                    width: 60.w,
                    height: 60.h,
                  ),
                  KText2(
                    text: &quot;happy&quot;.tr,
                    fontSize: 20.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ],
              ),
            ),
          ),
        ],
        firstCircleRadius: 330.w,
        visibleSecondCircle: true,
        // secondCircleRadius: 330.w,
        // secondCircleAnimationDuration: 12000,
        // thirdCircleRadius: 150.w,
        // secondCircleWidgets: [
        //   Container(
        //     width: 120.w,
        //     // height: 100.h,
        //     padding: EdgeInsets.all(10),
        //     alignment: Alignment.center,
        //     decoration: BoxDecoration(
        //       color: Colors.white,
        //       shape: BoxShape.circle,
        //     ),
        //     child: Column(
        //       children: [
        //         Image(
        //           image: ImageString.happy,
        //           width: 60.w,
        //           height: 60.h,
        //         ),
        //         KText2(
        //           text: &quot;happy&quot;.tr,
        //           fontSize: 20.sp,
        //           fontWeight: FontWeight.bold,
        //         ),
        //       ],
        //     ),
        //   ),
        // ],
        // thirdCircleWidgets: _thirdCircleImages,
        // thirdCircleRadians: Dimens.thirdCircleRadians,
        // secondCircleRadians: Dimens.secondCircleRadians,
        // firstCircleRadians: Dimens.firstCircleRadians,
      ),
    ),
  ),
);
  }
}

答案1

得分: 1

你可以使用circle_list包创建它,如下所示:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const RotateCircles(),
    );
  }
}

class RotateCircles extends StatefulWidget {
  const RotateCircles({Key? key}) : super(key: key);
  @override
  State<RotateCircles> createState() => _RotateCirclesState();
}

class _RotateCirclesState extends State<RotateCircles>
    with SingleTickerProviderStateMixin {
  late AnimationController animationController;

  @override
  void initState() {
    animationController = AnimationController(
      upperBound: pi * 2,
      vsync: this,
      duration: const Duration(seconds: 2),
    );
    super.initState();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  List<int> elements = List.generate(6, (index) => index);

  _rotate(int index) {
    final angle = (pi * 2) * (index / 10);
    animationController.animateTo(angle);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      drawer: const Drawer(),
      body: Center(
        child: AnimatedBuilder(
          animation: animationController,
          builder: ((context, child) {
            return CircleList(
              onDragEnd: () {},
              onDragStart: (cord) {},
              innerCircleColor: Colors.redAccent,
              outerCircleColor: Colors.greenAccent,
              initialAngle: -animationController.value - pi / 2,
              outerRadius: 130,
              innerRadius: 50,
              centerWidget: Container(
                width: 100,
                height: 100,
                decoration: const BoxDecoration(
                  color: Colors.blue,
                  shape: BoxShape.circle,
                ),
              ),
              rotateMode: RotateMode.stopRotate,
              origin: const Offset(0, 0),
              children: elements
                  .map(
                    (index) => GestureDetector(
                      onTap: () => _rotate(index),
                      child: Container(
                        width: 75,
                        height: 75,
                        decoration: const BoxDecoration(
                          color: Colors.green,
                          shape: BoxShape.circle,
                        ),
                      ),
                    ),
                  )
                  .toList(),
            );
          }),
        ),
      ),
    );
  }
}

你可以在这里找到完整的代码:这里

英文:

You can create it using the circle_list package as follows

more examples here

import &#39;package:flutter/material.dart&#39;;
import &#39;dart:math&#39;;
import &#39;package:circle_list/circle_list.dart&#39;;
void main() =&gt; runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: &#39;Flutter Demo&#39;,
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const RotateCircles(),
);
}
}
class RotateCircles extends StatefulWidget {
const RotateCircles({Key? key}) : super(key: key);
@override
State&lt;RotateCircles&gt; createState() =&gt; _RotateCirclesState();
}
class _RotateCirclesState extends State&lt;RotateCircles&gt;
with SingleTickerProviderStateMixin {
late AnimationController animationController;
@override
void initState() {
animationController = AnimationController(
upperBound: pi * 2,
vsync: this,
duration: const Duration(seconds: 2),
);
super.initState();
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
List&lt;int&gt; elements = List.generate(6, (index) =&gt; index);
_rotate(int index) {
//final _random = new Random();
final angle = (pi * 2) * (index / 10);
// you can rotate to any angle you want
animationController.animateTo(angle);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
drawer: const Drawer(),
body: Center(
child: AnimatedBuilder(
animation: animationController,
builder: ((context, child) {
return CircleList(
onDragEnd: () {},
onDragStart: (cord) {},
innerCircleColor: Colors.redAccent,
outerCircleColor: Colors.greenAccent,
initialAngle: -animationController.value - pi / 2,
outerRadius: 130,
innerRadius: 50,
centerWidget: Container(
width: 100,
height: 100,
decoration: const BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
),
rotateMode: RotateMode.stopRotate,
origin: const Offset(0, 0),
children: elements
.map(
(index) =&gt; GestureDetector(
onTap: () =&gt; _rotate(index),
child: Container(
width: 75,
height: 75,
decoration: const BoxDecoration(
color: Colors.green,
shape: BoxShape.circle,
),
),
),
)
.toList(),
);
}),
),
),
);
}
}

code is here

huangapple
  • 本文由 发表于 2023年5月17日 16:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定