2017-09-14 113 views

回答

2

我相信我已经成功地做同样的事情,而无需使用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; 
       } 
       }); 
      }) 
      ], 
     ), 
    ), 
    ); 

然而,唯一的原因,我试图使用ContainerGestureDetector是回答你的第二个问题,我不知道如何使用与RaisedButton shrinkWrap。