所以我想给我正在开发的应用程序增加一点情趣,我有一个非常好和简单的 idea .现在有一个年龄屏幕页面,看起来像这样:(参见下面的img).我希望这个年龄屏幕在年龄滑块和继续按钮之间有一个婴儿(听我说完).当用户向上移动滑块时,婴儿会逐渐变老,而当滑块向下移动时,婴儿会逐渐变老.所以当滑块为10时,它会显示一个小男孩,当滑块为90时,它会显示一个老人.我希望这是一个平稳的过渡,它可以只是一个剪影(这是最好的工作).什么是最好的方式来做到这一点,如果你们中的任何人可以提供一个有效的代码片段,我将欠你们很多.谢谢!该代码是用于屏幕位于图像上方的.

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workout_app/Screens/Components/Sign_Up_Screens/screen1.dart';
import 'package:workout_app/Screens/Components/Sign_Up_Screens/screen3.dart';

class screen2 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => Main();
}

class Main extends State<screen2> {
  void returnScreen(context) {
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        fullscreenDialog: true,
        builder: (context) => screen1(),
      ),
    );
  }

    
  @override
  void initState() {
    loadData();
    super.initState();
  }

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

  @override
  int age = 10;
  bool nextValid = false;

  void toast(String text) {
    final scaffold = ScaffoldMessenger.of(context);
    scaffold.showSnackBar(
      SnackBar(
        width: MediaQuery.of(context).size.width * .5,
        behavior: SnackBarBehavior.floating,
        content: Text(text),
        duration: const Duration(seconds: 1),
        elevation: 10,
      ),
    );
  }

  void submitData() async {
    if(nextValid == false) {
      toast('Please input your age');
      return;
    }
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt('age', age);
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        fullscreenDialog: true,
        builder: (context) => SingleSelectListViewWithLogo(items : ['Tone Up - You want visible muscles with as little mass as possible and a low body fat percentage', 'Bulk Up - You want large, defined muscles, with a low percentage of body fat', 'Get Jacked - You want to lift an insane amount of weight and don\'t care about body fat or muscle definition']),
      ),
    );
  }

  void loadData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    try {
      int? x = prefs.getInt('age');
      print(x);
      if(x != null) {
        setState(() => {age = x, nextValid = true});
      }
    } catch (Exception){
      //continue;
    }
  }

  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Material(
      child: Scaffold(
      body: 
        Container (
          decoration: const BoxDecoration(color: Color.fromARGB(255, 94, 94, 94)),
          height: size.height,
          width: double.infinity,
          child: Stack(
            children: <Widget> [
              Positioned(
                top: size.height * .06,
                left: size.width * .03,
                child: InkWell(
                  onTap: () {
                    returnScreen(context);
                  },
                  child: Image.asset(
                    alignment: Alignment.topLeft,
                    "assets/images/back2.png",
                    width: size.width * .07,
                  ),
                ),
              ),
              Positioned (
                top: size.height * .33,
                width: size.width * .9,
                height: size.height * .47,
                left: size.width * .05,
                child: FittedBox(
                  fit: BoxFit.fill,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(20.0),
                    child: Image.asset(
                      "assets/images/screen2background.png",
                    ),
                  ),
                )
              ), 
              Positioned(
                top: size.height * .09,
                left: size.width * .42,
                child: const Text(
                  style: TextStyle(fontSize: 30, color: Color.fromARGB(255, 4, 3, 3)),
                  'Next,'
                )
              ),
              Positioned(
                top: size.height * .15,
                left: size.width * .16,
                child: const Text(
                  style: TextStyle(fontSize: 20, color: Color.fromARGB(255, 42, 42, 42)),
                  'Lets customize your workout!'
                )
              ),
              Positioned(
                top: size.height * .35,
                left: size.width * .15,
                child: const Text(
                  style: TextStyle(fontSize: 25, color: Color.fromARGB(255, 0, 0, 0), fontWeight: FontWeight.bold),
                  'What\'s your current age?'
                )
              ),
              Positioned (
                top: size.height * .5,
                left: size.width * .1,
                child: SliderTheme(
                  data: const SliderThemeData(
                    trackHeight: 30,
                    inactiveTrackColor: Color.fromARGB(255, 255, 255, 255),
                    activeTrackColor: Color.fromARGB(255, 0, 0, 0),
                    thumbColor: Colors.black,
                    disabledActiveTrackColor: Colors.black,
                    disabledInactiveTrackColor: Colors.black12,
                    thumbShape: RoundSliderThumbShape(enabledThumbRadius: 25.0),
                  ),
                  child: Container (
                    width: size.width * .8,
                    child: Slider(
                      label: "Select Age",
                      value: age.toDouble(),
                      onChanged: (value) {
                        setState(() {
                          age = value.toInt();
                          nextValid = true;
                        });
                      },
                      min: 10,
                      max: 99,
                    )
                  )
                )
              ),
              Positioned (
                top: size.height * .42,
                left: size.width * .28,
                child: Text(
                  age.toString(),
                  style: const TextStyle(
                    fontSize: 40.0,
                  ),
                ),
              ),
              Positioned (
                top: size.height * .44,
                left: size.width * .42,
                child: const Text (
                  'years old',
                  style: TextStyle(
                    fontSize: 25.0,
                  ),
                )
              ),
              Positioned(
                top: size.height * .66,
                left: size.width * .1,
                child: SizedBox(
                  width: size.width * .8,
                  height: size.height * .08,
                  child: ElevatedButton(
                    style: ButtonStyle(                  
                      backgroundColor: MaterialStateProperty.all<Color>(!nextValid ? Color.fromRGBO(69, 75, 85, 1) : Color.fromARGB(255, 23, 100, 22)),
                    ),
                    child: const Text('Continue',
                      style: TextStyle(fontSize: 20),
                    ),
                    onPressed: () async {
                      submitData();
                    },
                  ),
                ),
              ),
            ]
          )
        )
      )
    );
  }
}

Age Screen

推荐答案

State级开始时具有assets资源 位置的状态:

String assetName = 'assets/images/pic.png';

然后在加载Image.asset的位置下方,使用上面创建的assetName来加载图像:

Center(
  child: AnimatedContainer(
    duration: const Duration(seconds: 1),
    curve: Curves.bounceIn,
    height: age * 2.toDouble(),
    child: Image.asset(
      assetName,
    ),
  ),
),

然后,在获得Slider的更新值的位置,判断该值并相应地更新assetName:

Slider(
  label: "Select Age",
  value: age.toDouble(),
  onChanged: (value) {
    setState(() {
      age = value.toInt();
      nextValid = true;
    });
    if (value > 25) {
      setState(() {
        assetName = 'assets/images/rotated.png';
      });
    } else {
      setState(() {
        assetName = 'assets/images/pic.png';
      });
    }
  },
  min: 10,
  max: 99,
)

Final result

关于Flutter 中的StatefulWidget的更多信息:StatefulWidget class

Ios相关问答推荐

带有动画层的UIView表示不会在工作表中设置动画

TrueDepth摄像机像素距离不准确

SwiftUI绑定到特定的一个或多个枚举 case

SwiftUI标题/标题文本背景 colored颜色 屏幕宽度

部分保留 UITextField 中的占位符

为什么这个 init 被分派到一个内部扩展 init?

具有一个参数/参数的泛型类的泛型类

滚动 swiftUI 列表时,未调用单元格的任务修饰符.怎么修?

过渡动画在 iOS16 中不起作用,但在 iOS15 中起作用

有没有办法访问自动生成的 Codable 一致性的编码键?

如何在不支持并发的自动关闭中修复'async'调用?

UIImageView 使用 CGAffineTransform 错误旋转

Swift 共享数据模型在页面之间进行通信.这个怎么运作

使用swift将图像转换为A4大小的pdf

使用 AVAudioPlayer 播放声音

UITableView 背景 colored颜色 在 iPad 上总是白色

Xcode 8:函数类型不能有参数标签 destruct 我的构建

如何在ios中生成UUID

如何在 iOS 中获取正在运行的应用程序的名称

iPhone UITableView.如何开启音乐 App 之类的单字母字母列表?