Flutter 颤振:如何在应用程序栏中使用动画图标-我想在颤振应用程序的应用程序栏中使用此动画图标,而不是无动画图标
我想在此AppBar中使用动画图标,但无法使用,因为动画图标有一个带“with TickerProviderStateMixin”的有状态小部件。如果我将整个脚手架移动到一个有状态的小部件,那么“onMenuTap”就不起作用。问题的主要目的是在颤振AppBar中使用动画图标Flutter 颤振:如何在应用程序栏中使用动画图标-我想在颤振应用程序的应用程序栏中使用此动画图标,而不是无动画图标,flutter,flutter-layout,flutter-animation,flutter-appbar,Flutter,Flutter Layout,Flutter Animation,Flutter Appbar,我想在此AppBar中使用动画图标,但无法使用,因为动画图标有一个带“with TickerProviderStateMixin”的有状态小部件。如果我将整个脚手架移动到一个有状态的小部件,那么“onMenuTap”就不起作用。问题的主要目的是在颤振AppBar中使用动画图标 import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../FreelanceTheme/AppS
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../FreelanceTheme/AppStyleModeNotifier.dart';
class HomePage extends StatelessWidget with NavigationStates {
final Function onMenuTap;
const HomePage({Key key, this.onMenuTap}) : super(key: key);
@override
Widget build(BuildContext context) {
final appStyleMode = Provider.of<AppStyleModeNotifier>(context);
return MaterialApp(
home: Scaffold(
backgroundColor: Color(0xffE5E5E5),
appBar: AppBar(
elevation: 0,
backgroundColor: appStyleMode.appBarBackgroundColor,
actions: <Widget>[
Switch(
activeColor: Colors.orange,
value: appStyleMode.mode,
onChanged: (value) => appStyleMode.switchMode(),
),
],
leading: IconButton(
tooltip: 'App Settings',
icon: Icon(
FontAwesomeIcons.bars,
color: Colors.white,
),
onPressed: onMenuTap,
),
centerTitle: true,
title: Text(
"Home",
style: TextStyle(
color: Colors.white,
),
),
),
body: FreelancingHomePage(),
),
);
}
}
以下是动画图标的代码。我想在上面的应用程序栏中使用这个动画图标
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
AnimationController _animationIconController1,
bool isarrowmenu = false;
@override
void initState() {
super.initState();
_animationIconController1 = AnimationController(
vsync: this,
duration: Duration(milliseconds: 750),
reverseDuration: Duration(milliseconds: 750),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
GestureDetector(
onTap: () {
setState(() {
isarrowmenu
? _animationIconController1.reverse()
: _animationIconController1.forward();
isarrowmenu = !isarrowmenu;
});
},
child: ClipOval(
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: 2.5,
color: Colors.green,
),
borderRadius: BorderRadius.all(
Radius.circular(50.0),
),
),
width: 75,
height: 75,
child: Center(
child: AnimatedIcon(
icon: AnimatedIcons.arrow_menu,
progress: _animationIconController1,
color: Colors.red,
size: 60,
),
),
),
),
),
),
);
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState使用TickerProviderStateMixin扩展状态{
AnimationController\u animationIconController1,
bool isarrowmenu=false;
@凌驾
void initState(){
super.initState();
_animationIconController1=AnimationController(
vsync:这个,,
持续时间:持续时间(毫秒:750),
反转:持续时间(毫秒:750),
);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
手势检测器(
onTap:(){
设置状态(){
isarrowmenu
?_animationIconController1.reverse()
:_animationIconController1.forward();
isarrowmenu=!isarrowmenu;
});
},
孩子:斜坡(
子:容器(
装饰:盒子装饰(
边界:边界(
宽度:2.5,
颜色:颜色。绿色,
),
borderRadius:borderRadius.all(
圆形半径(50.0),
),
),
宽度:75,
身高:75,
儿童:中心(
孩子:动画片(
图标:animateDictions.arrow_菜单,
进度:_animationIconController1,
颜色:颜色,红色,
尺码:60,
),
),
),
),
),
),
);
}
您可以复制粘贴运行下面的完整代码步骤1:您可以使用
StatefulWidget
制作此动画图标,该控件在菜单上具有VoidCallback
class CustomIcon extends StatefulWidget {
VoidCallback onMenuTap;
CustomIcon({Key key, this.onMenuTap}) : super(key: key);
@override
_CustomIconState createState() => _CustomIconState();
}
class _CustomIconState extends State<CustomIcon> with TickerProviderStateMixin {
AnimationController _animationIconController1;
工作演示
I/flutter (25195): hi
I/flutter (25195): hi
工作演示的输出
I/flutter (25195): hi
I/flutter (25195): hi
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(
onMenuTap: () {
print("hi");
},
),
);
}
}
class HomePage extends StatelessWidget {
final Function onMenuTap;
const HomePage({Key key, this.onMenuTap}) : super(key: key);
@override
Widget build(BuildContext context) {
//final appStyleMode = Provider.of<AppStyleModeNotifier>(context);
return MaterialApp(
home: Scaffold(
backgroundColor: Color(0xffE5E5E5),
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.blue,
actions: <Widget>[
/* Switch(
activeColor: Colors.orange,
value: appStyleMode.mode,
onChanged: (value) => appStyleMode.switchMode(),
),*/
],
leading: CustomIcon(
onMenuTap: () {
onMenuTap();
},
),
centerTitle: true,
title: Text(
"Home",
style: TextStyle(
color: Colors.white,
),
),
),
body: Text("FreelancingHomePage()"),
),
);
}
}
class CustomIcon extends StatefulWidget {
VoidCallback onMenuTap;
CustomIcon({Key key, this.onMenuTap}) : super(key: key);
@override
_CustomIconState createState() => _CustomIconState();
}
class _CustomIconState extends State<CustomIcon> with TickerProviderStateMixin {
AnimationController _animationIconController1;
bool isarrowmenu = false;
@override
void initState() {
super.initState();
_animationIconController1 = AnimationController(
vsync: this,
duration: Duration(milliseconds: 750),
reverseDuration: Duration(milliseconds: 750),
);
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(() {
isarrowmenu
? _animationIconController1.reverse()
: _animationIconController1.forward();
isarrowmenu = !isarrowmenu;
if (widget.onMenuTap != null) {
widget.onMenuTap();
}
});
},
child: ClipOval(
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: 2.5,
color: Colors.green,
),
borderRadius: BorderRadius.all(
Radius.circular(50.0),
),
),
width: 75,
height: 75,
child: Center(
child: AnimatedIcon(
icon: AnimatedIcons.arrow_menu,
progress: _animationIconController1,
color: Colors.red,
size: 60,
),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:主页(
onMenuTap:(){
打印(“hi”);
},
),
);
}
}
类主页扩展了无状态小部件{
最终函数onMenuTap;
const主页({Key,this.onMenuTap}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
//final-appStyleMode=Provider.of(上下文);
返回材料PP(
家:脚手架(
背景颜色:颜色(0xFFE5E5),
appBar:appBar(
海拔:0,
背景颜色:Colors.blue,
行动:[
/*开关(
activeColor:Colors.orange,
值:appStyleMode.mode,
onChanged:(值)=>appStyleMode.switchMode(),
),*/
],
领先:CustomIcon(
onMenuTap:(){
onMenuTap();
},
),
标题:对,
标题:正文(
“家”,
样式:TextStyle(
颜色:颜色,白色,
),
),
),
正文:文本(“FreelancingHomePage()”,
),
);
}
}
类CustomIcon扩展StatefulWidget{
无尿;
CustomIcon({Key-Key,this.onMenuTap}):super(Key:Key);
@凌驾
_CustomIconState createState()=>\u CustomIconState();
}
类_CustomIconState使用TickerProviderStateMixin扩展状态{
AnimationController_animationIconController1;
bool isarrowmenu=false;
@凌驾
void initState(){
super.initState();
_animationIconController1=AnimationController(
vsync:这个,,
持续时间:持续时间(毫秒:750),
反转:持续时间(毫秒:750),
);
}
@凌驾
小部件构建(构建上下文){
返回手势检测器(
onTap:(){
设置状态(){
isarrowmenu
?_animationIconController1.reverse()
:_animationIconController1.forward();
isarrowmenu=!isarrowmenu;
if(widget.onMenuTap!=null){
onMenuTap();
}
});
},
孩子:斜坡(
子:容器(
装饰:盒子装饰(
边界:边界(
宽度:2.5,
颜色:颜色。绿色,
),
borderRadius:borderRadius.all(
圆形半径(50.0),
),
),
宽度:75,
身高:75,
儿童:中心(
孩子:动画片(
图标:animateDictions.arrow_菜单,
进度:_animationIconController1,
颜色:颜色,红色,
尺码:60,
),
),
),
),
);
}
}
嗨,亲爱的chunhunghan,这对我很有用。你让我真心为你祈祷。我永远不会忘记你在这个应用程序中所做的工作。没有什么可以对你说的。非常感谢你……你是一个伟大的人。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(
onMenuTap: () {
print("hi");
},
),
);
}
}
class HomePage extends StatelessWidget {
final Function onMenuTap;
const HomePage({Key key, this.onMenuTap}) : super(key: key);
@override
Widget build(BuildContext context) {
//final appStyleMode = Provider.of<AppStyleModeNotifier>(context);
return MaterialApp(
home: Scaffold(
backgroundColor: Color(0xffE5E5E5),
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.blue,
actions: <Widget>[
/* Switch(
activeColor: Colors.orange,
value: appStyleMode.mode,
onChanged: (value) => appStyleMode.switchMode(),
),*/
],
leading: CustomIcon(
onMenuTap: () {
onMenuTap();
},
),
centerTitle: true,
title: Text(
"Home",
style: TextStyle(
color: Colors.white,
),
),
),
body: Text("FreelancingHomePage()"),
),
);
}
}
class CustomIcon extends StatefulWidget {
VoidCallback onMenuTap;
CustomIcon({Key key, this.onMenuTap}) : super(key: key);
@override
_CustomIconState createState() => _CustomIconState();
}
class _CustomIconState extends State<CustomIcon> with TickerProviderStateMixin {
AnimationController _animationIconController1;
bool isarrowmenu = false;
@override
void initState() {
super.initState();
_animationIconController1 = AnimationController(
vsync: this,
duration: Duration(milliseconds: 750),
reverseDuration: Duration(milliseconds: 750),
);
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(() {
isarrowmenu
? _animationIconController1.reverse()
: _animationIconController1.forward();
isarrowmenu = !isarrowmenu;
if (widget.onMenuTap != null) {
widget.onMenuTap();
}
});
},
child: ClipOval(
child: Container(
decoration: BoxDecoration(
border: Border.all(
width: 2.5,
color: Colors.green,
),
borderRadius: BorderRadius.all(
Radius.circular(50.0),
),
),
width: 75,
height: 75,
child: Center(
child: AnimatedIcon(
icon: AnimatedIcons.arrow_menu,
progress: _animationIconController1,
color: Colors.red,
size: 60,
),
),
),
),
);
}
}