在 Flutter 中使用 ToggleButton 在同一位置同时在两个有状态小部件之间切换

Change Between two Stateful Widgets while staying in the Same Location using a ToggleButton in Flutter

我的应用程序中有一个 ToggleButtons 小部件,当用户单击切换按钮时,我想在两个不同的 StatefulWidget 类(两者都相当复杂)之间切换。我试过使用 animatedCrossFade 但是我无法在两个屏幕之间切换。

AnimatedCrossFade(
              duration: const Duration(seconds: 3),
              firstChild: AppointmentMain(),
              secondChild: RequestPage(),
              crossFadeState:
                  _first ? CrossFadeState.showFirst : CrossFadeState.showSecond,
            )

尝试使用AnimatedSwitcher在两个不同的Widget之间切换。

AnimatedSwitcher(
      duration: Duration(seconds: 3),
      child: _first? AppointmentMain() : RequestPage(),
    )

有一个使用按钮加载单独小部件的主小部件。

void main() {
   runApp(MaterialApp(
 home: myApp(),
 ));

class myApp extends StatefulWidget {
  @override
  _myAppState createState() => _myApp();
}

class _myAppState extends State<Home> {

 int counter = 1;

  return Scaffold(
   body: RaisedButton(
     on pressed loadFirstWidget
   ),
 );
}

Widget loadWidget() {
 setState() {
  counter += 1;
 }

 if (counter % 2 == 0) {
    return firstWidget();
  }
  return secondWidget();
}

希望这对您有所帮助!