jam*_*mes 3 togglebutton flutter flutter-layout flutter-widget
我是颤振新手,我需要自定义一个切换按钮,如下所示,任何人都可以帮助我吗?我使用堆栈并定位两个按钮重叠,但当它们更改移动显示时,请帮助我关于这一点。图像
Jac*_*hen 14
希望它可以帮助你:
import 'package:flutter/material.dart';
class ToggleButton extends StatefulWidget {
@override
_ToggleButtonState createState() => _ToggleButtonState();
}
const double width = 300.0;
const double height = 60.0;
const double loginAlign = -1;
const double signInAlign = 1;
const Color selectedColor = Colors.white;
const Color normalColor = Colors.black54;
class _ToggleButtonState extends State<ToggleButton> {
double xAlign;
Color loginColor;
Color signInColor;
@override
void initState() {
super.initState();
xAlign = loginAlign;
loginColor = selectedColor;
signInColor = normalColor;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: Center(
child: Container(
width: width,
height: height,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.all(
Radius.circular(50.0),
),
),
child: Stack(
children: [
AnimatedAlign(
alignment: Alignment(xAlign, 0),
duration: Duration(milliseconds: 300),
child: Container(
width: width * 0.5,
height: height,
decoration: BoxDecoration(
color: Colors.lightGreen,
borderRadius: BorderRadius.all(
Radius.circular(50.0),
),
),
),
),
GestureDetector(
onTap: () {
setState(() {
xAlign = loginAlign;
loginColor = selectedColor;
signInColor = normalColor;
});
},
child: Align(
alignment: Alignment(-1, 0),
child: Container(
width: width * 0.5,
color: Colors.transparent,
alignment: Alignment.center,
child: Text(
'Login',
style: TextStyle(
color: loginColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
GestureDetector(
onTap: () {
setState(() {
xAlign = signInAlign;
signInColor = selectedColor;
loginColor = normalColor;
});
},
child: Align(
alignment: Alignment(1, 0),
child: Container(
width: width * 0.5,
color: Colors.transparent,
alignment: Alignment.center,
child: Text(
'Signin',
style: TextStyle(
color: signInColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10531 次 |
| 最近记录: |