简体   繁体   English

如何在 flutter 中使用 slider 按钮时更改文本的不透明度和图标的角度

[英]How to change opacity of text and angle of icon while using slider button in flutter

I want that as I move my slider button towards right, the opacity of text decreases and arrow icon rotates exactly oppposite, ie it strts rotating and at last last it should point backwards.我希望当我将 slider 按钮向右移动时,文本的不透明度会降低,并且箭头图标会完全相反地旋转,即它会旋转,最后它应该向后指向。 I want to use opacity and Transform.rotate widgets, but how do I keep updating the value of dx,so I can divide it with total width of container and use the fraction for calculation.我想使用 opacity 和 Transform.rotate 小部件,但是我如何不断更新 dx 的值,所以我可以将它除以容器的总宽度并使用分数进行计算。

If there is another way, please do tell me.如果有其他方法,请告诉我。

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,
              );
            },
          ),
        ],
      ),
    );
  }
}*/

You can use Slider widget from Flutter framework and update a local variable in the onChange function:您可以使用Slider框架中的 Slider 小部件并更新onChange function 中的局部变量:

Slider(
      value: _currentSliderValue,
      max: 100, //or any max value you need
      onChanged: (double value) {
        setState(() {
          _value = value;
        });
      },
    );

And the _value variable you will use in Opacity and Transform widgets.以及您将在OpacityTransform小部件中使用的_value变量。

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

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