Skip to content

Instantly share code, notes, and snippets.

@amelnikov78
Last active March 31, 2023 07:54
Show Gist options
  • Save amelnikov78/d947c19f5b913845f40142be4a84bd1f to your computer and use it in GitHub Desktop.
Save amelnikov78/d947c19f5b913845f40142be4a84bd1f to your computer and use it in GitHub Desktop.
Nested Widgets Test
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Nested Widgets Test',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const WidgetA(),
);
}
}
/*
* WidgetA
* */
class WidgetA extends StatefulWidget {
const WidgetA({super.key});
@override
State<WidgetA> createState() => _WidgetAState();
}
class _WidgetAState extends State<WidgetA> {
int _stateCounterA = 0;
void _incrementCounter() {
setState(() {
_stateCounterA++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
const SizedBox(height: 16,),
Text('_stateCounterA: $_stateCounterA'),
const SizedBox(height: 16,),
WidgetB(counterB: _stateCounterA,),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
/*
* WidgetB
* */
class WidgetB extends StatefulWidget {
final int counterB;
const WidgetB({Key? key, required this.counterB}) : super(key: key);
@override
State<WidgetB> createState() => _WidgetBState();
}
class _WidgetBState extends State<WidgetB> {
late int _stateCounterB;
@override
void initState() {
_stateCounterB = widget.counterB;
super.initState();
}
@override
Widget build(BuildContext context) {
return Text('_stateCounterB: $_stateCounterB');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment