Home > database >  Unable to navigate from GetX Dialog to another screen
Unable to navigate from GetX Dialog to another screen

Time:03-10

I have follow dialog box. When I click 'Next' I want it to navigate to GamePage() screen. But unfortunately it doesn't work.

await Get.defaultDialog(
  backgroundColor: Colors.transparent,
  title: '',
  barrierDismissible: false,
  content: Container(
    child: Column(
      children: [
        Container(
          width: double.infinity,
          child: Lottie.asset(
              'assets/lottie/success.json'),
        ),
        ElevatedButton(
          onPressed: () {
            Get.to(() => GamePage());
          },
          child: Text("Next Word"),
        )
      ],
    ),
  ),
);

Get.back(); is working though.

CodePudding user response:

Try

ElevatedButton(
  onPressed: () {
  Navigator.push(
      context,
      MaterialPageRoute(
        builder: (BuildContext context) {
          return const GamePage();
        },
      ),
    );
  },
  child: Text("Next Word"),
)

CodePudding user response:

If you want to use GetX navigation system you should wrap your application in a GetMaterialApp instead of MaterialApp.

So in your main use this:

class GetxApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: HomePage(),
    );
  }
}

instead of this:

class NormalApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

CodePudding user response:

Try this code -

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_memory/next_page.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  //check getMaterialApp is used
  runApp(const GetMaterialApp(
    title: 'Temp',
    home: const MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Image Picker'),
      ),
      body: Center(
        child: ElevatedButton(
            onPressed: () {
              print('pressed here');
              Get.defaultDialog(
                  title: 'Go to next page',
                  content: Container(
                    child: Column(
                      children: [
                        Text('You are about to move to another screen'),
                        ElevatedButton.icon(
                            onPressed: () {
                              Get.to(() => NextPage());
                            },
                            icon: Icon(
                              Icons.arrow_right,
                            ),
                            label: Text('Go'))
                      ],
                    ),
                  ));
            },
            child: Text('Open Dialog')),
      ),
    );
  }
}

and next page is -

import 'package:flutter/material.dart';

class NextPage extends StatefulWidget {
  const NextPage({ Key? key }) : super(key: key);

  @override
  State<NextPage> createState() => _NextPageState();
}

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Next Page'),
      ),
      body: Container(
        child: Center(
          child: Text("this is next page"),
        ),
      ),
    );
  }
}

And yes, you need to insure that you are using 'GetMaterialApp'.

  • Related