我有以下代码:
class RadialMenu extends StatefulWidget {
@override
_RadialMenuState createState() => _RadialMenuState();
}
class _RadialMenuState extends State<RadialMenu>
with SingleTickerProviderStateMixin {
AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: Duration(milliseconds: 900),
vsync: this,
);
}
@override
Widget build(BuildContext context) {
return RadialAnimation(controller: controller);
}
}
class RadialAnimation extends StatelessWidget {
RadialAnimation({Key key, this.controller})
:
super(key: key);
final AnimationController controller;
final Animation<double> scale;
final Animation<double> translation;
final Animation<double> rotation;
build(context) {
return AnimatedBuilder(
animation: controller,
builder: (currentContext, builder) {
return Transform.rotate(
angle: radians(rotation.value),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
_buildButton(currentContext, 0, 'Train Arrivals',
color: Colors.red, icon: FontAwesomeIcons.personBooth),
Transform.scale(
scale: scale.value - 1.5,
child: FloatingActionButton(
child: Icon(FontAwesomeIcons.timesCircle),
onPressed: _close,
backgroundColor: Colors.red,
),
),
Transform.scale(
scale: scale.value,
child: FloatingActionButton(
child: Icon(FontAwesomeIcons.train),
onPressed: _open,
backgroundColor: Colors.black,
),
),
],
),
);
},
);
}
_open() {
controller.forward();
}
_close() {
controller.reverse();
}
_buildButton(
final BuildContext context,
double angle,
String text, {
Color color,
IconData icon,
}) {
final double rad = radians(angle);
return Transform(
transform: Matrix4.identity()
..translate(
(translation.value) * cos(rad),
(translation.value) * sin(rad),
),
child: FloatingActionButton(
child: Icon(icon),
backgroundColor: color,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (anotherContext) {
return SecondRoute();
}),
);
},
tooltip: text,
),
);
}
}
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: Center(
child: Text('Data'),
),
);
}
}
在 _buildButton 部分,我想包含一个 Navigator.push 实例,以便在单击该按钮时我需要转到另一条路线。
但目前这是不可能的,因为根据我的理解,Navigator.push 需要一个我没有在此处传递的构建上下文。
请帮我在这里传递正确的上下文,或者我还能做些什么来解决这个问题?
最佳答案
由于您是在 builder: (context, builder) 方法中创建按钮,因此此方法提供给您的上下文与您可以通过参数传递给 的上下文相同_buildButton(BuildContext context, ...) 函数并在 Navigator.push 方法中使用相同的上下文并转到另一条路线。
更新我之前告诉的所有答案都是正确的,但在你的情况下出现黑屏是因为你在屏幕上有很多 FAB,你需要为每个 FAB 中的 tagHero 属性提供一个唯一值你有。您的代码中的更改位于 onPress 属性之前的 FloatActionButton 的构造函数中。我将只把源代码的主要部分放在这里。
build(context) {
return AnimatedBuilder(
animation: controller,
builder: (currentContext, builder) {
return Transform.rotate(
angle: radians(rotation.value),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
_buildButton(currentContext, 0, 'Train Arrivals',
color: Colors.red, icon: FontAwesomeIcons.personBooth),
_buildButton(currentContext, 45, 'Train Fare Enquiry',
color: Colors.green, icon: FontAwesomeIcons.moneyBill),
_buildButton(currentContext, 90, 'Live Train Status',
color: Colors.cyan[400], icon: FontAwesomeIcons.hourglass),
_buildButton(currentContext, 135, 'Train Between Statios',
color: Colors.blue, icon: FontAwesomeIcons.landmark),
_buildButton(currentContext, 180, 'Train Route',
color: Colors.yellow[900], icon: FontAwesomeIcons.route),
_buildButton(currentContext, 225, 'Train Name/Number',
color: Colors.purple, icon: FontAwesomeIcons.train),
_buildButton(currentContext, 270, 'PNR Status',
color: Colors.teal, icon: FontAwesomeIcons.infoCircle),
_buildButton(currentContext, 315, 'Seat Availability',
color: Colors.pinkAccent, icon: FontAwesomeIcons.chair),
Transform.scale(
scale: scale.value - 1.5,
child: FloatingActionButton(
child: Icon(FontAwesomeIcons.timesCircle),
heroTag: "close", /// Unique tag for this FAB
onPressed: _close,
backgroundColor: Colors.red,
),
),
Transform.scale(
scale: scale.value,
child: FloatingActionButton(
child: Icon(FontAwesomeIcons.train),
heroTag: "open", /// Unique tag for this FAB
onPressed: _open,
backgroundColor: Colors.black,
),
),
],
),
);
},
);
}
_buildButton(
final BuildContext context,
double angle,
String text, {
Color color,
IconData icon,
}) {
final double rad = radians(angle);
return Transform(
transform: Matrix4.identity()
..translate(
(translation.value) * cos(rad),
(translation.value) * sin(rad),
),
child: FloatingActionButton(
child: Icon(icon),
backgroundColor: color,
heroTag: text, /// HERE I'am using the text as unique tag fot this FAB.
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (anotherContext) {
return SecondRoute();
}),
);
},
tooltip: text,
),
);
}
关于flutter - 传递正确的上下文以在 flutter 中构建按钮小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55170261/
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
在编写Ruby(客户端脚本)时,我看到了三种构建更长字符串的方法,包括行尾,所有这些对我来说“闻起来”有点难看。有没有更干净、更好的方法?变量递增。ifrender_quote?quote="NowthatthereistheTec-9,acrappyspraygunfromSouthMiami."quote+="ThisgunisadvertisedasthemostpopularguninAmericancrime.Doyoubelievethatshit?"quote+="Itactuallysaysthatinthelittlebookthatcomeswithit:themo
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
我在OSX上(如果重要的话)。如果我使用RVM安装Ruby,它会默认将Bundler安装到@globalgemset假设我想要一个不同版本的bundler。我假设我需要做的就是执行geminstallbundler--version但是,这会将bundler安装到默认gemset并且RVM不会为其设置路径。因此,如果我键入bundler,它仍会启动一个与Ruby一起安装到@global中的bundler两个问题:如何将bundler安装到@globalgemset。将bundler安装到@globalgemset中的模式是否正确,或者我遗漏了什么 最佳答案