import 'package:flutter/material.dart'; void main() { runApp(UnixApp()); } class UnixApp…
- User
import 'package:flutter/material.dart';
void main() { runApp(UnixApp()); }
class UnixApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'UNIX App', debugShowCheckedModeBanner: false, initialRoute: '/', routes: { '/': (context) => WelcomePage(), '/selection': (context) => SelectionPage(), }, onGenerateRoute: (settings) { if (settings.name == '/development') { final args = settings.arguments as Map<String, dynamic>; return MaterialPageRoute( builder: (context) { return DevelopmentPage( selectedSubjects: args['selectedSubjects'], ); }, ); } assert(false, 'Need to implement ${settings.name}'); return null; }, theme: ThemeData( fontFamily: 'Involve', // Set the default font family here // Optionally, define text themes to specify styles for different text types textTheme: TextTheme( bodyLarge: TextStyle(color: Colors.black), bodyMedium: TextStyle(color: Colors.black), // Define other text styles if needed ), ), ); } }
class WelcomePage extends StatelessWidget { final Color darkAccent = Color.fromARGB(255, 0, 0, 0); // Dark blue
@override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color.fromARGB(255, 255, 255, 240), // Fully opaque light color body: Center( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'Добро', style: TextStyle( fontSize: 32, color: darkAccent, fontWeight: FontWeight.bold), ), Text( 'пожаловать', style: TextStyle( fontSize: 32, color: darkAccent, fontWeight: FontWeight.bold), ), Text( 'в', style: TextStyle( fontSize: 32, color: darkAccent, fontWeight: FontWeight.bold), ), Text( 'UNIX', style: TextStyle( fontSize: 32, color: darkAccent, fontWeight: FontWeight.bold), ), SizedBox(height: 16), // Added spacing for better layout Text( 'Давай выберем твои предметы', style: TextStyle( fontSize: 17, color: darkAccent, height: 1.5, ), textAlign: TextAlign.center, // Center the text ), SizedBox(height: 30), ElevatedButton( onPressed: () { Navigator.pushNamed(context, '/selection'); }, child: Text( 'Начать', style: TextStyle( fontSize: 18, color: Color.fromARGB(255, 255, 255, 240)), ), style: ElevatedButton.styleFrom( backgroundColor: darkAccent, // Button background color padding: EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), ), ], ), ), ), ); } }
class SelectionPage extends StatefulWidget { @override _SelectionPageState createState() => _SelectionPageState(); }
class _SelectionPageState extends State { final Color darkAccent = Color.fromARGB(255, 0, 0, 0); // Simple dark final Color darkPrimary = Color.fromARGB( 255, 52, 49, 49); // Fully opaque dark green for inactive elements
// State variables for switches bool russianSelected = false; bool mathSelected = false; bool informaticsSelected = false; bool physicsSelected = false; bool obshestvoSelected = false; bool englishSelected = false; bool chemistrySelected = false; bool literatureSelected = false; bool historySelected = false; bool biologySelected = false;
// Controllers for text fields TextEditingController russianController = TextEditingController(text: '50'); TextEditingController mathController = TextEditingController(text: '50'); TextEditingController informaticsController = TextEditingController(text: '50'); TextEditingController physicsController = TextEditingController(text: '50'); TextEditingController obshestvoController = TextEditingController(text: '50'); TextEditingController englishController = TextEditingController(text: '50'); TextEditingController chemistryController = TextEditingController(text: '50'); TextEditingController literatureController = TextEditingController(text: '50'); TextEditingController historyController = TextEditingController(text: '50'); TextEditingController biologyController = TextEditingController(text: '50');
@override void dispose() { russianController.dispose(); mathController.dispose(); informaticsController.dispose(); physicsController.dispose(); obshestvoController.dispose(); englishController.dispose(); chemistryController.dispose(); literatureController.dispose(); historyController.dispose(); biologyController.dispose(); super.dispose(); }
// Method to collect selected subjects and their scores Map<String, double> _getSelectedSubjects() { Map<String, double> selected = {}; if (russianSelected) { double value = double.tryParse(russianController.text) ?? 0; selected['Русский'] = value; } if (mathSelected) { double value = double.tryParse(mathController.text) ?? 0; selected['Математика'] = value; } if (informaticsSelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['Информатика'] = value; } if (physicsSelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['Физика'] = value; } if (obshestvoSelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['Обществознание'] = value; } if (englishSelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['Английский'] = value; } if (chemistrySelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['Химия'] = value; } if (literatureSelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['Литература'] = value; } if (historySelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['История'] = value; } if (biologySelected) { double value = double.tryParse(informaticsController.text) ?? 0; selected['Биология'] = value; } return selected; }
@override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color.fromARGB(255, 255, 255, 240), // Fully opaque white appBar: AppBar( title: Text( 'Выбор предметов', style: TextStyle(color: darkAccent), ), backgroundColor: Color.fromARGB(255, 255, 255, 240), // Fully opaque white background elevation: 0, // Remove shadow leading: IconButton( icon: Icon(Icons.arrow_back, color: darkAccent), onPressed: () { Navigator.pop(context); }, ), ), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ SubjectBlock( title: 'Русский', isSelected: russianSelected, onToggle: (value) { setState(() { russianSelected = value; }); }, controller: russianController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 20), SubjectBlock( title: 'Математика', isSelected: mathSelected, onToggle: (value) { setState(() { mathSelected = value; }); }, controller: mathController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 20), SubjectBlock( title: 'Информатика', isSelected: informaticsSelected, onToggle: (value) { setState(() { informaticsSelected = value; }); }, controller: informaticsController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), SubjectBlock( title: 'Физика', isSelected: physicsSelected, onToggle: (value) { setState(() { physicsSelected = value; }); }, controller: physicsController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), SubjectBlock( title: 'Обществознание', isSelected: obshestvoSelected, onToggle: (value) { setState(() { obshestvoSelected = value; }); }, controller: obshestvoController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), SubjectBlock( title: 'Английский', isSelected: englishSelected, onToggle: (value) { setState(() { englishSelected = value; }); }, controller: englishController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), SubjectBlock( title: 'Химия', isSelected: chemistrySelected, onToggle: (value) { setState(() { chemistrySelected = value; }); }, controller: chemistryController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), SubjectBlock( title: 'Литература', isSelected: literatureSelected, onToggle: (value) { setState(() { literatureSelected = value; }); }, controller: literatureController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), SubjectBlock( title: 'История', isSelected: historySelected, onToggle: (value) { setState(() { historySelected = value; }); }, controller: historyController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), SubjectBlock( title: 'Биология', isSelected: biologySelected, onToggle: (value) { setState(() { biologySelected = value; }); }, controller: biologyController, darkAccent: darkAccent, darkPrimary: darkPrimary, ), SizedBox(height: 40), ElevatedButton( onPressed: () { Map<String, double> selectedSubjects = _getSelectedSubjects(); Navigator.pushNamed( context, '/development', arguments: {'selectedSubjects': selectedSubjects}, ); }, child: Text( 'Продолжить', style: TextStyle( fontSize: 18, color: const Color.fromARGB(255, 255, 255, 255)), ), style: ElevatedButton.styleFrom( backgroundColor: darkAccent, // Button background color padding: EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), ), ), ], ), ), ), ); } }
class SubjectBlock extends StatelessWidget { final String title; final bool isSelected; final Function(bool) onToggle; final TextEditingController controller; final Color darkAccent; final Color darkPrimary;
SubjectBlock({ required this.title, required this.isSelected, required this.onToggle, required this.controller, required this.darkAccent, required this.darkPrimary, });
@override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( title, style: TextStyle(fontSize: 20, color: darkAccent), ), Switch( value: isSelected, onChanged: onToggle, activeColor: darkAccent, activeTrackColor: darkAccent.withOpacity(0.5), inactiveThumbColor: darkAccent, inactiveTrackColor: darkPrimary.withOpacity(0.5), ), ], ), if (isSelected) Padding( padding: const EdgeInsets.only(top: 10.0), child: TextField( controller: controller, keyboardType: TextInputType.number, decoration: InputDecoration( labelText: 'Баллы', border: OutlineInputBorder(), // Optional: Add error text or validation ), onChanged: (value) { // Optional: Add any additional logic or validation here }, ), ), ], ); } }
class DevelopmentPage extends StatelessWidget { final Map<String, double> selectedSubjects; final Color customTextColor = Color.fromARGB(255, 255, 253, 253); // Fully white color
DevelopmentPage({required this.selectedSubjects});
@override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color.fromARGB(255, 255, 255, 240), appBar: AppBar( title: Text( 'Ваши результаты', style: TextStyle(color: Color.fromARGB(255, 0, 0, 0)), ), backgroundColor: Color.fromARGB(255, 255, 255, 240), // Fully opaque white background elevation: 0, // Remove shadow leading: IconButton( icon: Icon(Icons.arrow_back, color: Color.fromARGB(255, 0, 0, 0)), onPressed: () { Navigator.pop(context); }, ), ), body: Padding( padding: const EdgeInsets.all(16.0), child: selectedSubjects.isNotEmpty ? ListView.builder( itemCount: selectedSubjects.length, itemBuilder: (context, index) { String subject = selectedSubjects.keys.elementAt(index); double score = selectedSubjects[subject]!; return Card( color: Color.fromARGB(255, 31, 31, 31), margin: EdgeInsets.symmetric(vertical: 8.0), child: ListTile( title: Text( subject, style: TextStyle( fontSize: 20, color: customTextColor, fontWeight: FontWeight.bold, ), ), subtitle: Text( 'Баллы: ${score.toInt()}', style: TextStyle( fontSize: 16, color: customTextColor, ), ), ), ); }, ) : Center( child: Text( 'Нет выбранных предметов.', style: TextStyle( fontSize: 20, color: customTextColor, ), ), ), ), ); } }
animate result's appear in card with default staggered animation
- Assistant
To add staggered animations to the cards in your
DevelopmentPage, you can use theflutter_staggered_animationspackage. This package simplifies the process of adding staggered animations to list items, making your UI more engaging.Here's a step-by-step guide to implement staggered animations for your result cards:
1. Add the
flutter_staggered_animationsPackageFirst, you need to add the
flutter_staggered_animationspackage to your project.a. Open your
pubspec.yamlfile and add the package underdependencies:dependencies: flutter: sdk: flutter flutter_staggered_animations: ^1.2.0b. Run
flutter pub getto install the package.flutter pub get2. Modify the
DevelopmentPageto Include AnimationsNext, update your
DevelopmentPageto utilize the staggered animations. Here's how you can do it:a. Import the necessary packages at the top of your
DevelopmentPage:import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';b. Update the
DevelopmentPageto wrap theListView.builderwithAnimationLimiterand useAnimationConfiguration,SlideAnimation, andFadeInAnimationfor each card.Here's the modified
DevelopmentPage:class DevelopmentPage extends StatelessWidget { final Map<String, double> selectedSubjects; final Color customTextColor = Color.fromARGB(255, 255, 253, 253); // Fully white color DevelopmentPage({required this.selectedSubjects}); @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color.fromARGB(255, 255, 255, 240), appBar: AppBar( title: Text( 'Ваши результаты', style: TextStyle(color: Color.fromARGB(255, 0, 0, 0)), ), backgroundColor: Color.fromARGB(255, 255, 255, 240), // Fully opaque white background elevation: 0, // Remove shadow leading: IconButton( icon: Icon(Icons.arrow_back, color: Color.fromARGB(255, 0, 0, 0)), onPressed: () { Navigator.pop(context); }, ), ), body: Padding( padding: const EdgeInsets.all(16.0), child: selectedSubjects.isNotEmpty ? AnimationLimiter( child: ListView.builder( itemCount: selectedSubjects.length, itemBuilder: (context, index) { String subject = selectedSubjects.keys.elementAt(index); double score = selectedSubjects[subject]!; return AnimationConfiguration.staggeredList( position: index, duration: const Duration(milliseconds: 375), child: SlideAnimation( verticalOffset: 50.0, child: FadeInAnimation( child: Card( color: Color.fromARGB(255, 31, 31, 31), margin: EdgeInsets.symmetric(vertical: 8.0), child: ListTile( title: Text( subject, style: TextStyle( fontSize: 20, color: customTextColor, fontWeight: FontWeight.bold, ), ), subtitle: Text( 'Баллы: ${score.toInt()}', style: TextStyle( fontSize: 16, color: customTextColor, ), ), ), ), ), ), ); }, ), ) : Center( child: Text( 'Нет выбранных предметов.', style: TextStyle( fontSize: 20, color: customTextColor, ), ), ), ), ); } }3. Explanation of the Changes
Let's break down what we've done:
-
Importing the Package:
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';This import gives you access to the animation widgets provided by the package.
-
Wrapping
ListView.builderwithAnimationLimiter:AnimationLimiter( child: ListView.builder( // ... ), )AnimationLimiterensures that animations are only applied to visible items, which optimizes performance. -
Configuring Each List Item with
AnimationConfiguration:AnimationConfiguration.staggeredList( position: index, duration: const Duration(milliseconds: 375), child: SlideAnimation( verticalOffset: 50.0, child: FadeInAnimation( child: Card( // ... ), ), ), )AnimationConfiguration.staggeredList: Sets up the staggered timing based on the item's position in the list.SlideAnimation: Makes the card slide in from a vertical offset (50.0pixels here).FadeInAnimation: Causes the card to fade in as it slides.Card: The original card widget containing the subject's information.
4. Optional: Customize the Animation
You can further customize the animation by adjusting parameters like
verticalOffset,duration, and adding more animation types (e.g., scaling). Here's an example with a slight scale effect:AnimationConfiguration.staggeredList( position: index, duration: const Duration(milliseconds: 375), child: SlideAnimation( verticalOffset: 50.0, child: FadeInAnimation( child: ScaleAnimation( scale: 0.5, child: Card( // ... ), ), ), ), )In this example, each card also scales up from 50% to 100% of its size as it appears.
5. Final Code Snapshot
For clarity, here's the complete
DevelopmentPageclass with the staggered animations:import 'package:flutter/material.dart'; import 'package:flutter_staggered_animations/flutter_staggered_animations.dart'; class DevelopmentPage extends StatelessWidget { final Map<String, double> selectedSubjects; final Color customTextColor = Color.fromARGB(255, 255, 253, 253); // Fully white color DevelopmentPage({required this.selectedSubjects}); @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color.fromARGB(255, 255, 255, 240), appBar: AppBar( title: Text( 'Ваши результаты', style: TextStyle(color: Color.fromARGB(255, 0, 0, 0)), ), backgroundColor: Color.fromARGB(255, 255, 255, 240), // Fully opaque white background elevation: 0, // Remove shadow leading: IconButton( icon: Icon(Icons.arrow_back, color: Color.fromARGB(255, 0, 0, 0)), onPressed: () { Navigator.pop(context); }, ), ), body: Padding( padding: const EdgeInsets.all(16.0), child: selectedSubjects.isNotEmpty ? AnimationLimiter( child: ListView.builder( itemCount: selectedSubjects.length, itemBuilder: (context, index) { String subject = selectedSubjects.keys.elementAt(index); double score = selectedSubjects[subject]!; return AnimationConfiguration.staggeredList( position: index, duration: const Duration(milliseconds: 375), child: SlideAnimation( verticalOffset: 50.0, child: FadeInAnimation( child: Card( color: Color.fromARGB(255, 31, 31, 31), margin: EdgeInsets.symmetric(vertical: 8.0), child: ListTile( title: Text( subject, style: TextStyle( fontSize: 20, color: customTextColor, fontWeight: FontWeight.bold, ), ), subtitle: Text( 'Баллы: ${score.toInt()}', style: TextStyle( fontSize: 16, color: customTextColor, ), ), ), ), ), ), ); }, ), ) : Center( child: Text( 'Нет выбранных предметов.', style: TextStyle( fontSize: 20, color: customTextColor, ), ), ), ), ); } }6. Run Your App
After making these changes, run your app. Navigate to the
DevelopmentPage, and you should see each result card animate into view with a staggered slide and fade effect.7. Additional Tips
-
Performance Considerations: While the
flutter_staggered_animationspackage is optimized for performance, ensure that you're not animating an excessively large number of items at once, which could affect performance on lower-end devices. -
Customization: Explore different animation types and configurations provided by the package to achieve the desired visual effect. You can combine multiple animations like scaling, rotating, etc., to create more dynamic interfaces.
-
Fallback for Large Lists: If you have a very large list of items, consider implementing lazy loading or pagination to maintain smooth animations and scrolling performance.
8. Alternative: Manual Staggered Animations
If you prefer not to use an external package, you can implement staggered animations manually using Flutter's
AnimatedList,AnimationController, orAnimatedBuilder. However, this approach requires more boilerplate code and is generally more complex.Here's a very basic example using
AnimatedOpacityandAnimatedSlide:class DevelopmentPage extends StatefulWidget { final Map<String, double> selectedSubjects; DevelopmentPage({required this.selectedSubjects}); @override _DevelopmentPageState createState() => _DevelopmentPageState(); } class _DevelopmentPageState extends State<DevelopmentPage> with TickerProviderStateMixin { late List<AnimationController> _controllers; @override void initState() { super.initState(); _controllers = List.generate(widget.selectedSubjects.length, (index) { AnimationController controller = AnimationController( duration: Duration(milliseconds: 500 + index * 100), vsync: this, ); controller.forward(); return controller; }); } @override void dispose() { for (var controller in _controllers) { controller.dispose(); } super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( // ... (AppBar and other UI elements remain the same) body: Padding( padding: const EdgeInsets.all(16.0), child: widget.selectedSubjects.isNotEmpty ? ListView.builder( itemCount: widget.selectedSubjects.length, itemBuilder: (context, index) { String subject = widget.selectedSubjects.keys.elementAt(index); double score = widget.selectedSubjects[subject]!; return FadeTransition( opacity: _controllers[index], child: SlideTransition( position: Tween<Offset>( begin: Offset(0, 0.3), end: Offset.zero, ).animate( CurvedAnimation( parent: _controllers[index], curve: Curves.easeOut, ), ), child: Card( color: Color.fromARGB(255, 31, 31, 31), margin: EdgeInsets.symmetric(vertical: 8.0), child: ListTile( title: Text( subject, style: TextStyle( fontSize: 20, color: Color.fromARGB(255, 255, 253, 253), fontWeight: FontWeight.bold, ), ), subtitle: Text( 'Баллы: ${score.toInt()}', style: TextStyle( fontSize: 16, color: Color.fromARGB(255, 255, 253, 253), ), ), ), ), ), ); }, ) : Center( child: Text( 'Нет выбранных предметов.', style: TextStyle( fontSize: 20, color: Color.fromARGB(255, 255, 253, 253), ), ), ), ), ); } }Note: This manual approach lacks the flexibility and features of the
flutter_staggered_animationspackage and is provided here for educational purposes.
By following the steps above, you can enhance your
DevelopmentPagewith elegant staggered animations, improving the overall user experience of your Flutter app. -