Ошибка щелчка контейнера - PullRequest
0 голосов
/ 28 мая 2020

хорошо? Я разрабатываю приложение, которое поможет мне, но у меня есть ошибка. когда я нажимаю на контейнер, он должен измениться, но теперь я касаюсь контейнера, он ничего не делает, и я не знаю, почему кто-то знает, что может происходить? Спасибо тем, кто мне помогает.

  @override
  Widget build(BuildContext context) {
  bool selected = true;
  return Scaffold(
  bottomNavigationBar: CurvedNavigationBar(
  index: 0,
  height: 75,
  items: <Widget>[
  Icon(Icons.home, size: 30, color: Colors.black), //0
  Icon(Icons.camera_alt, size: 30, color: Colors.black), //1
  Icon(Icons.build, size: 30, color: Colors.black), //2
  ],
  color: Colors.white.withOpacity(0.90),
  buttonBackgroundColor: Colors.white.withOpacity(0.90),
  backgroundColor: selected ? Colors.green.withOpacity(0.90) : Colors.deepOrangeAccent.withOpacity(0.90),
  animationCurve: Curves.easeInOut,
  animationDuration: Duration(
  milliseconds: 550
  ),
  body: Container(
  color: selected ? Colors.green.withOpacity(0.90) : Colors.deepOrangeAccent.withOpacity(0.90),
  child:Column(
  children: <Widget>[
  Expanded(
  flex: 5,
  child: Align(
  child: AnimatedContainer(
  alignment: selected ? Alignment(0, 0.3) : Alignment(0, 0.3),
  duration: Duration(
  milliseconds: 600),
  child: AnimatedContainer(
  width: selected ? 150 : 135,
  height: selected ? 150 : 135,
  color: Colors.transparent,
  duration: Duration(
  milliseconds: 1250),
  curve: Curves.bounceOut,
  child: GestureDetector(
  onTap: () {
  setState(
  () {
  selected = !selected;
  });
  },
  child: Container(
  decoration: BoxDecoration(
  color: selected ? Colors.green.withOpacity(0.90) : Colors.red.withOpacity(0.90),
  borderRadius: selected ? BorderRadius.circular(30) : BorderRadius.circular(40),
  boxShadow: [
  BoxShadow(
  color: Colors.white.withOpacity(0.40),
  spreadRadius: selected ? 10 : 4,
  blurRadius: 50,
  offset: Offset(0, 0),
  ),
  ],
  ),
  child: Center(
  child: Container(
  height: 250,
  width: 250,
  color: Colors.transparent,
  child: selected ? FlareActor(
  "animacoes/open.flr", animation: 'open',
  ): FlareActor(
  "animacoes/close.flr", animation: 'close',
  ),
  ),
  ),
  ),
  ),
  ),
  ),
  ),
  ),
  Expanded(
  flex: 5,
  child:Row(
  children: <Widget>[
  Expanded(
  flex: 10,
  child:Align(
  alignment:Alignment(0, -0.3),
  child: Container(
  width: 250,
  height: 250,
  child: (
  selected ? Text(
  'Portão Fechado',
  style: TextStyle(
  fontWeight: FontWeight.w500,
  fontSize: 30,
  color: Colors.black,
  ),
  textAlign: TextAlign.center,
  ) : Text(
  'Portão aberto',
  style: TextStyle(
  fontWeight: FontWeight.w500,
  fontSize: 30,
  color: Colors.black,
  ),
  textAlign: TextAlign.center,
  )
  ),
  ),
  ),
  ),
  ],
  ),
  ),
  ],
  )
  ),
  );
  }
}

Мой код должен был переключаться между зеленым экраном и красным экраном, но это только на зеленом экране. введите описание изображения здесь

1 Ответ

1 голос
/ 28 мая 2020

Вам нужно переместить этот bool selected = true; из метода build.

Каждый раз, когда вы вызываете setState, вызывается метод build , а selected сбрасывается до true. Вот почему вы не видите изменений.

Итак, если у вас было:

class SomeScreen extends StatefulWidget {
    @override
    _SomeScreenState createState() => _SomeScreenState();
}

class _SomeScreenState extends State<SomeScreen> {
     @override
     Widget build(BuildContext context) {
     bool selected = true;

    // Rest of the code
     }
}

, вы должны изменить это на:

class SomeScreen extends StatefulWidget {
        @override
        _SomeScreenState createState() => _SomeScreenState();
    }

    class _SomeScreenState extends State<SomeScreen> {
         bool selected = true;

         @override
         Widget build(BuildContext context) {

        // Rest of the code
         }
    }
...