关于flutter:Flutter-GetX-使用教程

66次阅读

共计 996 个字符,预计需要花费 3 分钟才能阅读完成。

一、集成

1、在 https://pub.dev/packages/get 查看以后的最新版本
2、在我的项目的根目录下的 pubspec.yaml 增加如下代码

dependencies:
  get: ^4.1.4

3、关上终端执行 flutter pub get 即可

二、根底配置

更改 main.dart 中的代码为(也就是将默认的 MaterialApp 替换为 GetMaterialApp)

GetMaterialApp(
  navigatorKey: Get.key,
  debugShowCheckedModeBanner: false,
  defaultTransition: Transition.cupertino,
  theme: ThemeData(
      primaryColor: Colors.white,
      highlightColor: Color.fromRGBO(0, 0, 0, 0),
      splashColor: Color.fromRGBO(0, 0, 0, 0)),
      initialRoute: RouteManager.splashPage,
      getPages: AppPages.getPages(),),
  );

三、根底应用(路由、Dialog、BottomSheet)

  • 路由
    新建页面 FirstPage
import 'package:flutter/material.dart';
class FirstPage extends StatelessWidget {
    
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title: Text('FirstPage')),
      body: Container(
        color: Colors.green,
        child:ElevatedButton(
          style: ElevatedButton.styleFrom(primary: CustomColors.appBarBgColor,),
             onPressed: () async {
             // 跳转
             Get.to(SecondPage();
             },
             child: Container(
               alignment: Alignment.center,
               margin: EdgeInsets.symmetric(horizontal: 16),
               width: 260,
               child: Text('to second page'),
              ),
            ),
      ),
    );
  }
}

未完待续~

正文完
 0