在Flutter中使用GetX包的导航问题

fzsnzjdm  于 2023-06-24  发布在  Flutter
关注(0)|答案(2)|浏览(156)

我正在使用GetX包的导航目的在flutter项目,但它是不工作的我。调试控制台中也显示了一些问题-“意外的格式,您只能在此处使用小部件和小部件功能”。
下面是我的主要. dart文件-

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jarvis/networking/login.dart';

void main() {
  runApp(const MyApp());
  //Get.to(Login());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData('Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _loginScreen(){
    Get.to(Login());
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
      //Get.to(Login());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            TextButton(onPressed: _loginScreen, child: const Text("Press here to go to next page", style: TextStyle(fontSize: 20),)),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

每当用户单击文本按钮时,假定将出现登录屏幕,但UI中没有变化。下面是我的login_screen. dart文件-

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart';

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

  @override
  Widget build(BuildContext context) {
    return const Material(
      child: GetMaterialApp(
        title: "Login Screen",
        home: GetOtp(),
      ),
    );
  }
}

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

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

class _GetOtpState extends State<GetOtp> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Login"),
      ),
      body: const Center(
        child: Text("hi here ankit", style: TextStyle(color: Colors.red, fontSize: 20),),
      ),
    );
  }
}

我试过了,但找不到错误。

nwwlzxa7

nwwlzxa71#

在主.dart文件中,您可以直接添加以下内容:
void main() {runApp(const Login());}

sirbozc5

sirbozc52#

我也面临着同样的问题,通过下面的修改,我的错误消失了,
在你的main.dart文件中,将mian函数改为:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

你的changeScreen函数应该看起来像这样:

changeScreen() {
    Future.delayed(const Duration(seconds: 3), () {
      Get.offAll(const LoginScreen());
    });
  }

相关问题