text stringlengths 1 372 |
|---|
_height = random.nextInt(300).toDouble(); |
// generate a random color. |
_color = Color.fromRGBO( |
random.nextInt(256), |
random.nextInt(256), |
random.nextInt(256), |
1, |
); |
// generate a random border radius. |
_borderRadius = |
BorderRadius.circular(random.nextInt(100).toDouble()); |
}); |
}, |
child: const Icon(Icons.play_arrow), |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
fade a widget in and out |
UI developers often need to show and hide elements on screen. |
however, quickly popping elements on and off the screen can |
feel jarring to end users. instead, |
fade elements in and out with an opacity animation to create |
a smooth experience. |
the AnimatedOpacity widget makes it easy to perform opacity |
animations. this recipe uses the following steps: |
<topic_end> |
<topic_start> |
1. create a box to fade in and out |
first, create something to fade in and out. for this example, |
draw a green box on screen. |
<code_start> |
container( |
width: 200, |
height: 200, |
color: colors.green, |
) |
<code_end> |
<topic_end> |
<topic_start> |
2. define a StatefulWidget |
now that you have a green box to animate, |
you need a way to know whether the box should be visible. |
to accomplish this, use a StatefulWidget. |
a StatefulWidget is a class that creates a state object. |
the state object holds some data about the app and provides a way to |
update that data. when updating the data, |
you can also ask flutter to rebuild the UI with those changes. |
in this case, you have one piece of data: |
a boolean representing whether the button is visible. |
to construct a StatefulWidget, create two classes: a |
StatefulWidget and a corresponding state class. |
pro tip: the flutter plugins for android studio and VSCode include |
the stful snippet to quickly generate this code. |
<code_start> |
// the StatefulWidget's job is to take data and create a state class. |
// in this case, the widget takes a title, and creates a _MyHomePageState. |
class MyHomePage extends StatefulWidget { |
final string title; |
const MyHomePage({ |
super.key, |
required this.title, |
}); |
@override |
State<MyHomePage> createState() => _MyHomePageState(); |
} |
// the state class is responsible for two things: holding some data you can |
// update and building the UI using that data. |
class _MyHomePageState extends State<MyHomePage> { |
// whether the green box should be visible. |
bool _visible = true; |
@override |
widget build(BuildContext context) { |
// the green box goes here with some other widgets. |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
3. display a button that toggles the visibility |
now that you have some data to determine whether the green box |
should be visible, you need a way to update that data. |
in this example, if the box is visible, hide it. |
if the box is hidden, show it. |
to handle this, display a button. when a user presses the button, |
flip the boolean from true to false, or false to true. |
make this change using setState(), |
which is a method on the state class. |
this tells flutter to rebuild the widget. |
for more information on working with user input, |
see the gestures section of the cookbook. |
<code_start> |
FloatingActionButton( |
onPressed: () { |
// call setState. this tells flutter to rebuild the |
// UI with the changes. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.