Last active
June 16, 2023 08:13
-
-
Save iqbalmineraltown/bb1477dd52416a3f923bc4d7e78a502d to your computer and use it in GitHub Desktop.
Flutter Class vs Function Widget
This file contains 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
// 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