Advertisement
Guest User

Untitled

a guest
Aug 7th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3.  
  4. Future<void> main() async {
  5. runApp(const AppMain());
  6. }
  7.  
  8. class AppMain extends StatelessWidget {
  9. const AppMain({Key? key}) : super(key: key);
  10.  
  11. @override
  12. Widget build(BuildContext context) {
  13. return const MaterialApp(
  14. debugShowCheckedModeBanner: false,
  15. home: S1(),
  16. );
  17. }
  18. }
  19.  
  20. class S1 extends StatelessWidget {
  21. const S1({Key? key}) : super(key: key);
  22.  
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: AppBar(),
  27. body: ChangeNotifierProvider(
  28. create: (context) => ViewModelTest(),
  29. builder: (context, vm) => Column(
  30. children: [
  31. Expanded(child: S2A()),
  32. const S2B(),
  33. ],
  34. ),
  35. ),
  36. );
  37. }
  38. }
  39.  
  40. class S2A extends StatelessWidget {
  41. S2A({Key? key}) : super(key: key);
  42. final List<GlobalKey> _keyList = List.generate(100, (index) => GlobalKey()).toList();
  43.  
  44. @override
  45. Widget build(BuildContext context) {
  46. /// ここでメソッドをviewModelに入れている事に強い違和感がある。
  47. final ViewModelTest vm = context.read();
  48. vm.scrollRequestCallback = (int index) {
  49. scrollRequest(index);
  50. };
  51. return Column(
  52. children: [
  53. Consumer<ViewModelTest>(
  54. builder: (context, vm, child) {
  55. return Container(
  56. padding: const EdgeInsets.all(10),
  57. decoration: BoxDecoration(
  58. border: Border.all(color: Colors.grey, width: 1),
  59. ),
  60. child: Text("VMから受信した値=${vm.counter}"),
  61. );
  62. },
  63. ),
  64. Expanded(
  65. child: SingleChildScrollView(
  66. child: Column(
  67. children: List.generate(100, (index) {
  68. return Container(
  69. key: _keyList[index],
  70. width: double.infinity,
  71. padding: const EdgeInsets.all(10),
  72. decoration: BoxDecoration(
  73. border: Border.all(color: Colors.grey, width: 1),
  74. ),
  75. child: Text("index ${index + 100}"),
  76. );
  77. }),
  78. ),
  79. ),
  80. ),
  81. ],
  82. );
  83. }
  84.  
  85. void scrollRequest(int scrollNumber) {
  86. Scrollable.ensureVisible(_keyList[scrollNumber - 100].currentContext!,
  87. duration: const Duration(milliseconds: 300),
  88. curve: Curves.easeOut,
  89. alignment: 0.5,
  90. alignmentPolicy: ScrollPositionAlignmentPolicy.explicit);
  91. }
  92. }
  93.  
  94. class S2B extends StatelessWidget {
  95. const S2B({Key? key}) : super(key: key);
  96.  
  97. @override
  98. Widget build(BuildContext context) {
  99. return Row(
  100. mainAxisAlignment: MainAxisAlignment.center,
  101. children: [
  102. ElevatedButton(
  103. onPressed: () {
  104. _button1(context);
  105. },
  106. child: const Text("1:VMに値+1")),
  107. ElevatedButton(
  108. onPressed: () {
  109. _button2(context);
  110. },
  111. child: const Text("2:100に移動")),
  112. ElevatedButton(
  113. onPressed: () {
  114. _button3(context);
  115. },
  116. child: const Text("3:199に移動")),
  117. ],
  118. );
  119. }
  120.  
  121. void _button1(BuildContext context) {
  122. final ViewModelTest vm = context.read();
  123. vm.counter += 1;
  124. }
  125.  
  126. void _button2(BuildContext context) {
  127. final ViewModelTest vm = context.read();
  128. vm.scrollRequest(100);
  129. }
  130.  
  131. void _button3(BuildContext context) {
  132. final ViewModelTest vm = context.read();
  133. vm.scrollRequest(199);
  134. }
  135. }
  136.  
  137. class ViewModelTest extends ChangeNotifier {
  138. int get counter => _counter;
  139. int _counter = 0;
  140.  
  141. set counter(int value) {
  142. _counter = value;
  143. notifyListeners();
  144. }
  145.  
  146. void Function(int) scrollRequestCallback = (_) {};
  147.  
  148. scrollRequest(int scrollIndex) {
  149. this.scrollRequestCallback(scrollIndex);
  150. }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement