Skip to content

Instantly share code, notes, and snippets.

@iqbalmineraltown
Last active June 16, 2023 08:13
Show Gist options
  • Save iqbalmineraltown/bb1477dd52416a3f923bc4d7e78a502d to your computer and use it in GitHub Desktop.
Save iqbalmineraltown/bb1477dd52416a3f923bc4d7e78a502d to your computer and use it in GitHub Desktop.
Flutter Class vs Function Widget
// Open console to view rebuild triggered
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool showCircle = false;
Widget _buildTile(
String title,
) {
print('$title function rebuilt!');
return InkWell(
onTap: () {},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: Row(
children: [
Expanded(
child: Text(title),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_buildTile('AAAAA'),
_buildTile('BBBBB'),
const _BuildTile('AAAAA'),
const _BuildTile('BBBBB'),
TextButton(
child: const Text('Trigger setstate'),
onPressed: () {
setState(() {});
},
),
],
),
),
);
}
}
class _BuildTile extends StatelessWidget {
final String title;
const _BuildTile(this.title);
@override
Widget build(BuildContext context) {
print('$title widget rebuilt!');
return InkWell(
onTap: () {},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: Row(
children: [
Expanded(
child: Text(title),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment