正如我在标题中所描述的,我有两条路线或两个类——一个登录屏幕和一个主页,问题是useMaterial3
只在登录屏幕上工作,而在主页上不工作
这是main.dart
import 'package:flutter/material.dart';
import 'package:tesis/home.dart';
import 'package:tesis/login.dart';
import 'package:tesis/theme/color_schemes.g.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Tesis',
theme: ThemeData(
useMaterial3: true,
colorScheme: lightColorScheme,
),
darkTheme: ThemeData(colorScheme: darkColorScheme),
initialRoute: '/',
routes: {
'/': (context) => const LoginScreen(),
'/second': (context) => const Home(),
},
// home: const LoginScreen(),
);
}
}
这是登录屏幕
import 'package:flutter/material.dart';
import 'package:tesis/home.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
bool _visibility = true;
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('images/background.jpg'),
fit: BoxFit.cover,
),
),
child: Center(
child: Container(
width: double.infinity,
height: 447,
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Theme.of(context).colorScheme.background,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
'Bienvenido',
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: 36,
),
),
const Padding(padding: EdgeInsets.all(7.0)),
const Text(
'Ingresar',
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: 24,
),
),
const Padding(
padding: EdgeInsets.all(10.0),
),
SizedBox(
width: 250,
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Usuario',
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.person),
),
),
),
const Padding(
padding: EdgeInsets.all(7.0),
),
SizedBox(
width: 250,
child: TextFormField(
obscureText: _visibility,
decoration: InputDecoration(
border: const OutlineInputBorder(),
prefixIcon: const Icon(Icons.lock),
labelText: 'Contraseña',
suffixIcon: IconButton(
icon: Icon(
_visibility
? Icons.visibility
: Icons.visibility_off,
),
onPressed: () {
setState(() {
_visibility = !_visibility;
});
})),
),
),
const Padding(
padding: EdgeInsets.all(11.0),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
// Foreground color
onPrimary: Colors.white,
// Background color
primary: Theme.of(context).colorScheme.primary,
).copyWith(
elevation: ButtonStyleButton.allOrNull(0.0),
),
onPressed: () {
Navigator.pushNamed(context,'/second');
},
child: const Text('Iniciar Sección'),
),
const Padding(
padding: EdgeInsets.all(11.0),
),
Text.rich(
TextSpan(
text: 'Aún no te has registrado?',
children: <TextSpan>[
TextSpan(
text: ' Registrate',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontWeight: FontWeight.w400,
fontSize: 14,
),
),
]),
),
],
),
),
),
),
);
}
}
这是主页
import 'package:flutter/material.dart';
import 'package:tesis/login.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {},
),
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.background,
leading: IconButton(
icon: Icon(Icons.menu),
onPressed: () {
Navigator.pushNamed(context, '/');
},
),
title: Text('Title Large'),
centerTitle: true,
actions: [
IconButton(
onPressed: (() {}),
icon: Icon(Icons.account_circle_outlined),
)
],
),
);
}
}
我已经解决了这个问题,我只是忘记了将useMaterial3: true,
放入darkTheme: ThemeData(colorScheme: darkColorScheme),
中