我相信我已经设法在不使用RaisedButton.
根据我的理解,您试图RaisedButton在第一个问题中在两种情况之间进行切换,您可以通过在调用States时在两者之间交替来实现这一点onPressed。
但是,为了同样回答您的第二个问题,我使用了 aContainer而不是 a RaisedButton,它Container现在充当 aButton并且当点击时,它会在状态之间切换。并且 the Containerwill 将自身调整为它的child,Text在我的示例中,它具有不断变化的状态(TheString和Colorvalues)。
最后,为了给这个Container与 的按下功能类似的行为RaisedButton,我将它包裹在一个 中GestureDetector,并控制onTap调用内部状态的变化。
这是我的完整代码:
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp()));
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
Color _myColor = Colors.green;
String _myAccountState = "Account Enabled";
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Manage Accounts"),
centerTitle: true,
),
body: new Center(
child: new GestureDetector(
child: new Container(
decoration: new BoxDecoration(color: Colors.grey),
child: new Text(
_myAccountState, style: new TextStyle(color: _myColor),),
),
onTap: () {
setState(() {
if (_myColor == Colors.green) {
_myAccountState = "Account Disabled";
_myColor = Colors.orange;
}
else {
_myAccountState = "Account Enabled";
_myColor = Colors.green;
}
});
},
)
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
PS:对于开/关行为,您绝对可以使用RaisedButton并产生类似的行为,如下所示:
return new Scaffold(
appBar: new AppBar(
title: new Text("Manage Accounts"),
centerTitle: true,
),
body: new Center(
child: new Column(
children: <Widget>[
new Text(_myAccountState),
new RaisedButton(
child: new Text("Click Me"), color: _myColor, onPressed: () {
setState(() {
if (_myColor == Colors.green) {
_myAccountState = "Account Disabled";
_myColor = Colors.red;
}
else {
_myAccountState = "Account Enabled";
_myColor = Colors.green;
}
});
})
],
),
),
);
Run Code Online (Sandbox Code Playgroud)
但是,我尝试将 aContainer与 aGestureDetector一起使用的唯一原因是为了回答您的第二个问题,我不确定如何将 shrinkWrap 与 a 一起使用RaisedButton。
| 归档时间: |
|
| 查看次数: |
4556 次 |
| 最近记录: |