New post on /r/flutterdev subreddit:
call method from another class using animationcontroller
i new on flutter i want to call a method to a antother class i find this error 'LateInitializationError: Field 'animationController' has not been initialized.'could somebody help me please?import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
u/override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State
with SingleTickerProviderStateMixin {
late AnimationController animationController;
static const double maxSlide = 225.0;
static const double minDragStartEdge = 60;
static const double maxDragstartEdge = maxSlide - 16;
bool _canBeDragged = false;
u/override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this, duration: const Duration(milliseconds: 250));
}
void toggle() => animationController.isDismissed ? open() : close();
void close() => animationController.reverse();
void open() => animationController.forward();
// Declaration des page
var drawer = MyDrawer();
var customChild = MyCustomChild();
u/override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: GestureDetector(
//gestation de comportments des page
onHorizontalDragStart: _onDragStart,
onHorizontalDragUpdate: _onDragUpdate,
onHorizontalDragEnd: _onDragEnd,
onTap: toggle,
child: AnimatedBuilder(
animation: animationController,
builder: (context, _) {
double numValue = animationController.value;
final slide = numValue * maxSlide;
final scale = 1.0 - (numValue * 0.3);
return Stack(children: [
drawer,
Transform(
transform: Matrix4.identity()
..translate(slide)
..scale(scale),
alignment: Alignment.centerLeft,
child: customChild),
]);
}),
),
),
);
}
void _onDragStart(DragStartDetails details) {
bool isDragOpenFromLeft = animationController.isDismissed &&
details.globalPosition.dx < minDragStartEdge;
bool isDragCloseFromRight = animationController.isCompleted &&
details.globalPosition.dx > maxDragstartEdge;
_canBeDragged = isDragOpenFromLeft || isDragCloseFromRight;
}
void _onDragUpdate(DragUpdateDetails details) {
if (_canBeDragged) {
double delta = details.primaryDelta! / maxSlide;
animationController.value += delta;
}
}
void _onDragEnd(DragEndDetails details) {
if (animationController.isDismissed || animationController.isCompleted) {
return;
}
if (details.velocity.pixelsPerSecond.dx.abs() >= 365.0) {
double visualVelocity = details.velocity.pixelsPerSecond.dx /
MediaQuery.of(context).size.width;
animationController.fling(velocity: visualVelocity);
} else if (animationController.value < 0.5) {
close();
} else {
open();
}
}
}
class MyDrawer extends StatefulWidget {
MyDrawer({Key? key}) : super(key: key);
u/override
_MyDrawerState createState() => _MyDrawerState();
}
class _MyDrawerState extends State {
u/override
Widget build(BuildContext context) => Scaffold(
backgroundColor: //const Color.fromRGBO(21, 30, 61, 1),
Theme.of(context).primaryColor,
body: Container());
}
class MyCustomChild extends StatefulWidget {
// ignore: non_constant_identifier_names
const MyCustomChild({Key? key}) : super(key: key);
u/override
_MyCustomChildState createState() => _MyCustomChildState();
}
class _MyCustomChildState extends State {
_HomePageState toggleDrawer = _HomePageState();
u/override
Widget build(BuildContext context) => Scaffold(
backgroundColor: Colors.yellow[800],
appBar: AppBar(
leading: IconButton(
onPressed:…