I am facing problems with this tematic, I would like to change a widget after certain time. e.g
I have this animatedFlutter Screen (screen 1) and then after the animation end I would like to change the screen to any screen that I have, for example. Login();
Any tips? Thanks.
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(AnimatedFlutterLogo());
class AnimatedFlutterLogo extends StatefulWidget {
#override
State<StatefulWidget> createState() => new _AnimatedFlutterLogoState();
}
class _AnimatedFlutterLogoState extends State<AnimatedFlutterLogo> {
Timer _timer;
FlutterLogoStyle _logoStyle = FlutterLogoStyle.markOnly;
_AnimatedFlutterLogoState() {
_timer = new Timer(const Duration(milliseconds: 800), () {
setState(() {
_logoStyle = FlutterLogoStyle.horizontal;
});
});
}
#override
void dispose() {
super.dispose();
_timer.cancel();
}
#override
Widget build(BuildContext context) {
return new FlutterLogo(
size: 200.0,
textColor: Colors.white,
style: _logoStyle,
);
}
}
try this,
_Act_NotificationScreenState() {
_timer = new Timer(const Duration(milliseconds: 800), () {
setState(() {
_logoStyle = FlutterLogoStyle.horizontal;
});
_timer = new Timer(const Duration(seconds: 1), () {
Navigator.push(context, MaterialPageRoute(builder: (context) => Act_Login()));
});
});
}
Wrap your AnimatedFlutterLogo with MaterialApp and in the callback function of Timer use Navigator to navigate to respective Page
Example:
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(MaterialApp(home:AnimatedFlutterLogo()));
class AnimatedFlutterLogo extends StatefulWidget {
#override
State<StatefulWidget> createState() => new _AnimatedFlutterLogoState();
}
class _AnimatedFlutterLogoState extends State<AnimatedFlutterLogo> {
Timer _timer;
FlutterLogoStyle _logoStyle = FlutterLogoStyle.markOnly;
_AnimatedFlutterLogoState() {
_timer = new Timer(const Duration(milliseconds: 800), () {
setState(() {
_logoStyle = FlutterLogoStyle.horizontal;
});
Navigator.push( //<-- Navigate to loginPage on Timeout
context,
MaterialPageRoute(builder: (context) => LoginPage()),
);
});
}
#override
void dispose() {
super.dispose();
_timer.cancel();
}
#override
Widget build(BuildContext context) {
return new FlutterLogo(
size: 200.0,
textColor: Colors.white,
style: _logoStyle,
);
}
}
class LoginPage extends StatelessWidget{
#override
Widget build(BuildContext context){
return Container(alignment: Alignment.center,child: Text("LOG IN PAGE"));
}
}
You need to write code of navigating to another screen (Widget in case of Flutter)
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Login()),
);
You need to import Login in your current screen.
Related
I am building a mobile application using flutter. The app has a homepage with an AnimatedIndexedStack. To switch to another screen I simply change the index. If I run the app, I can switch to any screen without any problem. However, if I switch to a screen with a textfield and tap on the textfield to edit. Now after cancelling the keyboard, if I switch to any other screen, the keyboard pops up automatically when there are no textfields on the screens and I am not tapping on any textfield. Regardless of the start screen and the end screen, the keyboard pops up automatically after changing the index.
The animated indexed stack looks like this:
import 'package:flutter/material.dart';
class AnimatedIndexedStack extends StatefulWidget {
final int index;
final List<Widget> children;
const AnimatedIndexedStack({
Key? key,
required this.index,
required this.children,
}) : super(key: key);
#override
_AnimatedIndexedStackState createState() => _AnimatedIndexedStackState();
}
class _AnimatedIndexedStackState extends State<AnimatedIndexedStack>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
int _index = 0;
#override
void initState() {
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 250),
);
_animation = Tween(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.ease,
),
);
_index = widget.index;
_controller.forward();
super.initState();
}
#override
void didUpdateWidget(AnimatedIndexedStack oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.index != _index) {
_controller.reverse().then((_) {
setState(() => _index = widget.index);
_controller.forward();
});
}
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return Opacity(
opacity: _controller.value,
child: child,
);
},
child: IndexedStack(
index: _index,
children: widget.children,
),
);
}
}
homepage looks like this:
class Homepage extends StatefulWidget {
const Homepage({Key? key}) : super(key: key);
#override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
var _currentIndex = 0;
final _homeIndex = 0;
final _titles = ['Home', 'New Booking', 'Trips', 'Payment History', 'Support', 'Profile'];
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
var _bookingStep = 0;
#override
void initState() {
_currentIndex = _homeIndex;
super.initState();
}
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => _onWillPop(),
child: Scaffold(
key: scaffoldKey,
extendBody: true,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
drawer: AppDrawer(selectedIndex: _currentIndex, onItemTap: (index) {
setState(() {
_currentIndex = index;
});
scaffoldKey.currentState!.closeDrawer();
},),
body: SafeArea(
bottom: false,
child: AnimatedIndexedStack(
index: _currentIndex,
children: [
const BookingsScreen(),
NewBookingScreen(onExit: () {
setState(() {
_currentIndex = 0;
});
}, bookingStep: _bookingStep, nextStep: () {
setState(() {
_bookingStep = _bookingStep + 1;
});
},),
const Trips(),
const PaymentHistoryScreen(),
SupportScreen(),
const ProfileScreen()
],
)
),
),
);
}
}
Try adding this to all screens where you have the issue,
FocusManager.instance.primaryFocus?.unfocus()
I'm having this issue while creating a splash screen in flutter. I looked for an answer but no one solve the matter or answer perfectly.
Cannot resolve symbol '#android:color/black'
Create StateFulWidget
Add one Future.delayed() to initstate.
#override
void initState() {
super.initState();
Future.delayed(const Duration(seconds: 3), () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondScreen(),
),
);
});
}
Duration(seconds:3) will wait for 3 seconds on the splash screen then it will redirect to SecondScreen.
the code should be like this
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
#override
State<SplashScreen> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
#override
void initState() {
super.initState();
Future.delayed(const Duration(seconds: 3), () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondScreen(),
),
);
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.blue,
child: const Center(child: Text("Splash Screen")),
),
);
}
}
the whole code
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SplashScreen(),
);
}
}
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
#override
State<SplashScreen> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
#override
void initState() {
super.initState();
Future.delayed(const Duration(seconds: 3), () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondScreen(),
),
);
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.blue,
child: const Center(child: Text("Splash Screen")),
),
);
}
}
class SecondScreen extends StatefulWidget {
const SecondScreen({super.key});
#override
State<SecondScreen> createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.green,
child: const Center(
child: Text("Home Screen"),
),
),
);
}
}
If it was useful, you can choose it as an approved answer and give points. Good coding.
Try using this,
<item android:background="#android:color/white" />
When it comes to drawable you should assign XMLs or images in your drawable or mipmap resource
i tried to get run the ShakeDetector in my Flutter app. I got a sample app here. Can somebody help me to fix the problem?
This codesample below is from the shake.dart github example. I dont get it why it is not working.
import 'package:flutter/material.dart';
import 'package:shake/shake.dart';
void main() {
runApp(DemoPage());
}
class DemoPage extends StatefulWidget {
#override
_DemoPageState createState() => _DemoPageState();
}
var phoneShakes;
class _DemoPageState extends State<DemoPage> {
#override
void initState() {
super.initState();
ShakeDetector detector = ShakeDetector.autoStart(onPhoneShake: () {
print('shaking');
});
// To close: detector.stopListening();
// ShakeDetector.waitForStart() waits for user to call detector.startListening();
phoneShakes = detector.mShakeCount;
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: AppBar(
title: Text(phoneShakes.toString()),
),
);
}
}
It's because there was no call to setState. Try this code:
class _DemoPageState extends State<DemoPage> {
late ShakeDetector _detector;
#override
void initState() {
super.initState();
_detector = ShakeDetector.autoStart(onPhoneShake: () {
setState(() {}); // Call setState every time phone shakes.
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Count: ${_detector.mShakeCount}'),
),
);
}
}
Apparently the error is actually in the main.dart -- crashing when opening the app only in first time
I am developing a flutter application with a welcome screen that appears only the first time the app is opened, after that the main screen is a webview
This is my main.dart
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
color: Colors.amber,
home: new Splash(),
);
}
}
class Splash extends StatefulWidget {
#override
SplashState createState() => new SplashState();
}
class SplashState extends State<Splash> with AfterLayoutMixin<Splash> {
Future checkFirstSeen() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool _seen = (prefs.getBool('seen') ?? false);
if (_seen) {
Navigator.of(context).pushReplacement(
new MaterialPageRoute(builder: (context) => new Home()));
} else {
await prefs.setBool('seen', true);
Navigator.of(context).pushReplacement(
new MaterialPageRoute(builder: (context) => new IntroScreen()));
}
}
#override
void afterFirstLayout(BuildContext context) => checkFirstSeen();
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text('Carregando...'),
),
);
}
}
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Entrega na Obra',
home: HomePage(),
);
}
}
class IntroScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Entrega na Obra',
home: WelcomeScreen(),
);
}
}
Flutter camera app renders very slowly when using websockets in image streams. How can I improve my app performance? what is the best way to overcome this? On emulator it works just fine but on real device there is lot of delay and stuttering. Can we use async for camera app? If yes, how can I use it with this code?
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:web_socket_channel/io.dart';
import 'dart:convert';
import 'package:archive/archive.dart';
List<CameraDescription> cameras;
Future<void> main() async {
cameras = await availableCameras();
runApp(App());
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Title',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: CameraApp(),
);
}
}
class CameraApp extends StatefulWidget {
#override
_CameraAppState createState() => _CameraAppState();
}
class _CameraAppState extends State<CameraApp> {
CameraController controller;
IOWebSocketChannel channel = IOWebSocketChannel.connect('ws://192.168.0.6:8000/');
#override
void initState() {
super.initState();
controller = CameraController(cameras[1], ResolutionPreset.low);
controller.initialize().then((_) async{
await controller.startImageStream((onAvailable) {
sd(onAvailable);
});
if (!mounted) {
return;
}
setState(() {});
});
}
#override
void dispose() {
controller?.dispose();
super.dispose();
}
#override
void sd(onAvailable) {
var framesY = onAvailable.planes[0].bytes;
List<int> gzipBytes = new GZipEncoder().encode(framesY);
String compressedString = base64Encode(gzipBytes);
channel.sink.add(compressedString);
}
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return new Scaffold(
body: new Container(
child: new AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: new CameraPreview(controller),
),
),
);
}
}