text stringlengths 1 372 |
|---|
setState(() { |
_visible = !_visible; |
}); |
}, |
tooltip: 'toggle opacity', |
child: const Icon(Icons.flip), |
) |
<code_end> |
<topic_end> |
<topic_start> |
4. fade the box in and out |
you have a green box on screen and a button to toggle the visibility |
to true or false. how to fade the box in and out? with an |
AnimatedOpacity widget. |
the AnimatedOpacity widget requires three arguments: |
<code_start> |
AnimatedOpacity( |
// if the widget is visible, animate to 0.0 (invisible). |
// if the widget is hidden, animate to 1.0 (fully visible). |
opacity: _visible ? 1.0 : 0.0, |
duration: const duration(milliseconds: 500), |
// the green box must be a child of the AnimatedOpacity widget. |
child: container( |
width: 200, |
height: 200, |
color: colors.green, |
), |
) |
<code_end> |
<topic_end> |
<topic_start> |
interactive example |
<code_start> |
import 'package:flutter/material.dart'; |
void main() => runApp(const MyApp()); |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
widget build(BuildContext context) { |
const appTitle = 'opacity demo'; |
return const MaterialApp( |
title: appTitle, |
home: MyHomePage(title: appTitle), |
); |
} |
} |
// 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 { |
const MyHomePage({ |
super.key, |
required this.title, |
}); |
final string 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) { |
return scaffold( |
appBar: AppBar( |
title: text(widget.title), |
), |
body: center( |
child: AnimatedOpacity( |
// if the widget is visible, animate to 0.0 (invisible). |
// if the widget is hidden, animate to 1.0 (fully visible). |
opacity: _visible ? 1.0 : 0.0, |
duration: const duration(milliseconds: 500), |
// the green box must be a child of the AnimatedOpacity widget. |
child: container( |
width: 200, |
height: 200, |
color: colors.green, |
), |
), |
), |
floatingActionButton: FloatingActionButton( |
onPressed: () { |
// call setState. this tells flutter to rebuild the |
// UI with the changes. |
setState(() { |
_visible = !_visible; |
}); |
}, |
tooltip: 'toggle opacity', |
child: const Icon(Icons.flip), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
hero animations |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.