Flutter의 여러 버튼에서 하나의 버튼 색상 변경

안킷

flutter를 사용하여 질문 답변 응용 프로그램을 개발 중입니다. 사람이 건너 뛰기 버튼을 클릭하면 해당 질문 번호 버튼이 빨간색으로 변경됩니다. 어떻게해야합니까?

위젯 :

@override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        backgroundColor: Colors.blue,
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: SizedBox(
            height: 600,
            child: Column(
              children: <Widget>[
                Expanded(
                  flex: 2,
                  child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Container(
                width: 40,
                padding: EdgeInsets.all(5),
                child: RaisedButton(onPressed: () {
                  i = -1;
                  ChooseQuestion();
                },
                  color: Colors.deepOrange,
                  textColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                          Radius.circular(10.0))),
                  child: Text("1",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 20
                    ),
                  ),
                ),
              ),
              Container(
                width: 40,
                padding: EdgeInsets.all(5),
                child: RaisedButton(onPressed: () {
                  i = 0;
                  ChooseQuestion();
                },
                  color: Colors.deepOrange,
                  textColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                          Radius.circular(10.0))),
                  child: Text('2',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 20
                    ),
                  ),
                ),
              ),
              Container(
                width: 40,
                padding: EdgeInsets.all(5),
                child: RaisedButton(onPressed: () {
                  i = 1;
                  ChooseQuestion();
                },
                  color: Colors.deepOrange,
                  textColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                          Radius.circular(10.0))),
                  child: Text('3',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 20
                    ),
                  ),
                ),
              ),
            ],
          );
                ),
                Expanded(
                    flex: 3,
                    child: Center(
                      child: Container(
                        width: 400,
                        height: 400,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Container(
                              width: 250,
                              child: Material(
                                elevation: 5,
                                color: Colors.deepOrange,
                                borderRadius: BorderRadius.all(
                                    Radius.circular(10.0)),
                                child: TextField(
                                  enabled: false,
                                  maxLines: 6,
                                  decoration: InputDecoration(
                                      fillColor: Colors.white,
                                      filled: true,
                                      hintText: questions[i]
                                  ),
                                  style: TextStyle(
                                      fontSize: 20,
                                      color: Colors.black
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    )
                ),
                Expanded(
                    flex: 3,
                    child: Center(
                        child: Container(
                            width: 400,
                            height: 400,
                            child: Column(
                                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                children: <Widget>[
                                  Container(
                                    width: 250,
                                    child: Material(
                                      elevation: 2,
                                      color: Colors.deepOrange,
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(10.0)),
                                      child: TextField(
                                        controller: answercontroller,
                                        maxLines: 3,
                                        //enabled: false,
                                        decoration: InputDecoration(
                                            fillColor: Colors.white,
                                            filled: true,
                                            hintText: 'Answer'
                                        ),
                                        style: TextStyle(
                                            fontSize: 20,
                                            color: Colors.black
                                        ),
                                      ),
                                    ),
                                  ),
                                ]
                            )
                        )
                    )
                ),
                Expanded(
                    flex: 1,
                    child: Align(
                      alignment: Alignment.topCenter,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          RaisedButton(onPressed: SkipQuestion,
                            color: Colors.deepOrange,
                            textColor: Colors.white,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.all(
                                    Radius.circular(10.0))),
                            child: Text('Skip',
                              style: TextStyle(
                                  fontSize: 20
                              ),
                            ),
                          ),
                          RaisedButton(onPressed: NextQuestion,
                            color: Colors.deepOrange,
                            textColor: Colors.white,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.all(
                                    Radius.circular(10.0))),
                            child: Text('Next',
                              style: TextStyle(
                                  fontSize: 20
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                )
              ],
            ),
          ),
        ),
      );
  }

SkipQuestion, 특정 버튼의 색상을 변경하려는 기능 :

void SkipQuestion() {
    setState(() {
      if(i < (questions.length - 1)){
        // ignore: unnecessary_statements
        btncolor[(i+1).toString()] == Colors.red;
        i++;
      }
      else{
        Navigator.of(context).pushReplacement(MaterialPageRoute(
          builder: (context) => resultpage(),
        ));
      }
    });
  }

btncolor는 전역 변수로 상태 저장 위젯으로 정의됩니다.

class _quizpageState extends State<quizpage> with SingleTickerProviderStateMixin {

  int i = 0;

  Map<String, Color> btncolor = {
    "1" : Colors.deepOrangeAccent,
    "2" : Colors.deepOrangeAccent,
    "3" : Colors.deepOrangeAccent,
    "4" : Colors.deepOrangeAccent,
    "5" : Colors.deepOrangeAccent,
    "6" : Colors.deepOrangeAccent,
    "7" : Colors.deepOrangeAccent,
    "8" : Colors.deepOrangeAccent,
    "9" : Colors.deepOrangeAccent,
  };

  //The widget code is here which is displayed in the first code

}
Akshay Kumar U

위젯을 다시 빌드 할 때 색상 변경을 적용하려면 버튼에 btnColor 를 사용해야 합니다. 현재 코드에서 Colors.deepOrange로 수동으로 색상을 설정하고 있습니다.

선물 :

RaisedButton(onPressed: SkipQuestion,
                        color: Colors.deepOrange,
                        textColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                                Radius.circular(10.0))),
                        child: Text('Skip',
                          style: TextStyle(
                              fontSize: 20
                          ),
                        ),
                      ),

다음으로 변경하십시오.

RaisedButton(onPressed: SkipQuestion,
                          //colorVariable you want to change the color when you rebuild the widget
                        color: btncolor[index_here],
                        textColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                                Radius.circular(10.0))),
                        child: Text('Skip',
                          style: TextStyle(
                              fontSize: 20
                          ),
                        ),
                      ),

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Flutter에서 여러 버튼의 색상을 차례로 변경

Flutter에서 여러 버튼 중 하나의 버튼 색상을 변경하는 방법은 무엇입니까?

여러 버튼에서 버튼 하나의 색상을 변경하는 방법 Vue

버튼의 표에서 색상 변경

여러 버튼에서 누른 색상의 버튼을 변경하는 방법 React Native

SwiftUI에서 여러 버튼의 배경색 변경

목록에서 onPress시 하나의 버튼 색상 변경

여러 버튼, 클릭 한 버튼 색상을 변경하는 하나의 이벤트

버튼으로 여러 div의 테두리 색상 변경

Ionic을 사용하여 Angular에서 특정 버튼의 버튼 색상을 동적으로 변경합니까?

여러 레이블의 색상을 변경하는 여러 버튼 TKINTER, PYTHON?

Python - 버튼 목록에서 한 버튼의 색상을 변경하는 방법

Android Studio에서 버튼 탭의 버튼 색상을 변경하는 방법

Flutter의 CupertinoNavigationBar에서 뒤로 버튼의 색상을 변경하는 방법

Swift의 UIAlertController에서 하나 또는 특정 버튼의 색상 변경

탐색 버튼 SwiftUI의 색상 변경

HTML의 버튼을 사용하여 SVG 원의 색상 변경

WPF에서 클릭하면 원 버튼의 색상 변경

Flutter에서 버튼 테마의 텍스트 색상을 변경하는 방법

버튼 클릭시 나머지 버튼의 색상 변경

다른 버튼에서 토글 버튼의 상태 변경

여러 버튼의 색상을 변경하는 더 좋은 방법

WebGL에서 버튼을 사용하여 색상 변경

CNContactPicker에서 취소 및 완료 버튼의 색상 변경

AlertDialog에서 확인 버튼의 색상 변경

Android에서 비활성화 된 버튼의 색상 변경

iOS에서 버튼 텍스트의 색상 변경 (objective-c)

Unity3D에서 버튼의 테두리 색상 변경

sass에서 메뉴 토글 버튼의 색상 변경

TOP 리스트

  1. 1

    C # 16 진수 값 0x12는 잘못된 문자입니다.

  2. 2

    Matlab의 반복 Sortino 비율

  3. 3

    Python의 csv 파일에서 첫 번째 열 삭제

  4. 4

    개체 참조가 개체의 인스턴스로 설정되지 않았습니까? (예외 오류 ~ ASP.NET MVC)

  5. 5

    atob은 인코딩 된 base64 문자열을 디코딩하지 않습니다.

  6. 6

    EventEmitter <string>의 컨텍스트 'this'가 Observable <string> 유형의 'this'메서드에 할당되지 않았습니다.

  7. 7

    병합 셀을 사용하여 워크 시트의 데이터 필터링

  8. 8

    PhpStorm 중단 점에서 변수 값을 볼 수 없습니다.

  9. 9

    jQuery에서 이벤트 핸들러를 제거하는 가장 좋은 방법은 무엇입니까?

  10. 10

    `@ Transactional`이 있음에도 불구하고 이러한 데이터베이스 수정 사항이 롤백되지 않는 이유는 무엇입니까?

  11. 11

    ssh를 사용하여 원격에서 로컬로 파일 복사

  12. 12

    종속 사용자 정의 Lightning 선택 목록 Level2 및 Level3을 설정한 다음 Lightning 구성 요소에서 Level2를 재설정하지만 Level2 캐시 데이터가 저장됨

  13. 13

    2 개의 이미지를 단일 평면 이미지로 결합

  14. 14

    팝업처럼 위젯을 표시하는 방법

  15. 15

    [해결] 쿠키 설정 SameSite = Chrome / JSP, JAVASCRIPT에서 작동하지 않습니다.

  16. 16

    버튼 클릭을 기반으로 특정 CSS 클래스를 추가하는 방법은 무엇입니까?

  17. 17

    React 구성 요소가 자동으로 초기 상태로 다시 렌더링됩니다.

  18. 18

    연결된 서버 쿼리는 작동하지만 동일한 OPENQUERY는 "sys.servers에서 서버 'SERVER'를 찾을 수 없습니다.

  19. 19

    파일 2의 파일 1에서 동일한 줄을 조건으로 바꿉니다.

  20. 20

    아이디어 Intellij : 종속성 org.json : json : 20180813을 찾을 수 없음, maven에서 org.json 라이브러리를 가져올 수 없음

  21. 21

    상황에 맞는 메뉴 색상

뜨겁다태그

보관