dart - How can I expose a StatefulWidget's method? -
say want build statefulwidget named myslidewidget
provides public instance method: animate()
.
when press button on parent of myslidewidget
, can call myslidewidget
's animate()
method trigger internal slidetransition
of myslidewidget
.
the usage this:
class myslidewidgetdemo extends statelesswidget { @override widget build(buildcontext context) { myslidewidget myslidewidget = new myslidewidget(); return new scaffold( body: myslidewidget, floatingactionbutton: new floatingactionbutton( onpressed: () { myslidewidget.animate(); }, tooltip: 'start', child: new icon(icons.add), )); } }
what wondering how encapsulate implementations of animationcontroller
, _controller.forward()
inside myslidewidget
, user of myslidewidget
can call animate()
.
is possible? or idea way encapsulation in flutter?
you should use animationcontroller , pass slidetransition
. call controller.forward()
when needed.
here sample:
class _myhomepagestate extends state<myhomepage> tickerproviderstatemixin { animationcontroller _controller; animation<fractionaloffset> _slidetransitionposition; @override initstate() { super.initstate(); _controller = new animationcontroller( vsync: this, duration: const duration(milliseconds: 2000), ); _slidetransitionposition = new fractionaloffsettween( begin: const fractionaloffset(0.0, -1.0), end: const fractionaloffset(0.0, 10.0), ).animate(new curvedanimation( parent: _controller, curve: curves.fastoutslowin, )); } void _onpress() { _controller.forward(); } @override widget build(buildcontext context) { return new scaffold( appbar: new appbar( title: new text(widget.title), ), body: new container( child: new slidetransition( position: _slidetransitionposition, child: new container( color: colors.red, width: 100.0, height: 100.0, ), ), ), floatingactionbutton: new floatingactionbutton( onpressed: _onpress, tooltip: 'start', child: new icon(icons.add), ), ); } }
also can find examples in demos
Comments
Post a Comment