【发布时间】:2021-11-27 07:24:29
【问题描述】:
我已经用 StreamBuilder
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:login_page_animation/blocs/auth_bloc.dart';
import 'package:login_page_animation/doctor_details_page.dart';
import 'package:login_page_animation/main.dart';
import 'package:provider/provider.dart';
class MainScreen extends StatefulWidget {
const MainScreen({Key? key}) : super(key: key);
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
StreamSubscription<User>? loginStateSubscription;
@override
void initState() {
var authBloc = Provider.of<AuthBloc>(context, listen: false);
loginStateSubscription = authBloc.currentUser.listen((fbUser) {
if (fbUser == null) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomePage(),
),
);
}
}) as StreamSubscription<User>?;
super.initState();
}
@override
void dispose() {
loginStateSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
final authBloc = Provider.of<AuthBloc>(context);
Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Color.fromRGBO(70, 212, 153, 0.8),
appBar: AppBar(
elevation: 0.0,
backgroundColor: Color.fromRGBO(70, 212, 153, 0.0),
actions: [
GestureDetector(
child: Container(
margin: EdgeInsets.only(right: 10),
child: Icon(
Icons.notifications_rounded,
color: Colors.white,
),
),
)
],
),
body: Center(
child: StreamBuilder<User?>(
stream: authBloc.currentUser,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
return Container(
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.only(
topRight: Radius.circular(30),
topLeft: Radius.circular(30),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(top: 20, left: 20),
child: Text(
snapshot.data!.displayName!,
style: TextStyle(
color: Color(0xff363636),
fontSize: 25,
fontFamily: 'Roboto',
),
),
),
Container(
margin: EdgeInsets.only(top: 5, left: 20),
child: Text(
"Welcome Back",
style: TextStyle(
color: Color(0xff363636),
fontSize: 30,
fontWeight: FontWeight.bold,
fontFamily: 'Roboto',
),
),
),
Container(
margin: EdgeInsets.only(top: 25, left: 20, right: 20),
width: size.width,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: Color(0x14000000),
offset: Offset(0, 10),
blurRadius: 15,
spreadRadius: 0,
),
],
),
child: Row(
children: [
Expanded(
flex: 5,
child: Container(
margin: EdgeInsets.only(left: 10, right: 10),
child: TextField(
maxLines: 1,
autofocus: false,
style: TextStyle(
color: Color(0xff107163), fontSize: 20),
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Search..',
),
cursorColor: Color(0xff107163),
),
),
),
Expanded(
flex: 1,
child: Container(
decoration: BoxDecoration(
color: Color(0xff107163),
borderRadius: BorderRadius.circular(5),
),
child: Center(
child: Icon(
Icons.search,
color: Colors.white,
size: 25,
),
),
),
),
],
),
),
Container(
width: size.width,
margin: EdgeInsets.only(top: 20, left: 20),
child: Stack(
fit: StackFit.loose,
children: [
Container(
child: Text(
'Category',
style: TextStyle(
color: Color(0xff363636),
fontSize: 20,
fontFamily: 'Roboto',
fontWeight: FontWeight.w700,
),
),
),
Container(
margin: EdgeInsets.only(right: 20, top: 1),
child: Align(
alignment: Alignment.centerRight,
child: Text(
'See all',
style: TextStyle(
color: Color(0xff5e5d5d),
fontSize: 19,
fontFamily: 'Roboto',
),
),
),
)
],
),
),
Container(
height: 120,
margin: EdgeInsets.only(top: 20, left: 20),
child: ListView(
scrollDirection: Axis.horizontal,
children: [
demoCategories("assets/images/tooth.png", "Tooth"),
demoCategories("assets/images/brain.png", "Brain"),
demoCategories("assets/images/heart.png", "Heart"),
demoCategories("assets/images/bone.png", "Bone"),
],
),
),
Container(
width: size.width,
margin: EdgeInsets.only(top: 20, left: 20),
child: Stack(
fit: StackFit.loose,
children: [
Container(
child: Text(
'Top Rated',
style: TextStyle(
color: Color(0xff363636),
fontSize: 20,
fontFamily: 'Roboto',
fontWeight: FontWeight.w700,
),
),
),
Container(
margin: EdgeInsets.only(right: 20, top: 1),
child: Align(
alignment: Alignment.centerRight,
child: Text(
'See all',
style: TextStyle(
color: Color(0xff5e5d5d),
fontSize: 19,
fontFamily: 'Roboto',
),
),
),
)
],
),
),
Expanded(
child: Container(
margin: EdgeInsets.only(left: 20, right: 20),
child: ListView(
children: [
demoTopRatedDr(
"assets/images/leaf.png",
"Dr. Lata Grover",
"Homeopathic Doctor",
"4.8",
),
],
),
),
)
],
),
);
},
),
),
drawer: StreamBuilder<User?>(
stream: authBloc.currentUser,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Drawer(
elevation: 0,
child: ListView(
padding: EdgeInsets.zero,
children: [
UserAccountsDrawerHeader(
accountName: Text(snapshot.data!.displayName!),
accountEmail: Text(snapshot.data!.email!),
decoration:
BoxDecoration(color: Color.fromRGBO(70, 212, 153, 1)),
currentAccountPicture: CircleAvatar(
backgroundImage: NetworkImage(snapshot.data!.photoURL!),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () {
authBloc.logout();
},
child: Text(
"Logout",
style: TextStyle(fontSize: 20),
),
),
IconButton(
onPressed: () {
authBloc.logout();
},
icon: Icon(Icons.logout))
],
)
],
),
);
}
return CircularProgressIndicator();
}),
);
}
Widget demoCategories(String img, String name) {
return Container(
width: 100,
margin: EdgeInsets.only(right: 15),
decoration: BoxDecoration(
color: Color(0xff107163),
borderRadius: BorderRadius.circular(10),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child: Image.asset(img),
),
Container(
margin: EdgeInsets.only(top: 10),
child: Text(
name,
style: TextStyle(
color: Colors.white,
fontSize: 13,
fontFamily: 'Roboto',
fontWeight: FontWeight.w500,
),
),
),
],
),
);
}
Widget demoTopRatedDr(
String img,
String name,
String speciality,
String rating,
) {
var size = MediaQuery.of(context).size;
return GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => DoctorDetailPage()));
},
child: Container(
height: 90,
margin: EdgeInsets.only(top: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(left: 20),
height: 90,
width: 50,
child: Image.asset(img),
),
Container(
margin: EdgeInsets.only(left: 20, top: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(top: 10),
child: Text(
name,
style: TextStyle(
color: Color(0xff363636),
fontSize: 17,
fontFamily: 'Roboto',
fontWeight: FontWeight.w700,
),
),
),
Container(
margin: EdgeInsets.only(top: 10),
child: Row(
children: [
Text(
name,
style: TextStyle(
color: Color(0xffababab),
fontFamily: 'Roboto',
fontWeight: FontWeight.w300,
),
),
Container(
margin:
EdgeInsets.only(top: 3, left: size.width * 0.25),
child: Row(
children: [
Container(
child: Text(
"Rating: ",
style: TextStyle(
color: Colors.black,
fontSize: 12,
fontFamily: 'Roboto',
),
),
),
Container(
child: Text(
rating,
style: TextStyle(
color: Colors.black,
fontSize: 12,
fontFamily: 'Roboto',
),
),
)
],
),
)
],
),
),
],
),
),
],
),
),
);
}
}
【问题讨论】: