簡體   English   中英

如何在 flutter 中使用 slider 按鈕時更改文本的不透明度和圖標的角度

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

我希望當我將 slider 按鈕向右移動時,文本的不透明度會降低,並且箭頭圖標會完全相反地旋轉,即它會旋轉,最后它應該向后指向。 我想使用 opacity 和 Transform.rotate 小部件,但是我如何不斷更新 dx 的值,所以我可以將它除以容器的總寬度並使用分數進行計算。

如果有其他方法,請告訴我。

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

您可以使用Slider框架中的 Slider 小部件並更新onChange function 中的局部變量:

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

以及您將在OpacityTransform小部件中使用的_value變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM