gpt4 book ai didi

flutter - 如何在 Flutter 中创建切换/切换按钮?

转载 作者:行者123 更新时间:2023-12-04 14:44:15 25 4
gpt4 key购买 nike

我正在寻找一种方法来添加 toggel/switch Flutter 中的按钮,但到目前为止还没有找到想要的结果。我正在创建 layout对于 APP因为它在附加的屏幕截图中,但卡在 toggle 上/switch按钮部分将切换 APP 的语言.
enter image description here

最佳答案

以下是如何实现您想要的切换设计:

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class AnimatedToggle extends StatefulWidget {
final List<String> values;
final ValueChanged onToggleCallback;
final Color backgroundColor;
final Color buttonColor;
final Color textColor;

AnimatedToggle({
@required this.values,
@required this.onToggleCallback,
this.backgroundColor = const Color(0xFFe7e7e8),
this.buttonColor = const Color(0xFFFFFFFF),
this.textColor = const Color(0xFF000000),
});
@override
_AnimatedToggleState createState() => _AnimatedToggleState();
}

class _AnimatedToggleState extends State<AnimatedToggle> {
bool initialPosition = true;
@override
Widget build(BuildContext context) {
return Container(
width: Get.width * 0.6,
height: Get.width * 0.13,
margin: EdgeInsets.all(20),
child: Stack(
children: <Widget>[
GestureDetector(
onTap: () {
initialPosition = !initialPosition;
var index = 0;
if (!initialPosition) {
index = 1;
}
widget.onToggleCallback(index);
setState(() {});
},
child: Container(
width: Get.width * 0.6,
height: Get.width * 0.13,
decoration: ShapeDecoration(
color: widget.backgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(Get.width * 0.1),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: List.generate(
widget.values.length,
(index) => Padding(
padding: EdgeInsets.symmetric(horizontal: Get.width * 0.05),
child: Text(
widget.values[index],
style: TextStyle(
fontFamily: 'Rubik',
fontSize: Get.width * 0.045,
fontWeight: FontWeight.bold,
color: const Color(0xAA000000),
),
),
),
),
),
),
),
AnimatedAlign(
duration: const Duration(milliseconds: 250),
curve: Curves.decelerate,
alignment:
initialPosition ? Alignment.centerLeft : Alignment.centerRight,
child: Container(
width: Get.width * 0.33,
height: Get.width * 0.13,
decoration: ShapeDecoration(
color: widget.buttonColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(Get.width * 0.1),
),
),
child: Text(
initialPosition ? widget.values[0] : widget.values[1],
style: TextStyle(
fontFamily: 'Rubik',
fontSize: Get.width * 0.045,
color: widget.textColor,
fontWeight: FontWeight.bold,
),
),
alignment: Alignment.center,
),
),
],
),
);
}
}
使用我们的 AnimatedToggle :
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:playground/animated_toggle.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Toggle Button'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _toggleValue = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Toggle Button'),
elevation: 10,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedToggle(
values: ['English', 'Arabic'],
onToggleCallback: (value) {
setState(() {
_toggleValue = value;
});
},
buttonColor: const Color(0xFF0A3157),
backgroundColor: const Color(0xFFB5C1CC),
textColor: const Color(0xFFFFFFFF),
),
Text('Toggle Value : $_toggleValue'),
],
),
),
);
}
}
结果:
enter image description here

关于flutter - 如何在 Flutter 中创建切换/切换按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65879501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com