【问题标题】:Flutter how to show AlertDialog on particular page onlyFlutter 如何仅在特定页面上显示 AlertDialog
【发布时间】:2022-01-09 15:47:11
【问题描述】:

我正在用颤振构建一个国际象棋时钟。当我在设置页面中更改时钟持续时间时,我想要求用户使用警报对话框进行确认。我希望警报对话框现在只出现在主页中,它会在我在设置页面中更改持续时间后立即出现。

当我更改持续时间时,我正在使用提供程序来管理状态,我将布尔值更改为 true。然后在主页中检查布尔值是否为真,然后运行警报对话框代码。我尝试将对话框代码放入future delay.Zero,但没有成功。

现在主页在我更改设置并弹出警报对话框后立即重建。

我的代码:

// home page code
class HomePage extends StatefulWidget {
  const HomePage({
    Key? key,
  }) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}


class _HomePageState extends State<HomePage>
    with TickerProviderStateMixin, WidgetsBindingObserver {

// code for initstate and logic

late AnimationController _whiteControl;
  late AnimationController _blackControl;

  int whiteTime = 1;
  int blackTime = 1;

 @override
  void initState() {
    super.initState();

    WidgetsBinding.instance!.addObserver(this);

    _whiteControl = AnimationController(
      vsync: this,
      duration: Duration(seconds: whiteTime * 60),
    );

    _blackControl = AnimationController(
      vsync: this,
      duration: Duration(seconds: blackTime * 60),
    );

@override
  void dispose() {
    WidgetsBinding.instance!.removeObserver(this);
    _whiteControl.dispose();
    _blackControl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var gameState = Provider.of<GameState>(context, listen: false);
    final ChoiceDialog resetChoice = ChoiceDialog(
      buttonOkOnPressed: () {
        _blackControl.reset();
        _whiteControl.reset();
        gameState.isgameChanged = false;
        Navigator.pop(context);
      },
      buttonCancelOnPressed: () {
        gameState.isgameChanged = false;
        Navigator.pop(context);
      },
    );

    if (gameState.isGameStarted && gameState.isgameChanged) {
      Future.delayed(Duration.zero, () {
        print('???? Game Changed ');
        resetChoice.show(context);
      });
    }
 print('???? Rebuilding Home Page ---');
    return Scaffold();
//reset of homepage code

设置页面代码:


class SettingsPage extends StatelessWidget {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
                itemCount: gamesList.length,
                itemBuilder: (context, index) {
                  return Consumer<GameState>(
                    builder: (context, _gameState, child) {
                      return GestureDetector(
                        onTap: () {
                          _gameState.changeGame(gamesList[index]);
                        },
                        child: Container(),
                      );
                    },
                  );
                }),
          )
        ],
      ),
    );
  }
}

提供者状态逻辑:


class Game {
  final String name;
  final int whiteTime;
  final int blackTime;

  Game({
    required this.name,
    required this.whiteTime,
    required this.blackTime,
  });
}

class GameState with ChangeNotifier {
  bool turnWhite = false;
  bool turnBlack = false;
  bool isgameChanged = false;
  bool isClocksFinished = false;
  bool isPaused = false;
  bool isGameStarted = false;
  bool resetGame = false;

  int white = 0;
  int black = 0;

  Game _currentGame = gamesList[0];

  Game get currentGame => _currentGame;

  bool get isWhiteTurn => turnWhite;

  void resetGameClocks() {
    resetGame = true;
    notifyListeners();
  }

  void setTurnWhite() {
    turnWhite = true;
    turnBlack = false;
    isGameStarted = true;
    notifyListeners();
  }

  void setTurnBlack() {
    turnWhite = false;
    turnBlack = true;
    isGameStarted = true;
    notifyListeners();
  }

  void changeGame(Game game) {
    _currentGame = game;
    isgameChanged = true;
    white = game.whiteTime;
    black = game.blackTime;
    notifyListeners();
  }
}

项目仓库:https://github.dev/tonydavidx/chess-clock-app/tree/dev

【问题讨论】:

    标签: flutter dart flutter-animation


    【解决方案1】:

    我已经检查了你的代码,你在主页屏幕的初始点/起点犯了一个错误,请避免这种情况

    如果您确实需要在开始时执行一些逻辑或代码,则永远不要在返回小部件的构建函数之前初始化或放置您的代码,然后将其放入 initstate() 或您的提供程序构造函数中并正确处理。

    这是您所面临问题的根本原因。

    【讨论】:

    • 我在initstate 中初始化了动画控制器,当我想重置持续时间时,我会在每次页面构建时检查特定条件是否为真并运行重置代码警报对话框。请查看我的项目仓库:github.dev/tonydavidx/chess-clock-app/tree/dev
    • 将重置代码逻辑放入 initstate 并正确管理状态所有问题都与您的状态管理技术有关,或者更短的状态是泄漏;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2023-01-31
    • 2016-11-12
    • 1970-01-01
    • 2022-01-09
    • 2019-06-25
    相关资源
    最近更新 更多