Skip to content

Instantly share code, notes, and snippets.

@SuperPenguin
Created December 21, 2022 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuperPenguin/01f7d7053550cb20a73720c57f95ac0f to your computer and use it in GitHub Desktop.
Save SuperPenguin/01f7d7053550cb20a73720c57f95ac0f to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyScreen(),
);
}
}
class MyController with ChangeNotifier {
MyController();
bool _privacyPolicy = false;
bool get privacyPolicy => _privacyPolicy;
set privacyPolicy(bool privacyPolicy) {
if (_privacyPolicy != privacyPolicy) {
_privacyPolicy = privacyPolicy;
notifyListeners();
}
}
bool _tos = false;
bool get tos => _tos;
set tos(bool tos) {
if (_tos != tos) {
_tos = tos;
notifyListeners();
}
}
@override
void dispose() {
// if you need cleanup
super.dispose();
}
}
class MyScreen extends StatefulWidget {
const MyScreen({super.key});
@override
State<MyScreen> createState() => _MyScreenState();
}
class _MyScreenState extends State<MyScreen>
with SingleTickerProviderStateMixin {
late final TabController _tabController;
late final MyController _myController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 2, vsync: this);
_myController = MyController();
}
@override
void dispose() {
_tabController.dispose();
_myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('My Screen'),
bottom: TabBar(
controller: _tabController,
tabs: const [
Tab(text: 'A'),
Tab(text: 'B'),
],
),
),
body: TabBarView(
controller: _tabController,
children: [
A(myController: _myController),
B(myController: _myController),
],
),
);
}
}
class A extends StatelessWidget {
const A({
super.key,
required this.myController,
});
final MyController myController;
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: myController,
builder: (context, child) => Column(
children: [
SwitchListTile.adaptive(
title: const Text('Privacy Policy'),
value: myController.privacyPolicy,
onChanged: (value) {
myController.privacyPolicy = value;
},
),
SwitchListTile.adaptive(
title: const Text('Terms of Service'),
value: myController.tos,
onChanged: (value) {
myController.tos = value;
},
),
],
),
);
}
}
class B extends StatelessWidget {
const B({
super.key,
required this.myController,
});
final MyController myController;
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: myController,
builder: (context, child) => Column(
children: [
ListTile(
title: const Text('Privacy Policy'),
subtitle: Text(myController.privacyPolicy.toString()),
),
ListTile(
title: const Text('Terms of Service'),
subtitle: Text(myController.tos.toString()),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment