Created
December 21, 2022 02:50
-
-
Save SuperPenguin/01f7d7053550cb20a73720c57f95ac0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
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