Skip to content

Commit

Permalink
RSM Prototype #3 PageView Test
Browse files Browse the repository at this point in the history
- Test PageView update state.
  • Loading branch information
rayliverified committed Nov 30, 2023
1 parent ba82a8f commit 68066c1
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions lib/main_pageview_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
int parentCounter = 10;

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
scrollBehavior: MaterialScrollBehavior().copyWith(
dragDevices: {
PointerDeviceKind.mouse,
PointerDeviceKind.touch,
PointerDeviceKind.stylus,
PointerDeviceKind.unknown
},
),
home: Scaffold(
body: PageView(
physics: AlwaysScrollableScrollPhysics(),
controller: PageController(viewportFraction: 0.8),
children: [
CounterPage(),
CounterPage(),
CounterPage(), // Add as many pages as you want
],
),
),
);
}
}

class CounterPage extends StatefulWidget {
@override
_CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Parent Counter: ${context.findAncestorStateOfType<MyAppState>()?.parentCounter}'),
ElevatedButton(
onPressed: () {
context
.findAncestorStateOfType<MyAppState>()
?.parentCounter += 1;
context
.findAncestorStateOfType<MyAppState>()
?.setState(() {});
},
child: Text('Increment Parent Counter')),
Text('You have pushed the button this many times:'),
Text('$_counter', style: Theme.of(context).textTheme.headline4),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

0 comments on commit 68066c1

Please sign in to comment.