Skip to content

Commit 9c00fa0

Browse files
committed
blog: new flutter story app
1 parent bb9231c commit 9c00fa0

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
+++
2+
title = 'Flutter study - Story App'
3+
summary = "With full code"
4+
date = "2025-03-26"
5+
# lastmod = 2025-03-11
6+
tags = ["flutter", "study", "coding", "sample"]
7+
coverImg = "story.webp"
8+
# externalUrl = "https://www.example.com" # Redirect
9+
draft = false
10+
showDate = true
11+
showReadingTime = false
12+
showToC = false
13+
showComments = true
14+
+++
15+
16+
Source code reference: <https://www.geeksforgeeks.org/flutter-story-app/>
17+
18+
I made some modifications here.
19+
20+
### lib/main.dart
21+
22+
```dart {linenos=true}
23+
import 'package:flutter/material.dart';
24+
25+
import 'widgets/appbar.dart';
26+
import 'screens/story_home.dart';
27+
28+
void main() {
29+
runApp(const MainApp());
30+
}
31+
32+
class MainApp extends StatelessWidget {
33+
const MainApp({super.key});
34+
35+
@override
36+
Widget build(BuildContext context) {
37+
return MaterialApp(
38+
title: "Story app",
39+
theme: ThemeData(
40+
colorSchemeSeed: Color(0xff2222ff),
41+
useMaterial3: true,
42+
),
43+
home: Scaffold(
44+
appBar: appBar(context: context, title: "Story app home"),
45+
body: StoryHome(),
46+
),
47+
);
48+
}
49+
}
50+
```
51+
52+
### lib/db/story.dart
53+
54+
```dart {linenos=true}
55+
import '../models/story.dart';
56+
57+
final List<Story> stories = [
58+
Story(
59+
title: 'The Lion and the Mouse',
60+
content:
61+
'Once upon a time, a lion was sleeping in the forest when a mouse ran over his nose. The lion woke up and was about to eat the mouse when the mouse begged for mercy. The lion laughed at the tiny mouse, but decided to let him go. Later, the lion got caught in a hunter\'s trap, and the mouse came to his rescue by gnawing through the ropes.',
62+
),
63+
Story(
64+
title: 'The Ant and the Grasshopper',
65+
content:
66+
'In a field one summer\'s day a Grasshopper was hopping about, chirping and singing to its heart\'s content. An Ant passed by, bearing along with great toil an ear of corn he was taking to the nest. "Why not come and chat with me," said the Grasshopper, "instead of toiling in that way?" "I am helping to lay up food for the winter," said the Ant, "and recommend you to do the same."',
67+
),
68+
Story(
69+
title: 'The Tortoise and the Hare',
70+
content:
71+
'Once upon a time, a tortoise and a hare had a race. The hare was very confident that he would win, so he took a nap during the race. Meanwhile, the tortoise slowly but steadily made his way to the finish line, and won the race.',
72+
),
73+
];
74+
```
75+
76+
### lib/models/story.dart
77+
78+
```dart {linenos=true}
79+
class Story {
80+
final String title;
81+
final String content;
82+
83+
Story({required this.title, required this.content});
84+
}
85+
```
86+
87+
### lib/screens/story_home.dart
88+
89+
```dart {linenos=true}
90+
import 'package:flutter/material.dart';
91+
92+
import '../db/story_data.dart';
93+
import 'story_screen.dart';
94+
95+
class StoryHome extends StatelessWidget {
96+
const StoryHome({super.key});
97+
98+
@override
99+
Widget build(BuildContext context) {
100+
return ListView.builder(
101+
itemCount: stories.length,
102+
itemBuilder: (context, index) {
103+
final story = stories[index];
104+
return ListTile(
105+
title: Text(story.title),
106+
onTap: () {
107+
Navigator.push(
108+
context,
109+
MaterialPageRoute(
110+
builder: (context) => StoryScreen(story: story),
111+
),
112+
);
113+
},
114+
);
115+
},
116+
);
117+
}
118+
}
119+
```
120+
121+
### lib/screens/story_screen.dart
122+
123+
```dart {linenos=true}
124+
import 'package:flutter/material.dart';
125+
126+
import '../widgets/appbar.dart';
127+
import '../models/story.dart';
128+
129+
class StoryScreen extends StatelessWidget {
130+
final Story story;
131+
const StoryScreen({
132+
super.key,
133+
required this.story,
134+
});
135+
136+
@override
137+
Widget build(BuildContext context) {
138+
return Scaffold(
139+
appBar: appBar(context: context, title: story.title),
140+
body: Padding(
141+
padding: const EdgeInsets.all(20.0),
142+
child: Text(
143+
story.content,
144+
style: TextStyle(
145+
fontSize: 17,
146+
height: 2,
147+
),
148+
),
149+
),
150+
);
151+
}
152+
}
153+
```
154+
155+
### lib/widgets/appbar.dart
156+
157+
```dart {linenos=true}
158+
import 'package:flutter/material.dart';
159+
160+
AppBar appBar({required BuildContext context, required String title}) {
161+
return AppBar(
162+
title: Text(
163+
title,
164+
style: Theme.of(context).textTheme.headlineLarge,
165+
),
166+
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
167+
titleTextStyle: TextStyle(
168+
color: Theme.of(context).colorScheme.onPrimary,
169+
),
170+
);
171+
}
172+
```
21.9 KB
Loading

0 commit comments

Comments
 (0)