text
stringlengths 1
474
|
---|
),
|
body: Center(child: Text(textToShow)),
|
floatingActionButton: FloatingActionButton(
|
onPressed: _updateText,
|
tooltip: 'Update Text',
|
child: const Icon(Icons.update),
|
),
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
How do I lay out my widgets? Where is my XML layout file?
|
In Android, you write layouts in XML, but in Flutter you write your layouts
|
with a widget tree.The following example shows how to display a simple widget with padding:
|
<code_start>@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
appBar: AppBar(
|
title: const Text('Sample App'),
|
),
|
body: Center(
|
child: ElevatedButton(
|
style: ElevatedButton.styleFrom(
|
padding: const EdgeInsets.only(left: 20, right: 30),
|
),
|
onPressed: () {},
|
child: const Text('Hello'),
|
),
|
),
|
);
|
}<code_end>
|
You can view some of the layouts that Flutter has to offer in the
|
widget catalog.<topic_end>
|
<topic_start>
|
How do I add or remove a component from my layout?
|
In Android, you call addChild() or removeChild()
|
on a parent to dynamically add or remove child views.
|
In Flutter, because widgets are immutable there is
|
no direct equivalent to addChild(). Instead,
|
you can pass a function to the parent that returns a widget,
|
and control that child’s creation with a boolean flag.For example, here is how you can toggle between two
|
widgets when you click on a FloatingActionButton:
|
<code_start>import 'package:flutter/material.dart';
|
void main() {
|
runApp(const SampleApp());
|
}
|
class SampleApp extends StatelessWidget {
|
const SampleApp({super.key});
|
// This widget is the root of your application.
|
@override
|
Widget build(BuildContext context) {
|
return MaterialApp(
|
title: 'Sample App',
|
theme: ThemeData(
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
),
|
home: const SampleAppPage(),
|
);
|
}
|
}
|
class SampleAppPage extends StatefulWidget {
|
const SampleAppPage({super.key});
|
@override
|
State<SampleAppPage> createState() => _SampleAppPageState();
|
}
|
class _SampleAppPageState extends State<SampleAppPage> {
|
// Default value for toggle.
|
bool toggle = true;
|
void _toggle() {
|
setState(() {
|
toggle = !toggle;
|
});
|
}
|
Widget _getToggleChild() {
|
if (toggle) {
|
return const Text('Toggle One');
|
} else {
|
return ElevatedButton(
|
onPressed: () {},
|
child: const Text('Toggle Two'),
|
);
|
}
|
}
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
appBar: AppBar(
|
title: const Text('Sample App'),
|
),
|
body: Center(
|
child: _getToggleChild(),
|
),
|
floatingActionButton: FloatingActionButton(
|
onPressed: _toggle,
|
tooltip: 'Update Text',
|
child: const Icon(Icons.update),
|
),
|
);
|
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.