繁体   English   中英

在 flutter 中使用 GetX package 的导航问题

[英]Navigation problem using GetX package in flutter

我在 flutter 项目中使用 GetX package 进行导航,但它不适用于我。 它在调试控制台中也显示了一些问题 - “意外格式,您只能在此处使用小部件和小部件功能”。

下面是我的 main.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),),
      ),
    );
  }
}

我试过但找不到错误。

在你的 main.dart 文件中,你可以直接添加这个:

void main() { runApp(const Login()); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM