Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How do I create a dialog box when it is being created in a separate dart file that isn't tied to the widget tree?

$
0
0

I'm trying to get away from Get (lol) and focus on building my flutter app using the native Navigator. My main.dart sets wrapper.dart as the home page. In wrapper, it returns either home.dart or login.dart, depending on the value of a global variable in a provider class called GlobalsProvider.

If login is successful, GlobalsProvider runs a function called startLoginStream. This starts a timer object that runs every 5 seconds and calls a function to check the login state on a remote SQL Server instance. If the user is remotely logged out, this timer function will notice that and log the user out + show them a dialog box saying that they have been logged out remotely.

Before, I was using the Get package to do this, but I want to see if this can be done natively. However, I don't know how to handle this. I know that a dialog box requires BuildContext, but if my timer function is called once from GlobalsProvider, which can't see the widget tree, then what would I need to modify to make this work? What is the recommended practice here? Below is some relevant code. I won't paste too much to keep it light, but I can answer any questions about other stuff in the app.

main.dart:

void main() {  WidgetsFlutterBinding.ensureInitialized();  runApp(const MyApp());}class MyApp extends StatelessWidget {  const MyApp({super.key});  // This widget is the root of your application.  @override  Widget build(BuildContext context) {    return ChangeNotifierProvider(      create: (context) => GlobalsProvider(),      child: Builder(builder: (BuildContext context) {        return MaterialApp.router(          title: 'myApp',          routerConfig: router,        );      })    );  }}class MyHomePage extends StatefulWidget {  const MyHomePage({super.key, required this.title});  final String title;  @override  State<MyHomePage> createState() => _MyHomePageState();}class _MyHomePageState extends State<MyHomePage> {  @override  Widget build(BuildContext context) {    return const SafeArea(        child: Wrapper()    );  }}

wrapper.dart:

class Wrapper extends StatelessWidget {  const Wrapper({super.key});  @override  Widget build(BuildContext context) {    return Consumer<GlobalsProvider>(builder: (context, value, child) {      if (value.loggedIn == true) {        return const Forms();      } else {        return const Login();      }    });  }}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>