dart
class MyComponent extends StatelessWidget {
final String displayString;
MyComponent({required this.displayString});
Widget build(BuildContext context) {
return Text(displayString);
}
}
class MyComponent extends StatelessWidget {
final String displayString;
MyComponent({required this.displayString});
Widget build(BuildContext context) {
return Text(displayString);
}
}
class ConditionalComponent extends StatelessWidget {
final bool condition;
ConditionalComponent({required this.condition});
Widget build(BuildContext context) {
if(condition) {
return Text("Condition is true");
} else {
return Text("Condition is false");
}
}
}
// Usage
ConditionalComponent(condition: true)
class Parent extends StatelessWidget {
final String data;
Parent({required this.data});
Widget build(BuildContext context) {
return IntermediateComponent(data: data);
}
}
class IntermediateComponent extends StatelessWidget {
final String data;
IntermediateComponent({required this.data});
Widget build(BuildContext context) {
return ChildComponent(data: data);
}
}
class ChildComponent extends StatelessWidget {
final String data;
ChildComponent({required this.data});
Widget build(BuildContext context) {
return Text("Received data: $data");
}
}
// Usage
Parent(data: "Some data")
class ClickableComponent extends StatefulWidget {
_ClickableComponentState createState() => _ClickableComponentState();
}
class _ClickableComponentState extends State<ClickableComponent> {
bool clicked = false;
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () => setState(() => clicked = true),
child: Text(clicked ? "Button clicked" : "Click me"),
);
}
}
class TextInputComponent extends StatefulWidget {
const TextInputComponent({super.key});
State<TextInputComponent> createState() => _TextInputComponentState();
}
class _TextInputComponentState extends State<TextInputComponent> {
late final _controller = TextEditingController(text: "");
void dispose() {
_controller.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return TextField(
controller: _controller,
decoration: const InputDecoration(labelText: "Enter text"),
);
}
}
Flutter doesn't have a built-in preview feature. You can, however, create a separate app or run your app in an emulator or on a device to view your components. Additionally, you can use the Flutter Studio web-based tool to create and preview Flutter widgets in a browser.
class ListComponent extends StatelessWidget {
final List<String> items;
ListComponent({required this.items});
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
);
}
}
// Usage
final items = ["Item 1", "Item 2", "Item 3"];
ListComponent(items: items)
class Person {
final String name;
final int age;
final String id;
Person({required this.name, required this.age, required this.id});
}
class ItemKeysExample extends StatelessWidget {
final List<Person> items;
ItemKeysExample({required this.items});
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final person = items[index];
return ListTile(
key: Key(person.id),
title: Text('Name: ${person.name}, Age: ${person.age}'),
);
},
);
}
}
// Usage
ItemKeysExample(items: [Person(name: 'John', age: 30, id: '1'), Person(name: 'Jane', age: 28, id: '2'), Person(name: 'Bob', age: 25, id: '3')])
class Parent extends StatelessWidget {
final Widget header;
final Widget content;
Parent({required this.header, required this.content});
Widget build(BuildContext context) {
return Column(
children: [
header,
content,
],
);
}
}
// Usage
Parent(
header: Text("Header"),
content: Child(),
)
class Child extends StatelessWidget {
Widget build(BuildContext context) {
return Text("Child Content");
}
}
In Flutter, you can wrap widgets with other widgets to achieve similar effects.
class ModifiersExample extends StatelessWidget {
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16.0),
color: Colors.blue,
child: Text('Hello, World!', style: TextStyle(color: Colors.white)),
);
}
}
class Counter extends StatefulWidget {
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int count = 0;
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () => setState(() => count += 1),
child: Text("Count: $count"),
);
}
}
class CustomInheritedWidget extends InheritedWidget {
final String data;
CustomInheritedWidget({required this.data, required Widget child})
: super(child: child);
bool updateShouldNotify(CustomInheritedWidget oldWidget) {
return oldWidget.data != data;
}
static CustomInheritedWidget of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<CustomInheritedWidget>()!;
}
}
class Parent extends StatelessWidget {
final String data;
Parent({required this.data});
Widget build(BuildContext context) {
return CustomInheritedWidget(
data: data,
child: Intermediate(),
);
}
}
class Intermediate extends StatelessWidget {
Widget build(BuildContext context) {
return Child();
}
}
class Child extends StatelessWidget {
Widget build(BuildContext context) {
final data = CustomInheritedWidget.of(context).data;
return Text("Received data: $data");
}
}
// Usage
Parent(data: "Some data")
class SideEffectOnLoadComponent extends StatefulWidget {
_SideEffectOnLoadComponentState createState() => _SideEffectOnLoadComponentState();
}
class _SideEffectOnLoadComponentState extends State<SideEffectOnLoadComponent> {
void initState() {
super.initState();
// Perform side effect, e.g. fetch data, update external data source
}
Widget build(BuildContext context) {
// Other UI components
return Container();
}
}