2017-09-14 3 views
1

토글 버튼 선택 상태에서 을 선택 상태로 만드는 방법은 무엇입니까?Flutter RaisedButton 선택 상태 선택

RaisedButton 만드는 방법 여분의 너비를 사용하지 않으려면, 즉, 새우 레이블 텍스트를 둘러 쌉니다.

enter image description here

답변

2

은 내가 RaisedButton를 사용하지 않고 같은 일을 관리해야 믿습니다.

첫 번째 질문에서 두 가지 경우 사이를 전환하려고 시도하고 있음을 이해 했으므로 onPressed이 호출 될 때 States이 번갈아 표시되면이를 수행 할 수 있습니다.

그러나뿐만 아니라 두 번째 질문에 대답하기 위해, 나는 Button으로 이제, RaisedButton 대신이 Container 행위를 Container을 사용하고 탭 때, 그것은 상태 사이를 전환합니다. 그리고 Containerchild으로 크기가 조정됩니다. 이는 이며 내 예에서는 변경된 상태입니다 (StringColor 값).

마지막으로, 위해이 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을 사용하여 시도한 유일한 이유는 두 번째 질문에 대한 답변 인 것입니다. shrinkWrap을 RaisedButton과 함께 사용하는 방법을 잘 모르겠습니다.

관련 문제