如何使RaisedButton
处于选择状态,如切换按钮选择状态?如何使Flutter RaisedButton选择状态
如何使RaisedButton
不占用额外的宽度,即shrimpWrap标签文本。
如何使RaisedButton
处于选择状态,如切换按钮选择状态?如何使Flutter RaisedButton选择状态
如何使RaisedButton
不占用额外的宽度,即shrimpWrap标签文本。
我相信我已经成功地做同样的事情,而无需使用RaisedButton
。
从我的理解,你正试图使RaisedButton
两种情况之间切换在你的第一个问题,你可以通过两个States
之间交替时onPressed
被称为实现这一目标。
但是,为了回答你的第二个问题,以及,我现在使用的Container
代替RaisedButton
,这Container
充当Button
和攻丝时,状态之间切换。 Container
将自己调整到它的child
,这是Text
,在我的示例中(String
和值)有变化的状态。
最后,为了给这个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;
}
});
},
)
),
);
}
}
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;
}
});
})
],
),
),
);
然而,唯一的原因,我试图使用Container
与GestureDetector
是回答你的第二个问题,我不知道如何使用与RaisedButton
shrinkWrap。