Created
August 30, 2020 15:10
-
-
Save roipeker/544309b24b3f5cd116ed3b0cd9a42a63 to your computer and use it in GitHub Desktop.
Embarques TabView GetX sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
class EmbarqueModel { | |
final String name; | |
final String id; | |
EmbarqueModel({this.name, this.id}); | |
String get endpoint => '/$id'; | |
} | |
class AppData { | |
final embarques = <EmbarqueModel>[ | |
EmbarqueModel(name: 'Maritimo', id: 'em'), | |
EmbarqueModel(name: 'Aereo', id: 'ea'), | |
EmbarqueModel(name: 'Rodario', id: 'er'), | |
EmbarqueModel(name: 'Todos', id: 'to'), | |
]; | |
final apiResponseDemo = List.generate(20, (index) => 'Item embarque $index'); | |
} | |
// emular um modelo | |
final appData = AppData(); | |
class SampleTabbarDemo extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return GetMaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: TabbarHome(), | |
initialBinding: BindingsBuilder(() { | |
Get.lazyPut(() => TabHomeController()); | |
}), | |
); | |
} | |
} | |
class TabHomeController extends GetxController | |
with SingleGetTickerProviderMixin { | |
final String title = 'Sample TabBar'; | |
final List<EmbarqueModel> embarques = appData.embarques; | |
TabController tabController; | |
final _isLoading = false.obs; | |
bool get isLoading => _isLoading.value; | |
// não precisa ser .obs, porque isLoading define a atualização da IU | |
final embarqueResponse = <String>[]; | |
@override | |
void onInit() { | |
print('tab controller ready'); | |
tabController = TabController(vsync: this, length: embarques.length); | |
} | |
@override | |
onClose() { | |
tabController.dispose(); | |
} | |
void onTabSelect(int index) { | |
print("Tab index: $index"); | |
} | |
void loadEmbarque(String id) async { | |
embarqueResponse.clear(); | |
_isLoading.value = true; | |
await Future.delayed(Duration(seconds: 1)); | |
embarqueResponse.addAll(appData.apiResponseDemo); | |
_isLoading.value = false; | |
} | |
} | |
class TabbarHome extends GetView<TabHomeController> { | |
final _tabs = appData.embarques.map<Tab>((e) => Tab(text: e.name)).toList(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(controller.title), | |
bottom: TabBar( | |
tabs: _tabs, | |
controller: controller.tabController, | |
onTap: controller.onTabSelect, | |
), | |
), | |
body: TabBarView( | |
controller: controller.tabController, | |
children: | |
appData.embarques.map((e) => EmbarquesView(model: e)).toList(), | |
), | |
); | |
} | |
} | |
class EmbarquesView extends GetView<TabHomeController> { | |
final EmbarqueModel model; | |
const EmbarquesView({Key key, this.model}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: GetX<TabHomeController>( | |
builder: (_) { | |
if (controller.isLoading) return _EmbarqueLoading(); | |
final list = controller.embarqueResponse; | |
return ListView.builder( | |
itemBuilder: (ctx, idx) { | |
return ListTile( | |
title: Text( | |
list[idx], | |
style: Get.textTheme.headline6, | |
), | |
); | |
}, | |
itemCount: list.length, | |
); | |
}, | |
initState: (_) { | |
controller.loadEmbarque(model.id); | |
}, | |
// dispose: (_) => controller.clearEmbarque(), | |
), | |
); | |
} | |
} | |
@override | |
class _EmbarqueLoading extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return Center( | |
child: Container( | |
width: 100, | |
height: 100, | |
decoration: BoxDecoration( | |
color: Colors.white, borderRadius: BorderRadius.circular(12)), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
'Loading...', | |
style: Get.textTheme.caption, | |
), | |
SizedBox(height: 20), | |
SizedBox( | |
width: 28, | |
height: 28, | |
child: CircularProgressIndicator( | |
valueColor: AlwaysStoppedAnimation<Color>(Colors.deepPurple), | |
), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
### An error occurred switching the TabView
