Stories Plugin
Ensure the basic integration of the Nudge Core SDK is complete. If not, check here.
Overview
The nudge_storiesv2
package lets you add interactive stories to your app. It includes the NudgeStoriesUi
class and NudgeStories
widget for displaying and customizing stories. Install this plugin to launch stories from the dashboard and increase user engagement.
Find the latest version of nudge_storiesv2
here
Step 1: Installation
Add nudge_storiesv2
to your pubspec.yaml
file:
dependencies:
nudge_storiesv2: ^latest_version
Run the following command to fetch the package:
flutter pub get
Step 2 : Initialization
Import the NudgeStoriesUi
class and NudgeStories
widget from the nudge_flutter_stories package:
import 'package:nudge_storiesv2/StoriesUI.dart';
import 'package:nudge_storiesv2/nudge_storiesv2.dart';
Pass an NudgeStoriesUi
class inside the plugins, in the NudgeProvider
.
return NudgeProvider(
nudgeInstance: core,
plugins : [NudgeStoriesUi()] //Add stories plugin here
child: MaterialApp(
navigatorKey: NudgeProviderState.navigatorKey,
navigatorObservers : [_trackerObserver],
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Home(),
),
);
Step 3: Add NudgeStories Widget
Place the NudgeStories
widget in your app where you want to display stories, and call the track function for the event using the track
method provided by the nudgecore_v2
package:
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const NudgeStories(),
ElevatedButton(
onPressed: () async {
nudge.track(type: "show_story");
},
child: const Text(
'Get Stories',
style: TextStyle(fontSize: 20),
),
),
],
),
),
);
}
Example
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nudgecore_v2/core/Nudge.dart';
import 'package:nudgecore_v2/core/nudge_provider.dart';
import 'package:nudgecore_v2/utilities/NudgeGlobalCallback.dart';
import 'package:nudge_storiesv2/StoriesUI.dart';
import 'package:nudge_storiesv2/nudge_storiesv2.dart';
void main() {
runApp(
MainApp(),
);
}
String apiKey = 'API_KEY';
Nudge nudge = Nudge(
apiKey: apiKey,
);
String token = "UNINITIALIZED";
String nudgeId = "UNINITIALIZED";
class MainApp extends StatelessWidget {
MainApp({super.key});
final NudgeTrackerObserver _trackerObserver = NudgeTrackerObserver();
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
nudge.userIdentifier(externalId: "EXTERNAL_ID");
final stories = NudgeStoriesUi();
return NudgeProvider(
nudgeInstance: nudge,
plugins: [
stories,
],
child: NudgeStoriesProvider(
child: MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
routes: {
"/": (context) => const HomeScreen(),
},
navigatorKey: NudgeProviderState.navigatorKey,
navigatorObservers: [_trackerObserver],
),
),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const NudgeStories(),
ElevatedButton(
onPressed: () async {
nudge.track(type: "TRIGGER_TYPE");
},
child: const Text(
'Get Stories',
style: TextStyle(fontSize: 20),
),
),
],
),
),
);
}
}
Stories UI Customizations
This section outlines the customizations available for the Stories.
NudgeStories(
// Sizing and Layout
trayHeight: 200, // Set the height of the entire story bar
trayItemWidth: 100, //The width for all story thumbnails
trayItemHeight: 100 //The height of all story thumbnails
trayItemRoundness : 10 // Set the roundness of the thumbnail
//Layout
horizontalEdgePadding: 10, // Set horizontal edge padding of the entire story bar
verticalEdgePadding: 10, // Set vertical edge padding of the entire story bar
itemSpacing: 10, // Set the spacing between individual story thumbnails
trayVerticalAlignment: TrayAlignment.top // Set the vertical alignment of the tray
trayItemContentGap:10, // Set the gap between the thumbnail and the text for every story
trayItemMargin: EdgeInsets.all(0) // Set the margin around every story thumbnail
//Text customizations
fontFamily: 'Abel', // Set the font family for the title and subtitle
showText: true, // Show or hide text for all story thumbnails
headingLightModeColor: Colors.black, // Set heading color for all story thumbnails in light mode
headingDarkModeColor: Colors.white, // Set heading color for all story thumbnails in dark mode
subheadingDarkModeColor: Colors.red, // Set subheading color for all story thumbnails in dark mode
subheadingLightModeColor: Colors.blue, // Set subheading color for all story thumbnails light mode
//Colors
skeletonColor: Colors.grey, // Set the color for the skeleton of every single story thumbnail
gradientColors: const [
Colors.red,
Colors.green,
Colors.blue,
Colors.yellow,
], // Apply gradient colors to all the story thumbnails
theme: ThemeData(
primaryColor: Colors.orangeAccent,
fontFamily: 'Dosis',
), // Customize the theme
children: [...], // Any list of widgets
),
Working with List View builder
NudgeStories (
children : [
ListView.builder(
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Container(
margin: const EdgeInsets.only(right: 10),
height: 100,
width: 100,
color: Colors.grey,
);
},
itemCount: 5,
shrinkWrap: true, // Important, this makes the list view builder take the space that it needs
),]
)
Handling multiple scrollable widgets
Incase you plan to add our story widget along with an existing scrollable widget, you refer to the below snippet to handle the scroll across both widgets seamlessly.
const NudgeStories(
customContent: [YourWidget1(...), YourWidget2(...), ...] // @type: List<Widget>
),