provider, bloc, getx 상태 관리 라이브러리 중 나는 getx를 선택 했으며 머릿속에 잘 들어가지 않아서 계속적인 테스트를 진행하고 있다.
이 글은 내가 getx를 익숙해지기 위해서 정리를 위한 글이다.
Navigator.push(context, MaterialPageRoute(builder:(context) => SecondPage());
Navigator.pop(context);
Get.to(SecondPage());
Get.toNamed('/second');
Get.back();
🙀 main.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_tutorial/home_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const GetMaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
🙀 home_page.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_tutorial/second_page.dart';
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
ElevatedButton(
onPressed: () {
// Get.to(const SecondPage()); //페이지 이동(back 가능)
Get.off(const SecondPage()); //나를 지우고 페이지 이동(back 불가능)
},
child: const Text('Route to Second Page'),
)
],
),
),
);
}
}
🙀 second_page.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class SecondPage extends StatelessWidget {
const SecondPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Page'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
ElevatedButton(
onPressed: () {
Get.back();
},
child: const Text('Get Back to First Page'),
)
],
),
),
);
}
}
😺 main.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_tutorial/home_page.dart';
import 'package:getx_tutorial/second_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
// home: const HomePage(),
getPages: [
GetPage(name: '/', page: () => const HomePage()),
GetPage(name: '/second', page: () => const SecondPage()),
],
initialRoute: '/',
);
}
}
😺 home_page.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_tutorial/second_page.dart';
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
ElevatedButton(
onPressed: () {
// Get.to(const SecondPage());
// Get.off(const SecondPage());
Get.toNamed('/second');
// Get.offNamed('/second');
},
child: const Text('Route to Second Page'),
)
],
),
),
);
}
}
페이지 이동 만큼은 정말 간단하게 가능해졌다.