kcwu229 / Mobile-App-BlackJack-Game

4 stars 0 forks source link

Adding dialog on the game and auto dismiss #25

Closed kcwu229 closed 2 months ago

kcwu229 commented 3 months ago

Q: i want to add a dialog for my black game to simulate the player to speak, and no need to click close to end the dialog, which would auto disappear after 1 to 2 second

A: import 'package:flutter/material.dart';

void main() { runApp(MyApp()); }

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Dialog Box Example', home: HomePage(), ); } }

class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); }

class _HomePageState extends State { @override void initState() { super.initState(); showDialog(); }

void showDialog() { Future.delayed(Duration(seconds: 1), () { showGeneralDialog( context: context, pageBuilder: (context, animation, secondaryAnimation) { return Align( alignment: Alignment.center, child: Container( width: 300, height: 150, child: Card( child: Padding( padding: EdgeInsets.all(16.0), child: Text('This is a dialog box that will disappear after 1-2 seconds.'), ), ), ), ); }, transitionDuration: Duration(milliseconds: 500), transitionBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: animation, child: child, ); }, ); });

Future.delayed(Duration(seconds: 3), () {
  Navigator.of(context).pop();
});

}

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Dialog Box Example'), ), body: Center( child: Text('Click to show dialog'), ), ); } }