Flutter / UI Elements / Widgets with class vs widgets with function
Widget with class
-
Code
We can create widgets in two primary ways: with a class or without a class.
1. Widget Without a Class (Function or Stateless):
import 'package:flutter/material.dart'; Widget myFunctionWidget() { return Container( child: Text('Hello, Function Widget!'), ); } 2. Widget with a Class
StatelessWidget
StatefulWidgetimport 'package:flutter/material.dart'; class MyStatelessWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Container( child: Text('Hello, Stateless Widget!'), ); } } import 'package:flutter/material.dart'; class MyStatefulWidget extends StatefulWidget { @override _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); } class _MyStatefulWidgetState extends State { @override Widget build(BuildContext context) { return Container( child: Text('Hello, Stateful Widget!'), ); } }