Flutter GetX 使用教程

一、集成

1、在pub.dev/packages/ge…查看当前的最新版本 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'),
              ),
            ),
      ),
    );
  }
}
复制代码

未完待续~

猜你喜欢

转载自juejin.im/post/7019188652244729886