首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在颤振中使用滑块按钮改变文本的不透明度和图标的角度

如何在颤振中使用滑块按钮改变文本的不透明度和图标的角度
EN

Stack Overflow用户
提问于 2022-01-14 13:08:59
回答 1查看 166关注 0票数 0

我希望当我将滑块按钮向右移动时,文本的不透明度会降低,箭头图标会完全相反地旋转,也就是说,它会旋转,最后它应该向后指向。我想使用不透明度和Transform.rotate小部件,但是如何不断更新dx的值,这样我就可以用容器的总宽度来除以它,并使用这个分数来计算。

如果有别的办法,请告诉我。

代码语言:javascript
复制
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:passenger_flutter_app/utils/colors.dart';
import 'package:passenger_flutter_app/widgets/custom_sliding_button.dart';

class CommonSwipeButton extends StatelessWidget {
  final String? buttonText1;
  final String buttonText2;
  final VoidCallback buttonCallBack2;
  final bool isInfo;
  final VoidCallback? buttonCallBack1;

  final Widget itemWidget;

  CommonSwipeButton(
      {this.buttonCallBack1,
        required this.buttonCallBack2,
        this.isInfo = false,
        this.buttonText1,
        required this.buttonText2,
        required this.itemWidget});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        //crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Padding(padding: const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 16.0, top: 16), child: itemWidget),
          Padding(
            padding:
            const EdgeInsets.only(bottom: 16.0, left: 16.0, right: 16.0),
            child: Align(
              alignment: Alignment.center,
              child: SizedBox(
                width: MediaQuery.of(context).size.width,
                height: 44,
                child: CustomSlidingButton(
                  //text: buttonText2,
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

/*
class SwipeButton extends StatefulWidget {
  final ValueChanged<double>? valueChanged;
  final String? text;
  final Function? callBack;

  SwipeButton({this.valueChanged, this.text, this.callBack});

  @override
  SwipeButtonState createState() {
    return new SwipeButtonState();
  }
}

class SwipeButtonState extends State<SwipeButton> {
  ValueNotifier<double> valueListener = ValueNotifier(.0);
  GlobalKey swipeKey = GlobalKey();
  ValueNotifier<double> x=ValueNotifier<double>(0);
  ValueNotifier<bool> isVisible = ValueNotifier<bool>(true);

  @override
  void initState() {
    valueListener.addListener(notifyParent);
    super.initState();
  }

  void notifyParent() {
    if (widget.valueChanged != null) {
      widget.valueChanged!(valueListener.value);
    }
  }

  void getPos(double totalSize) {
    RenderBox box = swipeKey.currentContext?.findRenderObject() as RenderBox;
    Offset position = box.localToGlobal(Offset.zero); //this is global position
    x.value = position.dx;
    print(x);
    if(x.value>355) {
      print("Reached");
      isVisible.value=false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: colorPrimary,
      height: 40.0,
      padding: EdgeInsets.symmetric(horizontal: 10.0),
      child: Stack(
        children: [
          Center(
            child: Padding(
              padding: const EdgeInsets.only(left: 10.0),
              child: Text(
                "${widget.text}",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 17,
                ),
              ),
            ),
          ),
          Builder(
            builder: (context) {
              final handle = GestureDetector(
                onHorizontalDragUpdate: (details) {
                  valueListener.value = (valueListener.value +
                      details.delta.dx / context.size!.width)
                      .clamp(.0, 1.0);
                  getPos(context.size!.width-5);
                  print(context.size?.width);
                },
                child: ValueListenableBuilder(
                  valueListenable: isVisible,
                  builder: (BuildContext context, bool val, Widget? child) {
                    return Container(
                      key: swipeKey,
                      height: 25.0,
                      width: 25.0,
                      color: val ? Colors.white : colorPrimary,
                      child: Center(
                        child: ValueListenableBuilder(
                          valueListenable: x,
                          builder: (BuildContext context, double d, Widget? child) {
                            return Transform.rotate(
                              angle: -pi*(d/350),
                              child: Icon(
                                Icons.arrow_forward,
                                color: Colors.orange,
                                size: 12,
                              ),
                            );
                          },
                        ),
                      ),
                    );
                  },
                ),
              );

              return AnimatedBuilder(
                animation: valueListener,
                builder: (context, child) {
                  return Align(
                    alignment: Alignment(valueListener.value * 2 - 1, 0),
                    child: child,
                  );
                },
                child: handle,
              );
            },
          ),
        ],
      ),
    );
  }
}*/
EN

回答 1

Stack Overflow用户

发布于 2022-01-14 13:52:27

您可以使用颤振框架中的Slider小部件,并在onChange函数中更新局部变量:

代码语言:javascript
复制
Slider(
      value: _currentSliderValue,
      max: 100, //or any max value you need
      onChanged: (double value) {
        setState(() {
          _value = value;
        });
      },
    );

以及您将在OpacityTransform小部件中使用的Transform变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70711053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档